//———————————————————————————-
// initial setup
//
// motor A refers to the right motor; motor B refers to the left motor
// speeds range from 5 to 180; this refers to the power delivered to the motor driver
//———————————————————————————-
int state = 0; //the initial state of the robot is 0 (no movement)
int spd = 180; //the initial speed of the robot is 180 (max)
String serialRead = String(0); //storage string for information from bluetooth module
int srlRead = 0; //intermediate integer used during if statement
const int motorPin1 = 5; // motar A pin 1 (IN1 on motor driver & pin 5 on the arduino)
const int motorPin2 = 6; // motor A pin 2 (IN2 on motor driver & pin 6 on the arduino)
const int motorPin3 = 10; // motor B pin 1 (IN3 on motor driver & pin 10 on the arduino)
const int motorPin4 = 9; // motor B pin 2 (IN4 on motor driver & pin 9 on the arduino)
const int LED = 13; // LED (pin 13 on arduino)
void setup() {
//set up bluetooth
Serial.begin(9600); // Default communication rate of the Bluetooth module
//Set pins as outputs for motors coresponding to integers established above
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
serialRead = Serial.read(); // Reads the data from the serial port & stores the information in a string
srlRead = serialRead.toInt(); // converts the string to an integer
//Serial.println(serialRead); // this line of code can be uncommented to use the serial reader to debug
if (srlRead>4){ // if the integer is greater than 4 then it is a speed adjustment
spd=srlRead; // changes the speed to the coresponding value
}else { //if the value is less than 5 it is a state and the state needs to be adjusted accordingly
state=srlRead; //adjust the state
}
}
if (state == 0){ // state zero corresponds to motors having no power
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 0);
digitalWrite(LED, LOW); // the LED is off when the motor is not running
}
else if (state == 1) { // state one corresponds to both motors forward
digitalWrite(LED, HIGH);
analogWrite(motorPin1, spd); //Move Right wheel forward
analogWrite(motorPin2, 0);
analogWrite(motorPin3, spd); //Move Left wheel forward
analogWrite(motorPin4, 0);
digitalWrite(LED, HIGH); //LED turns on when the robot is moving
//Serial.println(“FORWARD”); // this line of code can be uncommented to use the serial reader to debug
}
else if (state == 2) { //BACKWARD
analogWrite(motorPin1, 0);
analogWrite(motorPin2, spd); //Move Right wheel Backward
analogWrite(motorPin3, 0);
analogWrite(motorPin4, spd); //Move Left wheel backward
digitalWrite(LED, HIGH); //LED turns on when the robot is moving
//Serial.println(“BACK”); // this line of code can be uncommented to use the serial reader to debug
}
else if (state == 3) { //RIGHT
analogWrite(motorPin1, spd); //Move Right wheel forward
analogWrite(motorPin2, 0);
analogWrite(motorPin3, spd/5.); //Move Left wheel forward at 1/5 speed
analogWrite(motorPin4, 0);
digitalWrite(LED, HIGH); //LED turns on when the robot is moving
//Serial.println(“RIGHT”); // this line of code can be uncommented to use the serial reader to debug
}
else if (state == 4) { //LEFT
analogWrite(motorPin1, spd/5.); //Move Right wheel forward at 1/5 speed
analogWrite(motorPin2, 0);
analogWrite(motorPin3, spd); //Move Left wheel forward
analogWrite(motorPin4, 0);
digitalWrite(LED, HIGH); //LED turns on when the robot is moving
//Serial.println(“LEFT”); // this line of code can be uncommented to use the serial reader to debug
}
}