LED it's not blinking at the expected rate

Status
Not open for further replies.
Just use a for loop with delays.

Mike.
This is what I've done, but it's blinking 3 times, than 4 times, 3 times than 4 times....
C:
if(SW1 == 0)        // pressed
        {
        for(i=0; i<3; i++){
                 if(OCR1AH == 0x4C) 
                    {TCNT1H=0; TCNT1L=0; OCR1AH = 0x13; OCR1AL = 0x10;}    //0x1310
                else if(OCR1AH == 0x13)
                    {TCNT1H=0; TCNT1L=0; OCR1AH = 0x2F; OCR1AL = 0xA9;}    //0x2fa9
                else
                    {TCNT1H=0; TCNT1L=0; OCR1AH = 0x4C; OCR1AL = 0x40;}    //0x4C40
                
                 } 
                 delay_ms(1000);
        }
 
Last edited:
I've managed to do it in a much simpler way, thanks for your help!
C:
int state = 0;
int delay[] = { 150, 250, 500 };

void handle_press() {
 
  /* blink three times at the rate specified for the current state */
  for (int i = 0; i < 3; ++i) {
    LED1 = 1;
    delay_ms(delay[state]);
    LED1 = 0;
    delay_ms(delay[state]);
  }
 
  /* then wait another second */
  delay_ms(1000);
 
  /* update state */
  state = (state + 1);
  if (state == 3) {
    state = 0;
  }
}

void loop() {
  while (true) {
    if (SW1 == 0) {
      handle_press();
    }
  }
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…