0

Currently practicing my programming knowledge So,am using atmel studio to program atmega328p I have a "for" statement that flashes an LED(led1) 5 times when button1 is pressed. Then another "if" statement to control led2 via button2. But while simulating the circuit on Proteus. Whenever led1 is flashing, button 2 will not be able to light up led2 until after the flash. So, it appear as if the "for" statement is blocking the other program. Any help will be appreciated

while (1)
{
    for (int j=1; j<=5; j++){
        If (!(PINB &(1 <<button1))){
            PORTB |= (1<<led1);
            _delay_ms (100);
            PORTB &=~(1<<led1);
            _delay_ms (100);
        }
        else {
            PORTB &=~(1<<led1);
        }

        if (!(PINB &(1 <<button2))){
            PORTB |= (1<<led2);
        }
        else {
            PORTB &=~(1<<led2);
        }
    }
}
  • 3
    The Arduino can only do 1 thing at a time. That includes waiting. Look at the BlinkWithoutDelay example. – Majenko May 14 at 16:19
  • 1
    Your code runs one line at a time. If you have a for loop that takes a long time to get through then yes that blocks the rest of your program. Let the loop function handle the looping and use if statements to decide what sections of loop to run or not run at any given time. – Delta_G May 14 at 16:46
  • 3
    Did you read the Blink Without Delay tutorial? – Edgar Bonet May 14 at 16:59
  • Thank you for the hint... Could you kindly suggest me how to go about using if else statement to achieve my sim in this context. – Quadrry May 16 at 6:32
  • Note that I want the led to blink for a particular time (5 times) so am confused on using "if" to achieve this. – Quadrry May 16 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.