I'm fairly new to C, and im writing code for an Arduino project which is essentially an led roulette game. The below code is from a section for a button and the leds in the project. What I'm doing is holding down the button and cycling through the leds, and when I release the button I want it to take the led or port information from the while
loop at the time of release and go through the proceeding code.
I'm stuck at the if
statement below the while
loop. It always goes to the first else if
and I'm really struggling with trying to get it to work. I know that the logical operators in the if
statement below the while
loop are most likely incorrect but I don't know what else I could do.
I've tried assigning variables in the loop such as b=1, b=2, and so on and calling it in the if statement, but it didn't work and I also have a good feeling that it was probably a bad idea to do that.
Any help would be appreciated
if (a == 1) {
if (BIT_IS_SET(PINC,1)) {
a = 2;
}
while (BIT_IS_SET(PINC,1)) {
PORTD = 0b01000000;
_delay_ms(DEBOUNCE_MS);
PORTD = 0b00100000;
_delay_ms(DEBOUNCE_MS);
PORTD = 0b00010000;
_delay_ms(DEBOUNCE_MS);
PORTD = 0b00001000;
_delay_ms(DEBOUNCE_MS);
PORTD = 0b00000100;
_delay_ms(DEBOUNCE_MS);
PORTD = 0b00000010;
_delay_ms(DEBOUNCE_MS);
PORTD = 0b00000001;
_delay_ms(DEBOUNCE_MS);
}
if (~(BIT_IS_SET(PINC,1)) && (a == 2) && ((PORTD == 0b01000000) || (PORTD = 0b00100000) || (PORTD == 0b00010000) || (PORTD == 0b00001000) || (PORTD == 0b00000100) || (PORTD == 0b00000010) || (PORTD == 0b00000001))){
a = 3;
}
if (a == 3) {
if (PORTD == 0b01000000) {
_delay_ms(200);
PORTD = 0b00100000;
_delay_ms(400);
PORTD = 0b00010000;
_delay_ms(600);
PORTD = 0b00001000;
_delay_ms(800);
PORTD = 0b00000100;
_delay_ms(700);
PORTD = 0b00000010;
a = 4;
} else if (PORTD == 0b00100000) {
_delay_ms(200);
PORTD = 0b00010000;
_delay_ms(400);
PORTD = 0b00001000;
_delay_ms(600);
PORTD = 0b00000100;
_delay_ms(800);
PORTD = 0b00000010;
_delay_ms(800);
PORTD = 0b00000001;
a = 4;
} else if (PORTD == 0b00010000) {
_delay_ms(200);
PORTD = 0b00001000;
_delay_ms(400);
PORTD = 0b00000100;
_delay_ms(600);
PORTD = 0b00000010;
_delay_ms(800);
PORTD = 0b00000001;
_delay_ms(800);
PORTD = 0b01000000;
a = 4;
} else if (PORTD == 0b00001000) {
_delay_ms(200);
PORTD = 0b00000100;
_delay_ms(400);
PORTD = 0b00000010;
_delay_ms(600);
PORTD = 0b00000001;
_delay_ms(800);
PORTD = 0b01000000;
_delay_ms(800);
PORTD = 0b00100000;
a = 4;
} else if (PORTD == 0b00000100) {
_delay_ms(200);
PORTD = 0b00000010;
_delay_ms(400);
PORTD = 0b00000001;
_delay_ms(600);
PORTD = 0b01000000;
_delay_ms(800);
PORTD = 0b00100000;
_delay_ms(800);
PORTD = 0b00010000;
a = 4;
} else if (PORTD == 0b00000010) {
_delay_ms(200);
PORTD = 0b00000001;
_delay_ms(400);
PORTD = 0b01000000;
_delay_ms(600);
PORTD = 0b00100000;
_delay_ms(800);
PORTD = 0b00010000;
_delay_ms(800);
PORTD = 0b00001000;
a = 4;
} else if (PORTD == 0b00000001) {
_delay_ms(200);
PORTD = 0b01000000;
_delay_ms(400);
PORTD = 0b00100000;
_delay_ms(600);
PORTD = 0b00010000;
_delay_ms(800);
PORTD = 0b00001000;
_delay_ms(800);
PORTD = 0b00000100;
a = 4;
}
}
Aucun commentaire:
Enregistrer un commentaire