dayago
November 10, 2022, 9:44am
1
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
b707
November 10, 2022, 9:49am
2
Do you disagree?
Open any C book and look at the for loop syntax
dayago
November 10, 2022, 9:50am
3
Shouldn't it be just like this?`
for (initialization; condition; increment) {
// statement(s);
}`
b707
November 10, 2022, 9:52am
4
yes.
Why did you use commas instead of semicolons, if you know this?
dayago
November 10, 2022, 9:55am
5
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?
b707
November 10, 2022, 9:56am
6
I do not believe
Please show the link to that tutorial
dayago
November 10, 2022, 9:57am
7
If you follow a for loop with a semicolon ; then that stops the loop immediately.
dayago
November 10, 2022, 10:00am
9
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?
b707
November 10, 2022, 10:02am
12
i just found it at time 15:26
And just after that he says it's wrong, use semi colons....
b707
November 10, 2022, 10:08am
15
dayago:
then he uploads it and gets no errors
no, looks carefully:
newbies are always in a hurry and don't watch to the end
dayago
November 10, 2022, 10:10am
16
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.
system
Closed
May 9, 2023, 2:43pm
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.