Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LED it's not blinking at the expected rate

Status
Not open for further replies.

robi10101298

New Member
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:

robi10101298

New Member
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.

Latest threads

EE World Online Articles

Loading
Top