#include //#include enum { LMax=140, //Left L1=115, L2=80, L3=40, RMax=140, //Right R1=115, R2=80, R3=40, }; #define Sen1 PC0 //1. sensors #define Sen2 PC1 //2. sensors #define Sen3 PC2 //3. sensors #define Sen4 PC3 //4. sensors #define Sens_LEDs PD0//To sensor diode (+5V) #define MOTL PB1//(Left) Motor #define MOTR PB2//(Right) Motor uint8_t skaititajs = 0,L = 0,R = 0,Taimers = 0; //uint16_t Taimers = 0; /*void wait_some_time() { uint8_t i; for(i=0;i<10;i++) _delay_ms(100); }*/ void Read_Sensors() { L=LMax; R=LMax; if ((PINC & (1 << Sen1)) != 0) { if ((PINC & (1 << Sen2)) != 0) { L=L1; R=R2;//1+2 } else { L=L1; R=R3;//1 } } else { if ((PINC & (1 << Sen2)) != 0) { if ((PINC & (1 << Sen3)) != 0) { L=LMax; R=RMax;//2+3 } else { L=LMax; R=R1;//2 } } else { if ((PINC & (1 << Sen3)) != 0) { if ((PINC & (1 << Sen4)) != 0) { L=L2; R=R1;//3+4 } else { L=L1; R=RMax;//3 } } else { if ((PINC & (1 << Sen4)) != 0) { L=L3; R=R1;//4 } else { L=LMax; R=RMax;//None } } } } // if (((PINC & (1 << (PINC & (1 << Sen1)) != 0)) != 0) & ((PINC & (1 << Sen4)) != 0)) {L=RMax; R=LMax;} PORTD &= ~(1 << Sens_LEDs); // Switch on sensor LEDs } int main() { /* Ports as input and set pull-up */ DDRC = 0x00; //PORTC = (1 << (PINC & (1 << Sen1)) != 0)|(1 << (PINC & (1 << Sen2)) != 0)|(1 << Sen3)|(1 << Sen4); /* Set output pins */ PORTD = 0; DDRD = (1 << Sens_LEDs); PORTB = 0; DDRB = (1 << MOTL)|(1 << MOTR); PORTD |= (1 << Sens_LEDs); // Cikl while (1) { if (Taimers == 100) PORTD |= (1 << Sens_LEDs); if (++Taimers == 180) { Taimers = 0; Read_Sensors(); } //Motor control if (skaititajs == 0) {PORTB |= (1 << MOTL); PORTB |= (1 << MOTR);} if (skaititajs == L) PORTB &= ~(1 << MOTL); if (skaititajs == R) PORTB &= ~(1 << MOTR); skaititajs++; // skaititajs means counter. Use when counter goes from 255 to 0 } }