Wrong number returns from EEPROM.get arduino - arduino

In the setup function i clear the EEPROM if a specific button is clicked.
in the loop function at the start i have this code:
if(millis() - last_sample >= 180){
sampler();
EEPROM.get(stateEEPROMAdress, stateCode);
stateCode = getState(stateCode);
EEPROM.put(stateEEPROMAdress, stateCode);
Serial.println(stateCode);
}
and a bunch of sampling code.
then at the end of the loop i have:
if(millis() - last_xbee >= 900){
EEPROM.get(packetEEPROMAdress,packetCount );
EEPROM.get(stateEEPROMAdress,stateCode);
if(!initializing || (stateCode!= 0 && stateCode != 1)){
telemetry[2] = packetCount++;}
telemetry[21] =stateCode;
EEPROM.put(packetEEPROMAdress, packetCount);
.... and printing codes...
I also at the start of the sketch i have defined :
const int packetEEPROMAdress = 0;
const int stateEEPROMAdress = packetEEPROMAdress + sizeof(int);
and this is the getState function. simple state determination from sensor values:
int getState(const int stateCode=0){
int outState;
//state 0
if(stateCode == 0 &&(fabs(verticalSpeed) < 2 || fabs(relativeAltitude) < 5)&&initializing){
outState = 0;
//true should change later to see is launch botton is on
} else
if(missionReady && !initializing && stateCode == 0){
outState = 1;
}else if(verticalSpeed > 3&&stateCode == 1){
outState = 2;
}
//else
//if(VerticalSpeed < 3 && stateCode == 2){
// stateCode = 2;
// //apogee but no code in CDR
//}
else if(verticalSpeed < 2 &&stateCode == 2){
outState = 3;
} else if((fabs(relativeAltitude - 450)< 10 || relativeAltitude<440 ) &&stateCode == 3){
outState = 4;
}else
//true should be replaced with seperation photocell is bright
if(true && stateCode == 4){
outState = 5;
}else if(relativeAltitude < 3 && fabs(verticalSpeed) < .7 && stateCode == 5 ){
outState == 6;
//activate Buzzer stop telemetry
}
return outState;
}
everything a good when state is 0 . when i send a command and states becomes 1. the after a few loops that 1 appears. the number in EEPROM becomes 8663 . is there a problem in addressing the EEPROM?

It was sth silly:)
Nothing related to EEPROM.
I didn't give a initial value to outState so large number stored in EEPROM,
Sorry if i got your time:)

Related

Game Of Life ends quickly (Java)

I've created a basic version of the Game Of Life: each turn, the board is simulated by a 2D array of 1's and 0's, after which another class creates a drawing of it for me using the 2d array
I've read all the other questions here regarding this game, but no answer seems to work out for me....sorry if I'm beating a dead horse here.
I think I have a problem with my algorithm, thus maybe the board gets filled with the wrong amount of dead and alive cells and thus ends rather quickly (5-10 turns).
I've found an algorithm here to scan all the neighbors and even added a count = -1 in case it a cell in the grid scans itself as it's own neighbor, but I think I'm missing something here.
public static void repaint(board game, int size,int[][] alive, int[][] newGeneration)
{
int MIN_X = 0, MIN_Y = 0, MAX_X =9, MAX_Y =9, count;
for ( int i = 0; i < size; i++ )
{
for (int j = 0; j < size; j++) //here we check for each matrix cell's neighbors to see if they are alive or dead
{
count = 0;
if (alive[i][j] == 1)
count = -1;
int startPosX = (i - 1 < MIN_X) ? i : i - 1;
int startPosY = (j - 1 < MIN_Y) ? j : j - 1;
int endPosX = (i + 1 > MAX_X) ? i : i + 1;
int endPosY = (j + 1 > MAX_Y) ? j : j + 1;
for (int rowNum = startPosX; rowNum <= endPosX; rowNum++)
{
for (int colNum = startPosY; colNum <= endPosY; colNum++)
{
if (alive[rowNum][colNum] == 1)
count++;
}
}
if (alive[i][j] == 0 && count == 3) //conditions of the game of life
newGeneration[i][j] = 1; //filling the new array for the next life
if (alive[i][j] == 1 && count < 2)
newGeneration[i][j] = 0;
if (alive[i][j] == 1 && count >= 4)
newGeneration[i][j] = 0;
if (alive[i][j] == 1 && count == 3)
newGeneration[i][j] = 1;
}
}
game.setAlive(newGeneration); //we created a new matrix with the new lives, now we set it
SetupGUI(game,size); //re drawing the panel
}
}
What am I doing wrong? thanks for the help.

Arduino Uno R3 resets when trying to print to 16x2 LCD

I'm using an Arduino Uno R3 connected to a 16x2 LCD through I2C. Also using the LiquidCrystal_I2C library to print to the LCD. I recently changed a bunch of my code and this new version of my code makes my Arduino reset as soon as it hits an lcd.print() statement outside void setup(). The code is a work in progress so please bear with any spaghetti code. Here it is:
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Wire.h>
#include "Time.h"
#include "WiFly.h" //include the WiFly experimental library
#include "Credentials.h"
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
char msg[320];
char * ptr = &msg[0];
int letterCount = 0;
int countdownFlag = 0;
int cstopFlag = 0;
int lpcount = 0;
void(* resetFunc)(void) = 0; //pointer to reset function # address 0
long days, hrs, mins, sec, rem1, rem2;
long gametime, timediff, new_timediff, current_time;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;
int flag5 = 0;
int flag6 = 0;
int flag7 = 0;
char hmTeam[25];
char awTeam[25];
char dateTime[25];
WiFlyClient client("api.football-data.org", 80);
void setup(){
//Begin LCD
lcd.begin (16,2);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print("Loading...");
//Begin WiFly and Serial
WiFly.begin();
Serial.begin(9600);
Serial.println("Serial begun :D");
current_time = WiFly.getTime();
Serial.print("connecting to server...");
if(client.connect()){
Serial.println("connected");
client.print("GET http://api.football-data.org/teams/61/fixtures/?timeFrame=n20");
Serial.print("GET http://api.football-data.org/teams/61/fixtures/?timeFrame=n20");
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: api.football-data.org");
Serial.println("Host: api.football-data.org");
client.println("X-Auth-Token: 4f02cc524412487989ee61aed27503d5");
Serial.println("X-Auth-Token: 4f02cc524412487989ee61aed27503d5");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
} else{
Serial.println("connection failed");
}
}
void loop(){
if (client.available()) {
char c = client.read();
Serial.print(c);
if(flag4 == 1 && letterCount < 321){
recordMessage(c);
flag5 = 1;
}
if(c == '\n' && !client.available() && flag5 == 1){
char * ptr2; //declare a second pointer to change reference from start of msg string to start of "homeTeam" - i.e. "h"
int count = 0; //this is to count for hmTeam character array
int i;
for(i = 0; i < 321; i++){
if(*(ptr+i) == 'h' && *(ptr+i+4) == 'T' && *(ptr+i+7) == 'm'){ //once the sequence "h T m" is found, we know we have reached the word "homeTeam" in the string
ptr2 = ptr+i+11;
while(*ptr2 != '"'){
hmTeam[count] = *ptr2;
ptr2++;
count++;
}
}
count = 0;
if(*(ptr+i) == 'a' && *(ptr+i+3) == 'y' && *(ptr+i+4) == 'T' && *(ptr+i+7) == 'm'){
ptr2 = ptr+i+11;
while(*ptr2 != '"'){
awTeam[count] = *ptr2;
ptr2++;
count++;
}
}
count = 0;
if(*(ptr+i) == 'd' && *(ptr+i+2) == 't' && *(ptr+i+3) == 'e'){
ptr2 = ptr+i+7;
while(*ptr2 != '"'){
dateTime[count] = *ptr2;
ptr2++;
count++;
}
}
}
}
if(c == '{'){
flag1 = 1;
}
if(flag1 == 1){
if(c == '"'){
flag2 = 1;
}
}
if(flag2 == 1){
if(c == '_'){
flag3 = 1;
}
}
if(flag3 == 1){
if(c == 'l'){
flag4 = 1;
}
}
}
if(!client.connected() && cstopFlag == 0){
delay(100);
client.flush();
client.stop();
Serial.println();
Serial.println("Disconnected.");
delay(500);
Serial.println();
cstopFlag = 1;
flag7 = 1;
}
if(flag7 == 1){
checkAction();
countdownFlag = 1;
}
if(countdownFlag == 1){
//lcd.setCursor(0,0);
//lcd.setBacklight(LOW);
//teamNamePrint(hmTeam);
//lcd.print(" vs ");
//teamNamePrint(awTeam);
countdown();
}
}
void recordMessage(char message){
msg[letterCount] = message;
letterCount++;
}
/*
void teamNamePrint(){
char team[25];
strcpy(team,hmTeam);
if(team == "Arsenal FC")
lcd.print("Arsenal");
else if (team == "Aston Villa FC")
lcd.print("AVFC");
else if (team =="Burnley FC")
lcd.print("Burnley");
else if (team =="Chelsea FC")
lcd.print("Chelsea");
else if (team =="Crystal Palace FC")
lcd.print("CPFC");
else if (team =="Everton FC")
lcd.print("Everton");
else if (team =="Hull City")
lcd.print("Hull");
else if (team =="Leiceter City FC")
lcd.print("Lcity");
else if (team =="Liverpool FC")
lcd.print("Lpool");
else if (team =="Manchester City FC")
lcd.print("ManCity");
else if (team =="Manchester United")
lcd.print("ManU");
else if (team =="Newcastle FC")
lcd.print("NCFC");
else if (team =="Queens Park Rangers")
lcd.print("QPR");
else if (team =="FC Southampton")
lcd.print("Sthtn");
else if (team =="Stoke FC")
lcd.print("Stoke");
else if (team =="Sunderland FC")
lcd.print("Sndlnd");
else if (team =="Swansea FC")
lcd.print("SwnFC");
else if (team =="Tottenham Hotspur")
lcd.print("Spurs");
else if (team =="West Bromwich Albion")
lcd.print("WBrom");
else if (team =="West Ham Utd")
lcd.print("WHU");
}
*/
void checkAction(){
if(lpcount < 60){
gametime_calc();
timediff = gametime - current_time;
days = timediff / 86400;
rem1 = timediff % 86400;
hrs = rem1 / 3600;
rem2 = rem1 % 3600;
mins = rem2 / 60;
sec = rem2 % 60;
} else if(lpcount == 60){
lpcount = 0;
current_time = WiFly.getTime();
new_timediff = gametime - current_time;
if((new_timediff - timediff) != 0){
lcd.setBacklight(HIGH);
lcd.clear();
lcd.print("Resetting...");
delay(1500);
days = new_timediff / 86400;
rem1 = new_timediff % 86400;
hrs = rem1 / 3600;
rem2 = rem1 % 3600;
mins = rem2 / 60;
sec = rem2 % 60;
lcd.clear();
}
}
}
void countdown(){
lcd.setCursor(0,1);
lcd_print(days,0); //print days
lcd_print(hrs,3); //print hours
lcd_print(mins,6); //print minutes
lcd_print(sec,9); //print seconds
if(sec == 0 && mins == 0 && hrs == 0 && days == 0){
lcd.setBacklight(HIGH);
lcd.setCursor(0,0);
lcd.print("Game begins now!");
lcd.setCursor(0,1);
lcd.print(" ");
delay(30000);
resetFunc(); //call reset
} else if(sec == 0 && mins != 0){
mins--;
lpcount++;
sec = 60;
} else if(sec == 0 && mins == 0 && hrs != 0){
hrs--;
mins = 59;
sec = 60;
} else if(sec == 0 && mins == 0 && hrs == 0 && days != 0){
days--;
hrs = 23;
mins = 59;
sec = 60;
}
if(lpcount == 60){
checkAction();
lcd.setCursor(0,1);
lcd_print(days,0); //print days
lcd_print(hrs,3); //print hours
lcd_print(mins,6); //print minutes
lcd_print(sec,9); //print seconds
}
delay(1000);
sec--;
}
void lcd_print(long period, int col){
if(period < 10){
lcd.setCursor(col,1);
lcd.print("0");
lcd.setCursor(col+1,1);
lcd.print(period);
} else{
lcd.print(period);
}
if(col != 9){
lcd.print(":");
}
}
void gametime_calc(){
TimeElements tm; //create a TimeElements variable
//for conversion to time_t variable
//Fill in the elements of tm with those from the msg array
tm.Second = (dateTime[17] - '0')*10 + (dateTime[18] - '0');
tm.Minute = (dateTime[14] - '0')*10 + (dateTime[15] - '0');
tm.Hour = (dateTime[11] - '0')*10 + (dateTime[12] - '0');
tm.Day = (dateTime[8] - '0')*10 + (dateTime[9] - '0');
tm.Month = (dateTime[5] - '0')*10 + (dateTime[6] - '0');
tm.Year = ((dateTime[0] - '0')*1000 + (dateTime[1] - '0')*100 + (dateTime[2] - '0')*10 + (dateTime[3] - '0')) - 1970;
gametime = makeTime(tm); //game time as time_t variable
}
What could be causing this issue? Possibly hardware?

Strtol implementation different behaviour on 32 and 64 bit machine

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <tgmath.h>
#include <limits.h>
#include <stdbool.h>
#include <errno.h>
#define NEGATIVE -1
#define POSITIVE 1
#define OCTAL 8
#define DECIMAL 10
#define HEXADECIMAL 16
#define BASE_MIN 2
#define BASE_MAX 36
long int strtol (const char * str, char ** endPtr, int base)
{
if(base < 0 || base == 1 || base > BASE_MAX)
{
errno = EINVAL;
return 0L;
}
else
{
bool conversion = true;
int i = 0, sign = POSITIVE, save;
while(isspace(*(str + i)))
i++;
if(*(str + i) == '\0')
{
conversion = false;
save = i;
}
if(*(str + i) == '-')
{
sign = NEGATIVE;
i++;
}
else if(*(str + i) == '+')
i++;
if(base == 0) // find out base
{
if(*(str + i) == '0')
{
if(toupper(*(str + i + 1)) == 'X')
{
base = HEXADECIMAL;
i++;
}
else
base = OCTAL;
i++;
}
else
base = DECIMAL;
}
else if(base == OCTAL)
{
if(*(str + i) == '0')
i++;
}
else if(base == HEXADECIMAL)
{
if(*(str + i) == '0')
if(*(str + i + 1) == 'x' || *(str + i + 1) == 'X')
i += 2;
}
int start = i, end, exp, check = i;
long int long_int, sum, multiplier;
if(conversion) // find out the correct part of the string corresponding to the number
{
if(base < DECIMAL)
{
while(*(str + i) >= '0' && *(str + i) < base + '0') // numbers from 0 to base - 1
i++;
}
else if(base == DECIMAL)
{
while(*(str + i) >= '0' && *(str + i) <= '9') // numbers from 0 to 9
i++;
}
else
{
while((*(str + i) >= '0' && *(str + i) <= '9') || (toupper(*(str + i)) >= 'A' && toupper(*(str + i)) < 'A' + base - 10))
i++;// numbers from 0 to 9 and uper and lowercase letters from a to a + base - 11
}
}
if(i == check && conversion) //no digits at all
{
conversion = false;
save = i;
}
else if(endPtr != NULL && conversion) // assign pointer
*endPtr = (char *) (str + i);
if(conversion)
{
for(end = i - 1, exp = 0, long_int = 0L; end >= start; end--, exp++)
{
multiplier = pow(base, exp);
sum = 0L;
if(*(str + end) >= '0' && *(str + end) <= '9')
sum = (*(str + end) - '0') * multiplier;
else if(*(str + end) >= 'A' && *(str + i) <= (base == BASE_MAX ? 'Z' : 'F'))
sum = (*(str + end) - 'A' + 10) * multiplier;
else if(*(str + end) >= 'a' && *(str + i) <= (base == BASE_MAX ? 'z' : 'f'))
sum = (*(str + end) - 'a' + 10) * multiplier;
if(long_int <= LONG_MIN + sum)
{
errno = ERANGE;
return LONG_MIN;
}
if(long_int >= LONG_MAX - sum)
{
errno = ERANGE;
return LONG_MAX;
}
else
long_int += sum;
}
return sign * long_int;
}
else
{
if(endPtr != NULL)
{// if base is 16 we check if the string given is not in the form 0xIncorrect string in that way we need to return xIncorrect part of the string
if(base == HEXADECIMAL && save >= 2 && toupper(*(str + save - 1)) == 'X' && *(str + save - 2) == '0')
*endPtr = (char *) str + save - 1;
else if(base == OCTAL && save >= 1 && *(str + save - 1) == '0')
*endPtr = (char *) str + save;// if the string is of base 8 and in the form 0incorrect string
else //then we return everything after the 0 as the endptr string
*endPtr = (char *) str;//in other cases no conversion was done so we return original pointer
}
return 0L;
}
}
}
I've got problem with writing implementation of strtol() function. The thing is i compiled it on 64 bit machine and the output was correct but today i checked it on another machine that is 32-bit and something got wrong. 32-bit machine showed the result that for example string "7FFFFFFF" is out of range when on 64-bits the results is that strtol succeded which is the same as for th standard function. I also checked errno value and for 32-bit machine it's set to ERANGE which shouldn't be and it's not not on 64-bit. I have program that checks if your implementation gives the same output as the standard one for different strings. I spent few hours looking for possible bug but i'm out of ideas? Any tips?

Computing multiple numbers with multiple operations

So I am creating a calculator that will compute numbers.I have this code(I will not include very basic codes)
long num1, num2, answer;
boolean mySwitch = false;
boolean do_subtraction_flag = false; // when true we will apply subtraction
boolean multiply = false;
boolean divide = false;
void loop()
{
char keypressed = myKeypad.getKey();
if(keypressed != NO_KEY)
{
Serial.print(keypressed);
if(keypressed > 47 && keypressed < 58) // is between '0' and '9'
{
if(!mySwitch)
{
num1 = (num1 * 10) + (keypressed - 48);
}
else
{
num2 = (num2 * 10) + (keypressed - 48);
}
}
if(keypressed == '=')
{
if(do_subtraction_flag) // we want to subtract the numbers
{
answer = num1 - num2;
}else if(multiply){
answer = num1 * num2;
}else if(divide){
answer = num1 / num2;
}
else // we want to add the numbers
{
answer = num1 + num2;
}
Serial.println(answer);
num1 = 0;
num2 = 0;
mySwitch = false;
do_subtraction_flag = false;
multiply = false;
divide = false;
}
else if(keypressed == '+')
{
mySwitch = true;
}
else if(keypressed == '*'){
mySwitch = true;
multiply = true;
}else if(keypressed == '/'){
mySwitch = true;
divide = true;
}
else if(keypressed == '-')
{
mySwitch = true;
do_subtraction_flag = true;
}else if (keypressed == 'C'){
for (int i=0; i < 80; i++)
{
Serial.write(8); // print 80 times backspace (BS)
}
}
}
}
Im confused here because I want to compute multiple number with multiple operands (eg., 2+1+3 or 2+1-2), but when i add another variable 'num3' what should I suppose to do with it? if i put it into the do_subtraction flag what if the user inputs 2-1+3?Is it possible to compute 3 numbers with this code?I'm getting confused here but let me know if you are also confused what I want to do
Make an array to hold the numbers and operators that you input, then loop through them when the input == '='. Have your num1 always hold the result and num2 hold the next array value.

Arduino multi-dimensional array crash

I have a block of code that does something to this effect:
int pieceX = 0;
int pieceY = 0;
int board[8][47] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
if (pieceX > 0 && pieceY < 46) {
/* If I remove this it doesn't crash */
if (board[pieceX-1][pieceY] == 0 && board[pieceX][pieceY+1] == 0) {
pieceX -= 1;
}
/*-----------------------------------*/
}
As far as I can tell, I'm initializing my array correctly and I'm staying within the index bounds. I don't work much with Processing or Arduino, so I'm hoping it's something simple / obvious.
Edit: Hmm.. I just made a minimalistic test version with this code, and it doesn't crash. So, it's something to do with the code not in this example. Damn. Going to try to zero in on those lines. (My bad for posting this before properly isolating the problem code.) While this accurately describes the problem, it does not reproduce it. Strange bug.
Edit 2: This doesn't crash:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
}
}
}
This doesn't crash:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
pieceX -= 1;
}
}
This DOES crash:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
Any idea what's going on? ButtonA is never HIGH, so.. the code I'm tweaking shouldn't even matter (it all verifies and uploads fine.)
Edit 3: This crashes:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
This DOES NOT:
if (0 == 1) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
This crashes:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX = 1;
}
}
}
This DOES NOT:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
pieceX = 1;
}
}
AND THIS DOES NOT:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
}
}
}
Edit, here's the full source code. I'm only a few hours into a black and white Dr Mario clone. I never write in this language, so.. potentially a bit sloppy. More of a random learning experiment in processing / video game hardware / arduino.
Since the issue seems to be erratic, I would guess you are corrupting your stack.
I am not sure which Arduino you are using and how many other variables you have defined.
The array you are creating is 8 * 47 * 2 = 752 bytes, the Arduino Uno has 2048 ram bytes for the stack and all of your variables.
Edit:
Can you temporary reduce the size of the array(maybe 4 * 10) to see if it stops crashing?
Another test you could do is to list the values before you modify them and verify they are all 0.
This definitely looks like you are running out of memory.
int board[8][47]
consumes 752 bytes of memory. In addition
TV.begin(NTSC,120,96);
will call
char TVout::begin(uint8_t mode, uint8_t x, uint8_t y) {
// check if x is divisable by 8
if ( !(x & 0xF8))
return 1;
x = x/8;
screen = (unsigned char*)malloc(x * y * sizeof(unsigned char));
which tries to allocate 1440 bytes of memory. 1440 + 752 == 2192 > 2048 == SRAM size of Arduino
So you are running out of memory.
Can you switch int board[8][47] from int to int8_t or uint8_t? This would reduce the memory consumption of the array by 2. However you would still be very tight on memory.
This definitely looks like you are running out of memory.
You might be able to use less memory.
It looks like any given board element is either a 0 or 1.
If I am wrong disregard the rest of my statement.
Else you "could" make an array like this.
char board [47];
first = 0b00000001; //binary mask, for binary and
second = 0b00000010;
third = 0b00000100;
...
Then to look up any bit you
if (board[33]&second == 0 ) \\you are testing what was called board[2][33]
This might help you out.

Resources