dustin02rsx
- May 18, 2011
- 36
- Joined
- May 18, 2011
- Messages
- 36
So this has been kind of stumping me the past couple days. Im doing a project in class where a subpart is going to be run off a push button that triggers 3 functions and then resets.
In class we use ATmega16 microcontrollers in CodevisionAVR IDE
ive also tried doing it on an arduino board at home with no success.
here is what i have tried, it enters the first while loop but never exits (ignore the actual command codes inside the if statements as they were replaced with simple code just to check the push button would work, which it didnt)
Ive been stumped on this, ive tried a series of nested if/else statements and while loops. I know i can do this with a for loop but im not positive how to implement the push button into a for loop.
any advice?
thanks
In class we use ATmega16 microcontrollers in CodevisionAVR IDE
ive also tried doing it on an arduino board at home with no success.
Code:
when a push button is pressed once
{
execute command
}
when the same push button is pressed a second time
{
execute another command
}
when the same push button is pressed a third time
{
execute a third command
}
when the same push button is pressed a fourth time
{
reset/off
}
here is what i have tried, it enters the first while loop but never exits (ignore the actual command codes inside the if statements as they were replaced with simple code just to check the push button would work, which it didnt)
Code:
#include <mega16.h>
#include <delay.h>
unsigned char i=5;
void main (void)
{
DDRD = 0xFF; // port d output
DDRA = 0x00; // port a input
while(1)
{
if((PINA & 0x80)!= 0) //if pb is pressed once
{ //falls into while loop
i=1;
delay_ms(5); //to prevent multiple presses
}
while(i==1)
{
if(i==1)
{
OCR1A=55; //low setting PWM to portd for tac light
}
else if((PINA & 0x80)!= 0) //if pb pressed twice
{ //falls into second while loop
i=2;
delay_ms(5);
}
}
while(i==2)
{
if(i==2)
{
OCR1A=255; //high PWM setting
}
else if((PINA & 0x80)!=0)
{
i=3;
delay_ms(5);
}
}
while(i==3)
{
if(i==3)
{
PORTD=PORTD|0x20;
delay_ms(2); //strobe
PORTD=PORTD & 0xDF;
delay_ms(2);
}
else if((PINA & 0x80)!=0)
{
i=4;
delay_ms(5);
}
}
}
}
Ive been stumped on this, ive tried a series of nested if/else statements and while loops. I know i can do this with a for loop but im not positive how to implement the push button into a for loop.
any advice?
thanks
Last edited: