Go Down

Topic: My First Program (Read 718 times) previous topic - next topic

danofri

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:
Code: [Select]

// 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.

PaulS

Are you sure the green LED isn't backwards? There is nothing in the code that would prevent the green LED from coming on, so it must be a hardware problem. You are using current limiting resistors with the LEDs, right?
The art of getting good answers lies in asking good questions.

danofri

Actually, i'm not using any resistors...

I tried to switch to code, means to the make the green led turned on when the button is not pushed and the red to turn on when I push the button.

I had the same problem, but with the red led...

Many thanks!!
Danofri.

PaulS

You should be using resistors. The LED has nothing to limit current, so it'll draw more than the Arduino can provide, and burn out a pin.

I just noticed something...
Quote
I want my code to tun on green led when I push the on-boad button

Are you referring to the button on the Arduino? If so, that button HAS a function. It resets the Arduino.
The art of getting good answers lies in asking good questions.

danofri

silly me...  ;D


Many many thanks, and sorry for the mistake...

Eight

Sorry dano,

We all make mistakes with platforms/devices when starting out... but this one is a belter!!! :D

Go Up