Turn LED on and off

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

Hello, I am transitioning from assembly to C. Please don't take debouncing into consideration. All I want is for the LED to turn on when I press a button located on PB0 and for the LED to turn off when I press it again. I've been able to make the LED turn on while the button is pressed down and for it to turn off once the button is released. I don't want to have to hold the button down for the LED to be turned on. Note: 0 turn LED on.


#include 

int main(void) 
{    
	DDRB &= ~(1 << PINB0);
	PORTB |= 1 << PINB0;
	
	DDRA |= _BV(PORTA0);
	PORTA |= _BV(PORTA0);


while (1)
{
	if (bit_is_clear(PINB, 0))
	{
					
		PORTA=0B11111110;
	}
	else
	{
		PORTA=0B11111111;
	}
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Please don't take debouncing into consideration.
But to do what you want you need to take debouncing into consideration. Without it, the result of any press/release would be random. At the very least you need a delay when any transition is detected.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to add the current state of the LED to your logic.

 

In consideration of others, please RTFM!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try this:

//pseudocode

while(1)
{
  if buton pressed
  {    
    toggle Led
    wait 300 ms
  }  
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Visovian wrote:
Try this:

//pseudocode

while(1)
{
  if buton pressed
  {    
    toggle Led
    wait 300 ms
  }  
}

Works perfectly. Thanks.