I was trying to use buttons to turn on and off an LED on my Arduino, but nothing was working. I set it up so that when there is no power being supplied to port 2, an LED will turn on. It's a very simple project, and I actually downloaded it from Vilros.com, so I am sure the project is correct. I decided to test it by completely unplugging port 2. When I unplug it, the light stays off for about 4 seconds. (By the way, the whole time it is running, the TX light is blinking on and off - is that bad?) After the 4 seconds, the LED light begins to blink, in time with the TX light. It blinks five times, staying lit on the fifth. The AREF light blinks in time with the LED.
I am a newbie. What is going on?
Edit: I ran it again after unplugging it, and this time the TX light stays on solid the whole time, but the same thing happens with the flashing LED.
Edit:
const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int ledPin = 13; // LED pin
int x = 0;
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int button1State, button2State; // variables to hold the pushbutton states
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
x++;
if (x%10000 ==0) {
x = 0;
if (button1State == LOW) {
Serial.println("Button 1 is LOW!");
}
else Serial.println("Button 1 is HIGH!");
if (button2State == LOW) {
Serial.println("Button 2 is LOW!");
}
else Serial.println("Button 2 is HIGH!");
}
if (((button1State == LOW) || (button2State == LOW)) // if we're pushing button 1 OR button 2
&& ! // AND we're NOT
((button1State == LOW) && (button2State == LOW))) // pushing button 1 AND button 2
// then...
{
digitalWrite(ledPin, HIGH); // turn the LED on
}
else
{
digitalWrite(ledPin, LOW); // turn the LED off
}
}
Schematic: