0

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();
}

}

6
  • Just polling 4 non-interrupt pins should be sufficient. You don't need that much of a response time for user interaction (up to 100ms should be sufficient and achievable depending on the implementation on the snake code).
    – Rev
    May 18, 2015 at 7:10
  • Are you sure you want to call init_interrupts();all the time in the while? As I suggested I would not use interrupts for now at all. Just poll your inputs in the while() similar to what you do in the ISR. Furthermore it is bad practice to execute main functionality in the ISR (like calling up(), down() and so on) because you normally want to keep ISR execution time as short as possible. Start simple. Try to understand what you do, don't rely on example code without understanding what it does.
    – Rev
    May 18, 2015 at 8:29
  • Btw: What are those AND gates for? They just seem to indicate if all 4 buttons are pressed which could easily be done by software?!
    – Rev
    May 18, 2015 at 8:30
  • @Rev1.0 I'm fairly new to Avr so I followed mentioned tutorials :( AND gates used to detect which button pushed and enable our interrupts then in our ISR check which one pushed. What do you recommend to do to handle this ?
    – Amir
    May 18, 2015 at 8:43
  • Ah, OK you don't have interrupt support for the other button IO pins so you just use INT0 for all of them. You always end up in the first condition because you use "active low" logic for your buttons. When not pressed your button inputs are all HIGH. So just invert your condition and check if the respective pin is LOW (pressed).
    – Rev
    May 18, 2015 at 9:02

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.