Code:
// Sensor Links
long laengelinks;
long cm2;
// Sensor Vorne
long laengevorne;
long cm;
// Sensor Rechts
long laengerechts;
long cm3;
// Motor Rechts
const int Motor1Pin1 = 1;
const int Motor1Pin2 = 0;
//Motor Links
const int Motor2Pin1 = 2;
const int Motor2Pin2 = 3;
//int Abstand = 10;
void setup()
{
// Sensor PINS
// Serial.begin (9600);
pinMode(11,OUTPUT);
pinMode(10,INPUT);
pinMode(12,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,INPUT);
pinMode(7,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,INPUT);
pinMode(4,OUTPUT);
// Motor PINS
pinMode(Motor1Pin1, OUTPUT);
pinMode(Motor1Pin2, OUTPUT);
pinMode(Motor2Pin1, OUTPUT);
pinMode(Motor2Pin2, OUTPUT);
// pinMode(13,OUTPUT);
}
void loop()
{
scanvorne();
if (cm >= 20)
{
GoForward();
delay(300);
}
else
{
Stop();
scanlinks();
if (cm >= 20)
{
GoLeft();
delay(250);
}
else
{
Stop();
scanrechts();
if (cm3 >= 20)
{
GoRight();
delay(250);
}
else
{
Stop();
}
}
}
}
// Sensor Steuerung
void scanvorne()
{
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
delayMicroseconds(1000);
digitalWrite(11,LOW);
laengevorne = pulseIn(10,HIGH);
cm = (laengevorne/2)/29.1;
Serial.print(cm);
Serial.println("cm1");
}
void scanlinks()
{
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delayMicroseconds(1000);
digitalWrite(8,LOW);
laengelinks = pulseIn(9,HIGH);
cm2 = (laengelinks/2)/29.1;
Serial.print(cm2);
Serial.println("cm2");
}
void scanrechts()
{
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
delayMicroseconds(1000);
digitalWrite(5,LOW);
laengerechts = pulseIn(6,HIGH);
cm3 = (laengerechts/2)/29.1;
Serial.print(cm3);
Serial.println("cm3");
}
//Motoren Steuerung
void GoForward(){
digitalWrite(Motor1Pin1, HIGH);
digitalWrite(Motor2Pin1, HIGH);
digitalWrite(Motor2Pin2, LOW);
digitalWrite(Motor1Pin2, LOW);
}
void GoBackward(){
digitalWrite(Motor1Pin1, LOW);
digitalWrite(Motor1Pin2, HIGH);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, HIGH);
}
void GoLeft(){
digitalWrite(Motor1Pin1, HIGH);
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, LOW);
}
void GoRight(){
digitalWrite(Motor2Pin2, LOW);
digitalWrite(Motor2Pin1, HIGH);
digitalWrite(Motor1Pin1, LOW);
digitalWrite(Motor1Pin2, LOW);
}
void Stop(){
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor1Pin1, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, LOW);
}
Lesezeichen