Prikladam este pouzity kod:
Kód: Vybrať všetko
#include <avr/io.h>
#define LED_1 PB0
#define LED_2 PB1
#define LED_3 PB2
#define LED_4 PB3
#define SIGNAL_A (PINB & (1<<PB4))
#define SIGNAL_B (PINB & (1<<PB5))
int main(void){
uint8_t dir = 0;
uint8_t smer = 0;
DDRB = 0b00001111;
PORTB |= (1<<LED_2);
PORTB |= (1<<LED_3);
while(1){
if (dir == 0) {
if (SIGNAL_A & (!SIGNAL_B)) {
dir = 2;
} else if(SIGNAL_B & (!SIGNAL_A)) {
dir = 4;
} else {
dir = 0;
}
} else if (dir == 2){
if (SIGNAL_A & (!SIGNAL_B)) {
dir = 2;
} else if ((!SIGNAL_A) & (!SIGNAL_B)) {
smer = 0;
dir = 0;
} else {
dir = 0;
}
} else if (dir == 4) {
if (SIGNAL_B & (!SIGNAL_A)) {
dir = 4;
} else if ((!SIGNAL_A) & (!SIGNAL_B)) {
smer = 1;
dir = 0;
} else {
dir = 0;
}
} else if (SIGNAL_B & SIGNAL_A) {
dir = 0;
}
if (smer == 0) {
PORTB |= (1<<LED_1);
PORTB &= ~(1<<LED_4);
} else {
PORTB |= (1<<LED_4);
PORTB &= ~(1<< LED_1);
}
}
return 0;
}