&& vs nested if statement

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, I've just started to learn AVR programming a few days ago following Patrick Hood_Daniel Youtube video series on Atmega32.

While following this particular video to learn how to toggle between the two LEDs (on pin PINB0 & PINB2) using a push button on PINB1,  I've ran into a problem when writing my code like this:

if ( bit_is_clear(PINB, 1) && (Pressed == 0) )
{
    PORTB ^= (1 << PINB0);
    PORTB ^= (1 << PINB2);
    Pressed = 1;
}

The problem is: one LED keeps turning on and the other keeps flashing on/off when I pressed the button. (Instead of toggling between two LEDs)

 

However, if I follow Daniel's code and write like this, then it'd work as expected:

if (bit_is_clear(PINB, 1))
{
    if (Pressed == 0) {
        PORTB ^= (1 << PINB0);
        PORTB ^= (1 << PINB2);
        Pressed = 1;
    }
}

My question is: what is the difference between using && operator and using nested if statement.

As far as I know, there are no differences between them. Can someone explain this to me? Thank you!

 

I've also attached my zipped project folder for anyone who's interested.

 

Attachment(s): 

This topic has a solution.
Last Edited: Mon. Aug 28, 2017 - 03:58 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The detail that makes the behaviour different is inside the line of code after the if statement, in your rar file.

 

When the first code starts executing it checks if bit is clear(your button(?)) and if pressed variable is 0, then it executes the if statement which makes the pressed variable 1, next time it it comes across the if statement which is false now since pressed is 1, now it executes the else part of the statement which makes pressed 0 again, this will make your led flash when it happens again.

 

The other code is constructed differently since it only test if button is pressed  which it is if you don't release it, and the else part of the code can never execute, which also means your inner if statement is only executed once, no matter how long you press the key.

Last Edited: Mon. Aug 28, 2017 - 02:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JoniS wrote:

The detail that makes the behaviour different is inside the line of code after the if statement, in your rar file.

 

When the first code starts executing it checks if bit is clear(your button(?)) and if pressed variable is 0, then it executes the if statement which makes the pressed variable 1, next time it it comes across the if statement which is false now since pressed is 1, now it executes the else part of the statement which makes pressed 0 again, this will make your led flash when it happens again.

 

The other code is constructed differently since it only test if button is pressed  which it is if you don't release it, and the else part of the code can never execute, which also means your inner if statement is only executed once, no matter how long you press the key.

 

Thank you very much for explaining this in such a detailed way! I understand the problem now :D