School project for Arduino UNO R3

Hello Guys,

I need help with school project.
I have to write a program to make the LED turn on when I'm enter an odd number and turn off when I enter an even number. The number entered can be up to 2 digits. I wrote the code but is not working can you help the code

void setup() {
  
  
// put your setup code here, to run once:
int led=13;
int value = 0;
  
Serial.begin(9600);
pinMode (led,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println ("EET 439");
delay (10);
if(value == '1')   ``
{
  digitalWrite(led,HIGH);
  Serial.println ("LED is ON");
}
if(value == '0')
{
 digitalWrite(led,LOW);
 Serial.println("LED is OFF");
}
}
}

I have error value is not declared in the scope

Thank you for any help

  • This variable can only be used in setup( )

  • You need to read your numbers from the serial port, see the example sketches in the IDE.

1. How are you entering the value -- for example: 23?

2. Do you know about the Serial Monitor asscociated with Arduino IDE?

3. You can enter your vlaue from the InputBox of the Serial Monitor (Fig-1).

4. After that you receive the value at the UNO side, retrive he original decimal number, and then compare it with "23" using strcmp() function.

5. If the two string are equan, you turn on onBoard LED (L) of UNO Board.


Figure-1:

Hello zyraf30

Take a view to this code example:

// declare and initialize variables
const int Led = 13;
void setup()
{
  Serial.begin(9600);
  pinMode (Led, OUTPUT);
  Serial.println ("EET 439");
}
void loop()
{
  if (Serial.available())  // new serial characters available = ?
  {
    char inChar = Serial.read();  // read incoming characters
    switch (inChar) // filter incoming character
    {
      case '0' ...'9':    // is a number been received?
        Serial.print("received number: ");
        Serial.println(inChar);
        digitalWrite (Led, inChar & 1); // mask lower bit of number - this bit indicates odd or even
        break;
      default:
        // all other characters will be ignored
        break;
    }
  }
}

hth