volatile int state = 0, state2 = 0;
volatile bool schalter = true;
void setup() {
noInterrupts();
Serial.begin(9600);
setup_led();
setup_interrupt();
test();
setup_timer();
interrupts();
}
void setup_led() {
DDRD |= ((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5) | (1 << PD6) | (1 << PD7));
PORTD &= ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5) | (1 << PD6) | (1 << PD7));
}
void setup_button() {
DDRB |= (1 << PB5);
PORTB |= (1 << PB5);
MCUCR &= ~(1 << PUD);
}
void setup_interrupt() {
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT9);
}
void test() {
for (int i = 2; i <= 7; i++) {
PORTD = 1 << i;
delay(300);
}
PORTD &= ~(1 << PD7);
}
void setup_timer() {
TCCR1A = TCCR1B = 0;
TCCR1B |= ((1 << CS10) | (1 << CS12));
TCCR1B &= ~(1<<CS11);
TCNT1 = 49911;
}
void schaltung() {
if (schalter) {
switch (state) {
case 0:
PORTD |= (1 << PD2);
PORTD &= ~((1 << PD3) | (1 << PD4));
delay(1000);
schalter = !schalter;
state = 1;
break;
case 1:
PORTD |= ((1 << PD2) | (1 << PD3));
PORTD &= ~(1 << PD4);
delay(1000);
state = 2;
break;
case 2:
PORTD |= (1 << PD4);
PORTD &= ~((1 << PD2) | (1 << PD3));
delay(1000);
state = 3;
break;
case 3:
PORTD |= (1 << PD3);
PORTD &= ~((1 << PD2) | (1 << PD4));
delay(1000);
state = 0;
break;
}
} else {
switch (state2) {
case 0:
PORTD |= (1 << PD5);
PORTD &= ~((1 << PD6) | (1 << PD7));
delay(1000);
schalter = !schalter;
state2 = 1;
break;
case 1:
PORTD |= ((1 << PD5) | (1 << PD6));
PORTD &= ~(1 << PD7);
delay(1000);
state2 = 2;
break;
case 2:
PORTD |= (1 << PD7);
PORTD &= ~((1 << PD5) | (1 << PD6));
delay(1000);
state2 = 3;
break;
case 3:
PORTD |= (1 << PD6);
PORTD &= ~((1 << PD5) | (1 << PD7));
delay(1000);
state2 = 0;
break;
}
}
}
ISR(PCINT0_vect) {
Serial.println("Button");
}
ISR(TIMER1_OVF_vect) {
schaltung();
TCNT1 = 49911;
}
void loop() {
}