#define F_CPU 16500000UL
#include <avr/io.h>
#include <util/delay.h>
#define testBit(value, bit) (value & (1 << bit))
uint8_t dir = 0;
uint16_t k = 0;
uint16_t intv = 14400;
int main()
{
DDRB = 0b00000111; // Set PB0, PB1, PB2 as output and PB5 as input
TCCR0A = 0x00; // Normal mode
TCCR0B = 0x00;
TCCR0B |= (1 << CS00); // Set prescaling with 1024
TCNT0 = 0;
while (1) {
if (!testBit(PINB, PB5)) {
PORTB |= (1 << PB1);
PORTB &= ~(1 << PB2);
dir = 0;
k = 0;
wait();
PORTB &= ~(1 << PB1);
PORTB |= (1 << PB2);
dir = 1;
k = 3 * intv;
wait();
dir = 0;
k = 0;
PORTB &= ~(1 << PB2);
PORTB |= (1 << PB1);
wait();
}
PORTB &= ~(1 << PB1);
}
return 0;
}
void wait()
{
uint16_t i = 0;
uint16_t wsp;
while (i <= intv) {
while ((TIFR & (1 << TOV0) ) == 0);
TIFR |= (1 << TOV0);
wsp = (k / 1000 + 2);
if (i % wsp == 0) {
PORTB ^= (1 << PB0);
}
if (dir == 0) {
k += 2;
} else {
k -= 1;
}
i++;
}
}