Need help troubleshooting error

Hi, I am new to the Arduino.
I am using 2 LED's(Red and Green) and a button.

What i am trying to do is on first button press, Red Led starts to blink.
On second button press, Green Led Lights up.
After that Green Led and Red Led will keep alternating HIGH and LOW.

I managed to compile the code:

const int greenLed = 12;
const int redLed = 13;
const int pushButtonPin = 2;

int pushButtonCount = 0;
int pushButtonState = 0;
int lastButtonState = 0;

void setup()
{
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(pushButtonPin, INPUT);
}

void loop()
{
  pushButtonState = digitalRead(pushButtonPin);
 
  if(pushButtonState != lastButtonState) {
    if(pushButtonState == HIGH) {
      pushButtonCount++;
      updateOutput(pushButtonCount);
    }
  }
  
  lastButtonState = pushButtonState;
}

void updateOutput(int buttonPushCount) {
  if(buttonPushCount == 1) {
    
    digitalWrite(redLed, HIGH);
    
  } else if(buttonPushCount == 2)
  {
    digitalWrite(redLed, HIGH);
    delay(100);
    digitalWrite(redLed, LOW);
    delay(100);
    digitalWrite(greenLed, HIGH);
    delay(100);
    digitalWrite(greenLed, LOW);
    delay(100);
    digitalWrite(redLed, HIGH);
    delay(100);
    digitalWrite(redLed, LOW);
    delay(100);
    digitalWrite(greenLed, HIGH);
    delay(100);
    digitalWrite(greenLed, LOW);
    delay(100);
    digitalWrite(redLed, HIGH);
    delay(100);
    digitalWrite(redLed, LOW);
    delay(100);
    digitalWrite(greenLed, HIGH);
    delay(100);
    digitalWrite(greenLed, LOW);
    delay(100);
  }
}

There are two problems:

  1. Upon uploading the code, Red Led Lights up even though the button is not pressed at all
  2. Upon the button press, the console starts printing out error messages:
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
	at gnu.io.RXTXPort.nativeavailable(Native Method)
	at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
	at processing.app.Serial.serialEvent(Serial.java:258)
	at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
	at gnu.io.RXTXPort.eventLoop(Native Method)
	at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

Help is appreciated

Am I blind, or do I not see anywhere at all where you ever turn OFF any of the LEDs?

Also, how is your button wired up?

Sorry about the code
About the wiring, i have uploaded the picture of the whole setup.

snapshot.jpg

Can we have more detail of the button area please?

Maybe draw out a schematic on paper and photograph that too :wink:

Focus would be good

It looks to me like you have your button signal connected direct to +5V, and the other side of the switch connected to GND. Press the button, and you short circuit your power supply (USB) and the computer shuts off the power to prevent damage.

Be glad you're using USB and not an external power supply - your voltage regulator may have gone into meltdown if you were.

Updated Circuit diagram and pics, sorry for bad quality :frowning:

picanom-picture-02_2013-06-13.jpg

Yep, it is as I thought. The input is permanantly high, until you press the button, when the whole arduino dies due to a short circuit.

I suggest you read up on how to wire up a button

Ok thanks, should i leave the wiring of LED the same?

LEDs look fine.

Ok thanks alot, I will try it out when i get time and let u know.

Thanks a lot!