/* Code for Part 3 of the BaW-Bot Series of Instructables This code drives the bot dependant on inputs received from an IR Remote. It uses the following few simple functions: - motorDrive(motorNumber, motorDirection, motorSpeed) - motorBrake(motorNumber) - motorStop(motorNumber) - motorsStandby Connections: Motor Driver - Pin 5 ---> PWMA - Pin 6 ---> AIN2 - Pin 7 ---> AIN1 - Pin 8 ---> STBY - Pin 9 ---> PWMB - Pin 10 ---> BIN2 - Pin 11 ---> BIN1 Other - Pin 12 ---> IR Sensor pin OUT - Motor 1: A01 and A02 - Motor 2: B01 and B02 */ #include //Define the Pins //Motor 1 int pinAIN1 = 7; //Direction int pinAIN2 = 6; //Direction int pinPWMA = 5; //Speed //Motor 2 int pinBIN1 = 11; //Direction int pinBIN2 = 10; //Direction int pinPWMB = 9; //Speed //Standby int pinSTBY = 8; //Other Pins int pinIRRecv = 12; //IR Receiver // // // ** CHANGE THE LINE BELOW TO INSERT YOUR IR CODE ** // // const unsigned int irCodeStartStop = 2011265621; unsigned int irCodeRead; //Constants to help remember the parameters static boolean turnCW = 0; //for motorDrive function static boolean turnCCW = 1; //for motorDrive function static boolean motor1 = 0; //for motorDrive, motorStop, motorBrake functions static boolean motor2 = 1; //for motorDrive, motorStop, motorBrake functions //Working Variables int iCurrentMotion = 0; //This stores the current motion of the Bot: 0 = STOP 1 = FWD 2 = STOP 3 = TURN int iNewMotion = 0; //This decides the new motion of the Bot //Instantiate IR Receiver IRrecv irrecv(pinIRRecv); //Variable to store IR Results decode_results results; void setup() { //Set the PIN Modes pinMode(pinPWMA, OUTPUT); pinMode(pinAIN1, OUTPUT); pinMode(pinAIN2, OUTPUT); pinMode(pinPWMB, OUTPUT); pinMode(pinBIN1, OUTPUT); pinMode(pinBIN2, OUTPUT); pinMode(pinSTBY, OUTPUT); irrecv.enableIRIn(); // Start the receiver //Make sure motors are in Standby mode (not turning!) motorsStandby(); //For debugging: Serial.begin(9600); } void loop() { //Check for Infrared Signals if (irrecv.decode(&results)) { irCodeRead = results.value; Serial.println(irCodeRead); switch (irCodeRead) { //If the predefined code received, change the motion case irCodeStartStop: iNewMotion++; if (iNewMotion > 3) {iNewMotion = 0;} Serial.print("Received Code. Motion now: "); Serial.println(iNewMotion); break; } irrecv.resume(); // Receive the next value } if (iNewMotion != iCurrentMotion) { switch (iNewMotion) { case 0: Serial.println("Stop"); motorsStandby(); break; case 1: Serial.println("Forward"); motorDrive(motor1, turnCW, 200); motorDrive(motor2, turnCW, 200); break; case 2: Serial.println("Stop"); motorsStandby(); break; case 3: Serial.println("Turn"); motorDrive(motor1, turnCCW, 192); motorDrive(motor2, turnCW, 192); break; } iCurrentMotion = iNewMotion; } } void motorDrive(boolean motorNumber, boolean motorDirection, int motorSpeed) { /* This Drives a specified motor, in a specific direction, at a specified speed: - motorNumber: motor1 or motor2 ---> Motor 1 or Motor 2 - motorDirection: turnCW or turnCCW ---> clockwise or counter-clockwise - motorSpeed: 0 to 255 ---> 0 = stop / 255 = fast */ boolean pinIn1; //Relates to AIN1 or BIN1 (depending on the motor number specified) //Specify the Direction to turn the motor //Clockwise: AIN1/BIN1 = HIGH and AIN2/BIN2 = LOW //Counter-Clockwise: AIN1/BIN1 = LOW and AIN2/BIN2 = HIGH if (motorDirection == turnCW) pinIn1 = HIGH; else pinIn1 = LOW; //Select the motor to turn, and set the direction and the speed if(motorNumber == motor1) { digitalWrite(pinAIN1, pinIn1); digitalWrite(pinAIN2, !pinIn1); //This is the opposite of the AIN1 analogWrite(pinPWMA, motorSpeed); } else { digitalWrite(pinBIN1, pinIn1); digitalWrite(pinBIN2, !pinIn1); //This is the opposite of the BIN1 analogWrite(pinPWMB, motorSpeed); } //Finally , make sure STBY is disabled - pull it HIGH digitalWrite(pinSTBY, HIGH); } void motorBrake(boolean motorNumber) { /* This "Short Brake"s the specified motor, by setting speed to zero */ if (motorNumber == motor1) analogWrite(pinPWMA, 0); else analogWrite(pinPWMB, 0); } void motorStop(boolean motorNumber) { /* This stops the specified motor by setting both IN pins to LOW */ if (motorNumber == motor1) { digitalWrite(pinAIN1, LOW); digitalWrite(pinAIN2, LOW); } else { digitalWrite(pinBIN1, LOW); digitalWrite(pinBIN2, LOW); } } void motorsStandby() { /* This puts the motors into Standby Mode */ digitalWrite(pinSTBY, LOW); }