After 16 loops the program crashes shouldn't the variables that i use in GetSpeed be automatically removed once the function has run?
The only other possible thing i could see is that the micros(); thing goes back to zero but that should not happen until after 70 minutes right? (Is it possible that it has to to with the fact that i am not using unsigned long?)
And 17 loops takes 17 minutes.
The program reads a binary input that comes from a sensor.
here is my code
/*
Current problems:
-crashes after 16 times
*/
//add 100 to the actual baud rate
#define BaudRate 1200
void setup()
{
Serial.begin(115200);
Serial.println("started");
}
byte xchange(byte bt)
{
if(bt == 1)
return 0;
else
return 1;
}
int GetSpeed(long maxtime)
{
int State = digitalRead(3);
int OldState = State;
int ii = 0;
byte ar[73];
long time = micros();
long t2 = 999999999;
while(ii<73)
{
State = digitalRead(4);
if(OldState != State)//On change
{
t2 = micros()+BaudRate+150;//add 150 extra microsecunds
ar[ii] = State;
OldState = State;
ii++;
}
if(micros()>t2)
{
t2 = micros()+BaudRate;
ar[ii] = State;
ii++;
}
}
//convert
byte kl = 0;
byte tr =false;
while(64>kl)
{
if(kl == 58)// && ar[60] == 1
{
tr = true;
}
if(kl == 40 && ar[39] == 1)
kl+=2;
ar[kl] = xchange(ar[kl]);
kl++;
ar[kl] = xchange(ar[kl]);
kl+=3;
}
kl = 0;
while(73>kl)
{
if(ar[kl] == 1)
Serial.print('1');
else
Serial.print('0');
kl++;
}
return ii;
}
void loop()
{
GetSpeed(33000);
Serial.print('\n');
GetSpeed(33000);
Serial.print('\n');
GetSpeed(33000);
Serial.print('\n');
Serial.print('\n');
}
Your code is difficult to decipher, but in this block:
byte ar[73];
long time = micros();
long t2 = 999999999;
while(ii<73)
{
State = digitalRead(4);
if(OldState != State)//On change
{
t2 = micros()+BaudRate+150;//add 150 extra microsecunds
ar[ii] = State;
OldState = State;
ii++;
}
it looks like "ii" can be incremented to 73. You then have another: ar[ii] = State; in the following block, which means you are assigning a value to ar[73], which is larger than the array you have declared. ar[73] reserves space for ar[0]... ar[72], so you may be writing over something else in memory.
After 16 loops the program crashes shouldn't the variables that i use in GetSpeed be automatically removed once the function has run?
Yes, local variables are automatically removed once the function is exited. Now, trying to see whether your program really crashed... could you reduce your Serial baud rate to 9600 (or even lower rate) to begin with? I have noticed similar problems when the baud rate is way too high. What happens then is that the monitor can't keep up with the pace at which the output is produced so that it seems that the program had stopped responding.
I'd say reduce the baud rate in your Serial.begin and post back your result so we can determine the issue further.
What happens then is that the monitor can't keep up with the pace at which the output is produced so that it seems that the program had stopped responding.
Nah it's not that. The program randomly starts to output tons of zeros and sometimes 1's. Also 9600 is also to slow i used it before. It takes to long time for the program to write all digits in 9600 mode.
it looks like "ii" can be incremented to 73. You then have another:
Actually it impossible for the later if statement to activate until about 1300 micro s after the last change of ar so thats not it.
micros will be bigger than t2 forever after about 17 minutes. Not to mention, before you write to the bad position, you're probably triggering logic errors too =)