Hello,
I'm trying to understand why my code doesnt work.
I want my code to tun on green led when I push the on-boad button and turn on the red red led when the button is not pushed.
From some reason, although the red led is turned off when I press the button, the green led doesnt turned on.
This is the code I wrote:
// set pin numbers:
const int buttonPin = 13; // the number of the pushbutton pin
const int ledPinRed = 3; // the number of the RED LED pin
const int ledPinGreen = 2; // the number of the GREEN LED pin
int buttonState = 0;
void setup() {
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGreen, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn Green LED on:
digitalWrite(ledPinGreen, HIGH);
digitalWrite(ledPinRed, LOW);
}
else
{
// turn RED LED on:
digitalWrite(ledPinRed, HIGH);
digitalWrite(ledPinGreen, LOW);
}
}
Can you please let me know what I did wrong?
Many thanks!!
Danofri.