Go Down

Topic: can't get in for loop (Read 780 times) previous topic - next topic

osfd

Hi there, for some weird reason, I cant' get into the for loop in the following code

Code: [Select]

int ledPin =  13;    // LED connected to digital pin 13
char c;              // receiver bit
int d1, d2, d;        // variables
char dest[2];        // destination
int length;
byte dataBuffer[50];


// The setup() method runs once, when the sketch starts
void setup(){
  Serial.begin(115200); 
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);     
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop(){ 
  // check if data available
while(Serial.available() > 0 && c != byte(4)){

c = Serial.read();

if(c == 'S'){
Serial.println("\n\nsize to be read here !");
int n = 0;
for(int i=2; i=0; i--){
Serial.println(i);
c = Serial.read();
Serial.print(c);
}
Serial.println("Size is");
Serial.println(n);
}

else{
Serial.print(c);
if(c == '1'){
digitalWrite(ledPin,HIGH);
delay(1);}
if(c == '0'){
digitalWrite(ledPin,LOW);
delay(1);}
}
}

if(c == byte(4)){
Serial.println("\nTransmission Done");}
}


I know I enter the first if condition since I print on the serial monitor Serial.println("\n\nsize to be read here !");
Though, for some reason I never get in my for loop...

The purpose of this is to read three bytes containing the size of the frame to be sent (no in this code) if the byte read is 'S'...

Thank you for your help,


Olivier

PaulS

Code: [Select]
for(int i=2; i=0; i--)
The middle part of the for statement (i=0) is the continue condition. When the initial value is 2, the continue condition is false right off the bat, so the loop never executes. Looks to me like you want to have i>2 or i>=2 there.

Also, don't go reading from the serial port without checking that there is anything to read.

mmcp42

you'r not setting c to anything before you test it
generally a bad plan!
there are only 10 types of people
them that understands binary
and them that doesn't

PaulS

Along the same line, how are you sending a 4 to the Arduino (not a '4', mind you...)?

Go Up