My simple for loop is not working!

This in my code:

int myNumber;
int j;
int bt=100;
String msg="Enter your number:";
void setup() {
pinMode(12,OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(msg);
while(Serial.available()==0){
}
myNumber=Serial.parseInt();
for(j=1,j<=myNumber,j=j+2){
digitalWrite(12,HIGH);
delay(bt);
digitalWrite(12,LOW);
delay(bt);
}
}

When i try to upload it on my Arduino it gives me multiple errors saying that semicolons are expected to be like this: for(j=1,j<=myNumber,j=j+2);{

This is the error:

C:\Users\diego\AppData\Local\Temp\.arduinoIDE-unsaved20221010-6280-18o8n1r.x50u\BareMinimum\BareMinimum.ino: In function 'void loop()':
C:\Users\diego\AppData\Local\Temp\.arduinoIDE-unsaved20221010-6280-18o8n1r.x50u\BareMinimum\BareMinimum.ino:16:26: error: expected ';' before ')' token
 for(j=1,j<=myNumber,j=j+2);{
                          ^
C:\Users\diego\AppData\Local\Temp\.arduinoIDE-unsaved20221010-6280-18o8n1r.x50u\BareMinimum\BareMinimum.ino:16:28: error: expected primary-expression before '{' token
 for(j=1,j<=myNumber,j=j+2);{
                            ^
C:\Users\diego\AppData\Local\Temp\.arduinoIDE-unsaved20221010-6280-18o8n1r.x50u\BareMinimum\BareMinimum.ino:16:28: error: expected ';' before '{' token
C:\Users\diego\AppData\Local\Temp\.arduinoIDE-unsaved20221010-6280-18o8n1r.x50u\BareMinimum\BareMinimum.ino:16:28: error: expected primary-expression before '{' token
C:\Users\diego\AppData\Local\Temp\.arduinoIDE-unsaved20221010-6280-18o8n1r.x50u\BareMinimum\BareMinimum.ino:16:28: error: expected ')' before '{' token

exit status 1

Compilation error: expected ';' before ')' token

Thanks for helping

Do you disagree?
Open any C book and look at the for loop syntax

Shouldn't it be just like this?`

for (initialization; condition; increment) {
  // statement(s);
}`

yes.
Why did you use commas instead of semicolons, if you know this?

No, i didn't knew it, i was watching a tutorial where it uses commas, and it works.
But i still have some problems, when i give the input form the serial monitor it makes the LED blink 2 times, no matter what the input is, how can i fix this?

I do not believe
Please show the link to that tutorial

[Arduino Tutorial 18: Reading Numbers from the Serial Monitor - YouTube]

If you follow a for loop with a semicolon ; then that stops the loop immediately.

But i didn't, right? Excusme if i'm not understanding much but i've started learning just one week ago.

Nobody's going to watch half an hour to find the part that we need to watch. Can you give time stamp to where he shows the code with commas?

15:52

i just found it at time 15:26

And just after that he says it's wrong, use semi colons....

no, looks carefully:

newbies are always in a hurry and don't watch to the end :slight_smile:

Yes, then i've noticed it and i've deleted the reply.
Now i've fixed everything, thank you guys!

The video might actually be easier to follow if the guy left out the theatricals....

This is why it blinked twice after you changed the commas to semicolons. The code became:

  for(j=1; j<=myNumber; j=j+2) ;

  {
    digitalWrite(12,HIGH);
    delay(bt);
    digitalWrite(12,LOW);
    delay(bt);
  }

That extra ';' between the 'for' and the body of the 'for' became the only contents of the loop. The loop did nothing but count. After the for loop it blinked once. Then loop() repeated and the "wait for a character" line:
while(Serial.available()==0){}
saw that there were line-ending characters in the buffer so it dropped through to the
myNumber=Serial.parseInt();
Since there were no digits in the buffer, the .parseInt() returned 0. The for-loop counted from 1 to 0, doing nothing else, and the blink code executed again (2nd blink). Now the input buffer is empty so the next time through loop() it waits for input.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.