// --------------------------------------------------------------------------- Motors int motor_left[] = {2, 3}; int motor_right[] = {7, 8}; char command; //Initializing all variables //control pin for knight ryder int light = 9; int state = LOW; //digital pin 17 (analog 3) is the buzzer pin. int buz = 17; //counter for the Override beep. int buzCount = 0; //counter for the start - up beep. int startBuz = 0; //Variables where distances measured are stored. int distFront; int distLeft; int distRight; //Minimum distance of 20cm from any obstacle. int distLimit = 20; //HCSR04 Variables //digital pin 14,15 (analog 0 and 1 respectively) int trig = 14; int echo = 15; unsigned long pulsetime = 0; //Calculate Battery Level1. Arduino const float maxBattery1 = 8.0;// Change value to your max battery voltage level! int perVolt1; // Percentage variable float voltage1 = 0.0; // Read battery voltage int level1; //Calculate Battery Level2. Motor const float maxBattery2 = 6.0;// Change value to your max battery voltage level! int perVolt2; // Percentage variable float voltage2 = 0.0; // Read battery voltage int level2; // Use it to make a delay... without delay() function! long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec. long interval = 1000*10; // interval at which to read battery voltage, change it if you want! (10*1000=10sec) unsigned long currentMillis; //unsigned long currentMillis; // --------------------------------------------------------------------------- Setup void setup() { Serial.begin(9600); pinMode(trig,OUTPUT); pinMode(echo,INPUT); pinMode(buz,OUTPUT); pinMode(light,OUTPUT); startBeep(); // Setup motors int i; for(i = 0; i < 2; i++){ pinMode(motor_left[i], OUTPUT); pinMode(motor_right[i], OUTPUT); } } void loop() { if (Serial.available() > 0) { command = Serial.read(); motor_stop(); //initialize with motors stopped //Change pin mode only if new command is different from previous. //Serial.println(command); switch (command) { case 'F': //Checks for obstacle since forward command is received. distFront = readDistance(); if(distFront < distLimit) { //Override the controller since obstacle is detected. override(); distFront = readDistance(); Serial.print("A"); Serial.print(";"); Serial.print(distFront); //send distance to MIT App Serial.print(";"); Serial.print(0,DEC); //2nd value...dummy variable Serial.println(";"); } else { //No obstacle----> move forward. drive_forward(); } break; case 'B': drive_backward(); break; case 'L': turn_left(); break; case 'R': turn_right(); break; case 'S': { beep(); buzCount = 0; } break; case 'T': //reads distance { distFront = readDistance(); Serial.print("A"); Serial.print(";"); Serial.print(distFront); //send distance to MIT App Serial.print(";"); Serial.print(0,DEC); //2nd value...dummy variable Serial.println(";"); } break; case 'X': //switch ON OFF larson scanner if (state == HIGH){ state = LOW; digitalWrite(light, state); }else{ state = HIGH; digitalWrite(light, state);} break; } } /***********************Battery*****************************/ //Read battery voltage every 10sec. currentMillis = millis(); if(currentMillis - (previousMillis) > (interval)) { previousMillis = currentMillis; //Read voltage from analog pin A4 and make calibration for Arduino: voltage1 = (analogRead(A4)*5.0/1024.0)*2.0; //8.0V //Calculate percentage... perVolt1 = (voltage1*100)/ maxBattery1; if (perVolt1<=75) { level1=0; } else if (perVolt1>75 && perVolt1<=80) { level1=1; } // Battery level else if (perVolt1>80 && perVolt1<=85) { level1=2; } //Min ------------------------ Max else if (perVolt1>85 && perVolt1<=90) { level1=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | > else if (perVolt1>90 && perVolt1<=95) { level1=4; } // ------------------------ else if (perVolt1>95) { level1=5; } //Read voltage from analog pin A6 and make calibration for motor: voltage2 = (analogRead(A6)*5.0/1024.0)*2.0; // 6.0V //Calculate percentage... perVolt2 = (voltage2*100)/ maxBattery2; if (perVolt2<=75) { level2=0; } else if (perVolt2>75 && perVolt2<=80) { level2=1; } // Battery level else if (perVolt2>80 && perVolt2<=85) { level2=2; } //Min ------------------------ Max else if (perVolt2>85 && perVolt2<=90) { level2=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | > else if (perVolt2>90 && perVolt2<=95) { level2=4; } // ------------------------ else if (perVolt2>95) { level2=5; } Serial.print("B"); Serial.print(";"); Serial.print(level1); Serial.print(";"); Serial.print(level2); Serial.println(";"); //Serial.println(perVolt1); //Serial.println(voltage1); //Serial.println(";"); //Serial.println(perVolt2); //Serial.println(voltage2); } } // --------------------------------------------------------------------------- Drive void motor_stop(){ digitalWrite(motor_left[0], LOW); digitalWrite(motor_left[1], LOW); digitalWrite(motor_right[0], LOW); digitalWrite(motor_right[1], LOW); delay(25); } void drive_forward(){ digitalWrite(motor_left[0], HIGH); digitalWrite(motor_left[1], LOW); digitalWrite(motor_right[0], HIGH); digitalWrite(motor_right[1], LOW); } void drive_backward(){ digitalWrite(motor_left[0], LOW); digitalWrite(motor_left[1], HIGH); digitalWrite(motor_right[0], LOW); digitalWrite(motor_right[1], HIGH); } void turn_left(){ digitalWrite(motor_left[0], LOW); digitalWrite(motor_left[1], HIGH); digitalWrite(motor_right[0], HIGH); digitalWrite(motor_right[1], LOW); } void turn_right(){ digitalWrite(motor_left[0], HIGH); digitalWrite(motor_left[1], LOW); digitalWrite(motor_right[0], LOW); digitalWrite(motor_right[1], HIGH); } //Returns distance value from ultrasonic sensor int readDistance() { digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig,LOW); pulsetime = pulseIn(echo, HIGH); return pulsetime/58.2;//magic } void override() { /*Note: The time it takes for the rover to turn depends in the motors you use...so that will be fixed during debugging.*/ //Stop receiving data from the user. Serial.end(); delay(10); //Obstacle avoidance maneuver and all that jazz.. motor_stop(); delay(100); beep(); buzCount= 0; delay(500); drive_backward(); delay(300); motor_stop(); delay(10); scanLeft(); delay(100); scanRight(); if(distLeft > distRight && distLeft > distLimit) { turn_left(); delay(500); motor_stop(); } else if(distRight > distLeft && distRight > distLimit) { turn_right(); delay(500); motor_stop(); } else { drive_backward(); delay(300); motor_stop(); } //Restart the receiver. Serial.begin(9600); } void scanLeft() { Serial.println("Scanning Left...."); delay(10); turn_left(); //turns left to scan delay(300); motor_stop(); delay(10); distLeft = readDistance(); //Serial.print("Left is "); //Serial.println(distLeft); //scan delay(100); turn_right(); //turns to the centre again delay(300); motor_stop(); } void scanRight() { Serial.println("Scanning Right...."); delay(10); turn_right(); delay(300); motor_stop(); delay(10); distRight = readDistance(); //Serial.print("Right is "); //Serial.println(distRight); delay(100); turn_left(); delay(300); motor_stop(); } //Obstacle detected beep void beep() { while(buzCount<2) { digitalWrite(buz,LOW); delay(50); digitalWrite(buz,HIGH); delay(50); digitalWrite(buz,LOW); buzCount++; } } //Start-up beep void startBeep() { while(startBuz<2) { digitalWrite(buz,LOW); delay(50); digitalWrite(buz,HIGH); delay(50); digitalWrite(buz,LOW); delay(50); digitalWrite(buz,HIGH); delay(75); digitalWrite(buz,LOW); startBuz++; } }