Coding help.

image does anyone know what is wrong

Misplaced semicolons.

6 Likes

More specifically, the semicolons on lines 15 and 23 should not be there.

4 Likes

image Now it says this sorry I am new to coding and my teacher can’t even help thanks for the help

Read up on If then else and if then elseif

In your case else can not have another condition so it should not be else (…) {. } but else {. } or it is else if (…) {. }

See here: Conditions - Learn C - Free Interactive C Tutorial for example

3 Likes

Hi thanks for the help so far it means the world but for some reason my servos just stay in the same spot when I adjust the light sensor is something wrong with my code or the program if you can help that would be great image

You have defined lightSensor as A5, which is just a pin number on your Arduino board.

To get value from the sensor you will need to use analogRead() function like this:

int val = analogRead(A5);

https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

2 Likes

Thanks for help well I just put it at the very top avoid loop or what I put it in front of the if and else if statement both places. Thanks

You can put it at the top of the loop() function but then there will be some delay before it is used in the first if() statement.

I don’t know what you are trying to do in your code, but generally, I prefer to take in all the inputs values on top of the loop(), then do any servo or motor commands and then have one delay(20); at the end of the loop().

If you need to wait more than 20 msec between motor commands you can try to learn how to use global counters and timers to skip several loops. But that is more advanced.

Ok thanks I just need one more thing I want a else if that if it is between two light sensor values it does some thing different how would I set up. Thanks

else does not have a comparison operator. It is simply the action to take if the if comparison fails.

If example

if ( x > 10) {
  //Do this action
}

If / Else example

if ( x > 10) {
    //Do this action
} else {
    //Do the default action 
}

Multiple If Conditions

if (x > 10) {
    //Do the first action
} else if (y > 10) {
    //Do the second action
} else {
    //Do the default action 
}
1 Like

Thank you so much I never thought of that. I think that’s all I need thanks for all the help. Hope you have a good rest of you day.

1 Like