10. November 2010
Der DC Motor wird mit einem Drucksensor gesteuert. Wird der Sensor fest gedrückt, wird der Motor langsamer und haltet sogar an. Beim Wiederloslassen ändert der Motor seine Laufrichtung.#define EM 12 #define DIN1 11 #define DIN2 10 unsigned long time; int zaehler = 0; void setup (){ pinMode(EM, OUTPUT); pinMode(DIN1, OUTPUT); pinMode(DIN2, OUTPUT); Serial.begin( 9600 ); digitalWrite(EM, LOW); analogWrite(DIN1, 0); analogWrite(DIN2, 0); } void loop (){ digitalWrite(EM, HIGH); int sensorValue = analogRead(1)/4; if (zaehler == 0){ if (sensorValue > 220){ sensorValue = 255; } else if (sensorValue > 200 && sensorValue < 220){ sensorValue = 220; } else if (sensorValue > 180 && sensorValue < 200){ sensorValue = 200; } else if (sensorValue > 160 && sensorValue < 180){ sensorValue = 180; } else if (sensorValue > 150 && sensorValue < 160){ sensorValue = 160; } else if (sensorValue > 140 && sensorValue < 150){ sensorValue = 140; } else if (sensorValue < 140){ sensorValue = 0; zaehler = 1; } delay(200); analogWrite(DIN1, sensorValue); analogWrite(DIN2, 0); } else if (zaehler == 1){ if (sensorValue > 220){ sensorValue = 255; } else if (sensorValue > 200 && sensorValue < 220){ sensorValue = 220; } else if (sensorValue > 180 && sensorValue < 200){ sensorValue = 200; } else if (sensorValue > 160 && sensorValue < 180){ sensorValue = 180; } else if (sensorValue > 150 && sensorValue < 160){ sensorValue = 160; } else if (sensorValue > 140 && sensorValue < 150){ sensorValue = 140; } else if (sensorValue < 140){ sensorValue = 0; zaehler = 0; } delay(200); analogWrite(DIN1, 0); analogWrite(DIN2, sensorValue); } Serial.println(sensorValue, DEC); }