/* Liest den Wert von einem Potentiometer Steuert die Motorrichtung und Geschwindigkeit */ const int geschwindigkeitPin = 9; const int motorPin1 = 8; const int motorPin2 = 7; const int potiPin = A4; const int LINKS = -1; const int STOPP = 0; const int RECHTS = 1; void setup() { analogWrite(geschwindigkeitPin,0); // Motor anhalten pinMode(motorPin1,OUTPUT); pinMode(motorPin2,OUTPUT); } void motorSteuerung() { static int momentaneRichtung = STOPP; int neueRichtung; int geschwindigkeit; // Wert von Poti lesen int potiVal = analogRead(potiPin); // Richtung und Geschwindigkeit feststellen if (potiVal < 450) { neueRichtung = LINKS; geschwindigkeit = 511 - potiVal; // links ist 0 die grösste Geschwindigkeit } else if (potiVal > 574) { neueRichtung = RECHTS; geschwindigkeit = potiVal - 512; // rechts 512 = 0 } else { neueRichtung = STOPP; geschwindigkeit = 0; } geschwindigkeit = geschwindigkeit / 2; // Bereich ist 0 .. 255 if (neueRichtung != momentaneRichtung) { // anhalten analogWrite(geschwindigkeitPin,0); delay(200); // etwas Pause // neue Richtung momentaneRichtung = neueRichtung; switch (neueRichtung) { case LINKS: digitalWrite(motorPin1,HIGH); digitalWrite(motorPin2,LOW); break; case RECHTS: digitalWrite(motorPin1,LOW); digitalWrite(motorPin2,HIGH); } } // Geschwindigkeit analogWrite(geschwindigkeitPin,geschwindigkeit); } void loop() { motorSteuerung(); }