First custom programe

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

I have both LEDs on a different ground and different pins but when I press the button they both turn on.

Can you elaborate on that? Normally the ground is shared.

Hello jacoba,
I see that the led swap is done every 1000 (=SWAP_DELAY) calls of the BlinkLED function. But this is called inside loop() continously, without any time check, so the swap occurs at very high frequency (the time needed to complete 1000 loops).

I had them on a shared ground but I was unsure if that had any effect (I was 90% sure it didn't but I tried anyway)

ea123:
Hello jacoba,
I see that the led swap is done every 1000 (=SWAP_DELAY) calls of the BlinkLED function. But this is called inside loop() continously, without any time check, so the swap occurs at very high frequency (the time needed to complete 1000 loops).

I tried making it 10 million too but they both still came on, would that also be too fast? This is my first time working with the ADK so I still don't know all of the libraries, Is there a simple time keeping object that I can use to get a delta rather then the cycle based testing?

Thanks again!

I tried making it 10 million too

But not in an "int", right?

AWOL:

I tried making it 10 million too

But not in an "int", right?

Ahh yes that is an oversight... I changed it to an unsigned long and set it to 500,000 works like I wanted it to, thanks a lot!