Rover Eda

Schéma zapojení Educanta

Kód bez serva (padlo na Kubu)

#define trig 13
#define echo 12
#define ENA 11
#define IN1 10
#define IN2 9
#define IN3 8
#define IN4 7
#define ENB 6


void vpred(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void vlevo(){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void vpravo(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void vzad(){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void stat(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}


void setup() {
for (byte i=6; i<=11;i++) {
pinMode(i,OUTPUT);
}
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
randomSeed(analogRead(A4));
delay(2000); // odlozeny start o 2 s
}


void loop() {
long cas, delka;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
cas = pulseIn(echo, HIGH);
delka = (cas/2) / 29.1;
if(delka<25) {
random(2)==1 ? vlevo():vpravo();
delay(750); // cas na zatoceni
}
else {
vpred();
delay(15);
}
while(millis() > 120000)
{ // stop po 2 min
stat();
}
}