/* Evil robot brain! Ultra basic model. */ int E1 = 6; //M1 Speed Control int E2 = 5; //M2 Speed Control int M1 = 8; //M1 Direction Control int M2 = 7; //M2 Direction Control const int buttonPin = 12; // the number of the pushbutton pin int avoid=0; int bump=0; void setup() { pinMode(13, OUTPUT); // Avoid status LED pinMode(buttonPin, INPUT); // Bump sensor analogWrite (E2,255); analogWrite (E1,255); } void loop() { bump = digitalRead(buttonPin); // Read bump sensor if (bump == HIGH) { // Have we hit somthing? avoid=1; // Yes! Avoid it! } if (avoid == 1) { digitalWrite(13, HIGH); // Avoid start digitalWrite(M1,HIGH); digitalWrite(M2,HIGH); // Reverse robot for 1 second delay(1000); analogWrite (E2,0); // Pretend to be a roomba; delay(random(200,5000)); // Switch off one motor for a random analogWrite (E2,255); // Number of seconds. avoid=0; digitalWrite(13, LOW); // Avoid done } else { // No? Then keep going, full speed ahead! digitalWrite(M1,LOW); digitalWrite(M2,LOW); } }