const int DriverA_1 = 14; const int DriverA_2 = 15; const int plus_sensor = 2; const int sense_sensor = A2; const int minus_sensor = 4; //const int DriverB_1 = 10; //const int DriverB_2 = 11; char key; char S = 's'; char B = 'b'; char C = 'c'; int value = 0; int threshold = 100; void setup() { pinMode(DriverA_1, OUTPUT); pinMode(DriverA_2, OUTPUT); pinMode(plus_sensor, OUTPUT); pinMode(sense_sensor, INPUT); pinMode(minus_sensor, OUTPUT); digitalWrite(plus_sensor, HIGH); digitalWrite(minus_sensor, LOW); //pinMode(DriverB_1, OUTPUT); //pinMode(DriverB_2, OUTPUT); Serial.begin(9600); } void loop() { if (Serial.available()) { key = Serial.read(); if(key == S){ Serial.print("Start!"); Serial.println(""); while (Serial.read()) { value = analogRead(sense_sensor); Serial.println(value); if (value < threshold){ motor(HIGH, LOW, 150); } else { motor(HIGH, LOW, 10); } } } else if (key == B){ Serial.print("Back!"); Serial.println(""); motor(LOW, HIGH, 3000); } else if (key == C){ Serial.print("Stop!"); Serial.println(""); motor(LOW, LOW, 3000); } } } void motor(int a1, int a2, int d){ digitalWrite(DriverA_1, a1); digitalWrite(DriverA_2, a2); delay(d); digitalWrite(DriverA_1, a2); digitalWrite(DriverA_2, a2); delay(d); }