Tried to upload board, don't understand these error messages.

Hi. I tried to upload this code

int input;


void setup() {


  void loop();




  Serial.begin(34800);
  if (Serial.available())
    (input) == Serial.read();
  if (input = 1)
    digitalWrite(1, HIGH);
  digitalWrite(4, HIGH);




  if (input = 2)
    digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);

}

For reasons I don't understand, the IDE gave me these error messages: C:\Users\Bob\AppData\Local\Temp\ccRc0pfg.ltrans0.ltrans.o: In function `main':

C:\Users\Bob\Documents\ArduinoData\packages\arduino\hardware\avr\1.6.20\cores\arduino/main.cpp:46: undefined reference to `loop'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

I'm not sure what most of this means. Can anyone help?

Why is your loop part of setup?

Aka, where is the end bracket for setup? :wink:

When you first load the IDE, it comes up with a skeleton framework for a program. Compare that to what you posted here and you'll see the difference.

Should look like this:

int input;

void setup() {
  Serial.begin(34800);
}

void loop() {
  if (Serial.available()) input = Serial.read();
  if (input == 1) {
    digitalWrite(1, HIGH);
    digitalWrite(4, HIGH);
  }

  if (input == 2) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
  }
}

Equality tests use '=='
Put curly brackets around instruction blocks (I supposed that the 2 digitalWrite were in the same block each time, which may not be the case)

And now we're at the real code:

I doubt the OP want's to check for 1 because I assume he uses the Serial monitor. So that would be '1'.

Because 'input' is only used in loop, there is no need for it to be global.

Note pin 1, 2, 3 and for are not set as output. So a digitalWrite() to HIGH will only turn on the internal pull up.

Maybe obvious, maybe not, not calling digitalWrite(pin, HIGH) will not magically turn an output LOW. You need to explicitly call digitalWrite(pin, LOW) for that to happen.

Less obvious (and not a problem here but might be when extended). The if() of the last character is called over and over if there is no new character in serial. No big deal because you only do a digitalWrite() call but get's weird if you only want stuff to happen once. Especially because it's probably marked by the fact you have set line ending to newline (and/or carriage return) in the Serial Monitor thus having a newline (or carriage return) char after every character.

Last but not least, take the help the compiler gives you. Use logical variable names (not just short ones!) to help you remember what everything is.

digitalWrite(LedPin, HIGH);
//is way more readable than
digitalWrite(2, HIGH);