Hi everyone, I'm having a problem with my first sketch I made from scratch. I based it on the button lighting the LED demo from the 'Getting Started with Arduino' from Make. I was trying to alternate lit LEDs based on a cycle counter rather then using the "delay()" function. I have both LEDs on a different ground and different pins but when I press the button they both turn on. Here is the sketch
const int LED1_PIN = 13;
const int LED2_PIN = 12;
const int BUTTON_PIN = 7;
const int SWAP_DELAY = 1000;
int buttonState = 0;
int lastButtonState = 0;
int ledBlinking = 0;
int blinkTimer = 0;
int currentLitLED_pin = LED1_PIN;
int currentDimLED_pin = LED2_PIN;
void setup() {
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
CheckButton();
BlinkLED();
}
void CheckButton()
{
buttonState = digitalRead(BUTTON_PIN);
if(buttonState == HIGH && lastButtonState == LOW)
{
ledBlinking = 1 - ledBlinking;
delay(10);
}
lastButtonState = buttonState;
}
void BlinkLED()
{
if(ledBlinking == 1)
{
blinkTimer += 1;
if(blinkTimer >= SWAP_DELAY)
{
if(currentLitLED_pin == LED1_PIN)
{
currentLitLED_pin = LED2_PIN;
currentDimLED_pin = LED1_PIN;
}
else
{
currentLitLED_pin = LED1_PIN;
currentDimLED_pin = LED2_PIN;
}
blinkTimer = 0;
}
digitalWrite(currentDimLED_pin, LOW);
digitalWrite(currentLitLED_pin, HIGH);
}
else
{
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
}
}
Any ideas what I might be doing wrong?
Thanks,
Jacob