/* ----------------------------------
Motorsteuerung mit Poti und Taster
- - - - - - - - - - - - - - - - - -
Die leuchtende LED zeigt,
wenn der Motor sich drehen würde.
Die Geschwindigkeit des Motors wird
mit Serial.print angezeigt.
---------------------------------- */
const int motorPin1=5;
const int motorPin2=3; // unterstützt PWM, akzeptiert analoge Werte
const int potiPin=A2;
const int tasterPin=10;
const int ledPin=12; //
int potiWert, motorWert = 0;
bool tastWert, isRunning; // bool= nur 0 oder 1 bzw. LOW oder HIGH
void setup(){
pinMode(motorPin1,OUTPUT);
pinMode(motorPin2,OUTPUT);
pinMode(potiPin,INPUT);
pinMode(tasterPin,INPUT_PULLUP); // Verbindet den Tasterpin mit dem internen Pullup-Widerstand
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
// Ein sauberer Stop vor dem Richtungswechsel schont den Motor
void motorStop(){
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
delay(500);
}
void loop(){
tastWert = digitalRead(tasterPin);
delay(50);
if (tastWert == LOW) {
isRunning = !isRunning; // Wert toggled -> 0 wird zu 1 bzw. 1 zu 0
}
Serial.print("taster: "); Serial.print(tastWert);
Serial.print(" - run: "); Serial.print(isRunning);
if (isRunning) {
potiWert = analogRead(potiPin);
if (potiWert <= 500) {
motorWert = map(potiWert, 0, 500, 255, 0);
analogWrite(motorPin1,motorWert); // Motor Vor
digitalWrite(motorPin2,LOW);
delay(100);
} else if (potiWert >= 550) {
motorWert = map(potiWert, 550, 1023, 0, 255);
digitalWrite(motorPin1,LOW); // Motor Zurück
analogWrite(motorPin2,motorWert);
delay(100);
} else {
motorWert = 0;
motorStop(); // Motor Stop
}
// Ist der motorWert größer 0, leuchtet die LED
if (motorWert > 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
} else {
motorWert = 0;
digitalWrite(ledPin, LOW);
motorStop();
}
Serial.print(" - Poti: "); Serial.print(potiWert);
Serial.print(" - Motor: "); Serial.println(motorWert);
}