Search code examples
avratmegaatmega16

How handle directions with interrupt in Proteus


I want to implement Snake game with Atmega16 I following this tutorial. My problem is I couldnt handle the KeyPad ! I found another tutorial (here) about one Button with interrupt But in Snake we need 4 button for our directions And I dont know how to handle this in Proteus ? we have 3 external interrupt and 4 button I dont know what to do :(

Any help really appreciated

EDITED :

This is my last try but now it's not detect directions and always get into first condition of if-statement in my Interrupt and not check other conditions

enter image description here

Main :

void main()

{

TCCR0=0X01;
DDRC=0XFF;
DDRB=0XFF;
DDRD|=(1<<PD0)|(1<<PD1)|(1<<PD7); 
DDRD&=~((1<<PD2)|(1<<PD3)|(1<<PD4)|(1<<PD5)|(1<<PD6));
DDRA=0xFF;

pos=1;
position();
right();

while(1)
{
    no_inp();
    init_interrupts();
}

}

And here my interrupt :

ISR (INT0_vect){
sss=0;
if((PIND&(1<<PIND3))&& status!=3)
{
    right();
    status=1;
}
else if((PIND&(1<<PIND4))&& status!=4)
{   
    up();
    status=2;
}
else if((PIND&(1<<PIND5))&& status!=1)
{
    left();
    status=3;
}
else if((PIND&(1<<PIND6))&& status!=2)
{
    down();
    status=4;
}
else
{
    no_inp();
}

}


Solution

  • You always end up in the first condition because you use "active low" logic for your buttons, but you check if the bit is HIGH. However, when not pressed, your button inputs are pulled HIGH. So just invert your if-condition and check if the respective pin is LOW (actually pressed) instead.