Hello I am new. I have an error and I can’t find it

Hello, I’m new, my code doesn’t compile.
If someone could help me, I’d be very grateful.
I’m trying to do a counter, but when you’re compiling an error occurs, a comma is missing, but I can’t see where, I’ve checked it several times and I definitely don’t see it.
What’s wrong?

This is the code:

#define RED
#define GREEN
int x = 0;
#define button

void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
}

void loop() {
if(digitalRead(button)==true){
x++;
delay(100);
}

while(x == 1){
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
}

while(x == 2){
digitalWrite(GREEN, HIGH);
digitalWrite(RED, HIGH);
}

while(x == 3){
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
}

else if(x == 4){
x=0;
}
}

Hello
use code tag </> and post your sketch to see what´s going wrong.
Did you study the error messages provide by the compiler ?

This else appears to be orphaned from its preceding if statement.

The while() loops will get ‘stuck’ in each value of ‘x’
Try something like this…


if (x == 1) {
  digitalWrite(GREEN, HIGH);
  digitalWrite(RED, LOW);
}

else if (x == 2) {
  digitalWrite(GREEN, HIGH);
  digitalWrite(RED, HIGH);
}

else if (x == 3) {
  digitalWrite(GREEN, LOW);
  digitalWrite(RED, HIGH);
}

else {
  x=0;
}

You may prefer to try…

switch (x) {
  case 1:
  case 2:
  case 3:
  default:
}

Remember to
break;
from each case…

Later, Your four conditions could be turned into a function to make the code more compact

1 Like

ok, I’ll try, thank you very much.

#define NAME VALUE

you are missing values

As what?

@lastchancename is this coding workable if i have multiple void setup() or void loop() ?
thanks

You can’t have multiples of the same named functions…
The example I suggested is to simplify & clarify your code…

The use of while() the way you have done will trap the code whenever x equals any of those nominated values.

Read it through in your mind the way it’s shown… there’s no reason for the while() to ever end, because x never changes inside the while {…}

Hello again, I changed what @lastchancename said and in that respect it works better, but it keeps giving me the same error:
expected primary-expression before ‘,’ token
but I can’t see where.

my new code:

#define RED
#define GREEN
int valor = 0;
#define button

void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
}

void loop() {
 if(digitalRead(button)==true){
  valor++;
  delay(100);
 }
  
  if(valor == 1){
  digitalWrite(GREEN, HIGH);
  digitalWrite(RED, LOW);
  } 
  
  else if(valor == 2){
  digitalWrite(GREEN, HIGH);
  digitalWrite(RED, HIGH);  
  }
  
  else if(valor == 3){
  digitalWrite(GREEN, LOW);
  digitalWrite(RED, HIGH);
   }
   
  else { 
   valor=0;
   }
}

You don’t define any values for RED or GREEN, so when the compiler tries to replace them in the code, you end up with lines like

pinMode( , OUTPUT):
// and
digitalWrite( , HIGH);

Hence the complaint about there being something missing before the comma. Your first two lines of code need values after the key words as @JCA34F said above. ETA and the fourth line, where you define what pin the button is connected to.

1 Like


same for button
It’s #defined as null (nothing), so whenever you use it, nothing is inserted or processed.

pinMode(button, INPUT);

// becomes pinMode( , INPUT);

and so on down the code…

1 Like

Thank you all very much, it’s already fixed and it works. It was that I got lost and couldn’t see the mistake.

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