Stap 9: Verst: Bluetooth, bestuurd door Android telefoon
Zie mijn vorige bluetooth-panzer zonder camera. Het wordt beheerd door Android telefoon. Vier 555 Timer ICs rijden de dual gelijkstroommotoren. Verwacht niet dat snelheid :D
Verbinding maken met het bovenstaande diagram en upload deze schets:
/* Chienline @ 2014 Controlling an Arduino car/tank using an Android phone over Bluetooth connection. Android Software : Arduino Bluetooth RC Car by Andi.Co [in PlayStore]. Some codes are changed from the Official codes to get it works with my DIY Motor Driver. */char dataIn = 'S'; //Character/Data coming from the phone. S=Stop; int LCPin = 2; // 555 pinout 5 Left Motor int LTPin = 3; // 555 pinout 2 Left Motor int RCPin = 4; // 555 pinout 5 Right Motor int RTPin = 5; // 555 pinout 2 Right Motorint pinfrontLights = 6; //Pin that activates the Front lights. int pinbackLights = 7; //Pin that activates the Back lights. char determinant; //Used in the check function, stores the character received from the phone. char det; //Used in the loop function, stores the character received from the phone. int velocity = 0; //Stores the speed based on the character sent by the phone.void setup() { Serial.begin(9600); //Initialize serial communication with Bluetooth module at 9600 baud rate. pinMode(LCPin, OUTPUT); pinMode(LTPin, OUTPUT); pinMode(RCPin, OUTPUT); pinMode(RTPin, OUTPUT); pinMode(pinfrontLights , OUTPUT); pinMode(pinbackLights , OUTPUT); //Stop both motors on power up. stopMotors(); }void loop() { det = check(); while (det == 'F') //if incoming data is a F, move forward { moveForward(); det = check(); } while (det == 'B') //if incoming data is a B, move back { moveBackward(); det = check(); } while (det == 'L') //if incoming data is a L, move wheels left { moveLeftForward(); det = check(); } while (det == 'R') //if incoming data is a R, move wheels right { moveRightForward(); det = check(); } while (det == 'I') //if incoming data is a I, turn right forward { moveRightForward(); det = check(); } while (det == 'J') //if incoming data is a J, turn right back { moveRightBackward(); det = check(); } while (det == 'G') //if incoming data is a G, turn left forward { moveLeftForward(); det = check(); } while (det == 'H') //if incoming data is a H, turn left back { moveLeftBackward(); det = check(); } while (det == 'S') //if incoming data is a S, stop { stopMotors(); det = check(); }//Front Lights and Back Lights on/off are cool, but for starting I haven't connect them to my circuit. //But i leave the detection in the code for further use. while (det == 'U') //if incoming data is a U, turn ON front lights { digitalWrite(pinfrontLights, HIGH); det = check(); } while (det == 'u') //if incoming data is a u, turn OFF front lights { digitalWrite(pinfrontLights, LOW); det = check(); } while (det == 'W') //if incoming data is a W, turn ON back lights { digitalWrite(pinbackLights, HIGH); det = check(); } while (det == 'w') //if incoming data is a w, turn OFF back lights { digitalWrite(pinbackLights, LOW); det = check(); } }int check() { if (Serial.available() > 0) //Check for data on the serial lines. { dataIn = Serial.read(); //Get the character sent by the phone and store it in 'dataIn'. // Serial.println(dataIn); //this line is for debugging using Arduino serial monitor. if (dataIn == 'F') { determinant = 'F'; } else if (dataIn == 'B') { determinant = 'B'; } else if (dataIn == 'L') { determinant = 'L'; } else if (dataIn == 'R') { determinant = 'R'; } else if (dataIn == 'I') { determinant = 'I'; } else if (dataIn == 'J') { determinant = 'J'; } else if (dataIn == 'G') { determinant = 'G'; } else if (dataIn == 'H') { determinant = 'H'; } else if (dataIn == 'S') { determinant = 'S'; } //----------------------------- //Standard DC Motors are not fast enough to play with velocity. //You can set the velocity using 555 Trigger Pin with analogWrite(); //I bet you don't want to make your tank slower than its higest speed using standard DC Motors. /*----------------------------- else if (dataIn == '0') //velocity = 0 - 9, q = top speed. { velocity = 20; // value = 0 - 255; "velocity" does not need to be returned. } -------------------------------*/ else if (dataIn == 'U') { determinant = 'U'; } else if (dataIn == 'u') { determinant = 'u'; } else if (dataIn == 'W') { determinant = 'W'; } else if (dataIn == 'w') { determinant = 'w'; } } return determinant; }//These direction functions are designed for 555 Motor Driver //You need to change them to suit your other motor drivers.void moveForward(){ digitalWrite(LCPin, HIGH); digitalWrite(LTPin, HIGH); //L-Forward digitalWrite(RCPin, HIGH); digitalWrite(RTPin, HIGH); //R-Forward }void moveBackward(){ digitalWrite(LCPin, HIGH); digitalWrite(LTPin, LOW); //L-Backward digitalWrite(RCPin, HIGH); digitalWrite(RTPin, LOW); //R-Backward }void stopMotors(){ digitalWrite(LCPin, LOW); digitalWrite(LTPin, HIGH); //L-Stop digitalWrite(RCPin, LOW); digitalWrite(RTPin, HIGH); //R-Stop }void moveLeftForward(){ digitalWrite(LCPin, HIGH); digitalWrite(LTPin, LOW); //left wheel backward digitalWrite(RCPin, HIGH); digitalWrite(RTPin, HIGH); //right wheel forward }void moveRightForward(){ digitalWrite(RCPin, HIGH); digitalWrite(RTPin, LOW); //right wheel backward digitalWrite(LCPin, HIGH); digitalWrite(LTPin, HIGH); //left wheel forward }void moveLeftBackward(){ digitalWrite(LCPin, HIGH); digitalWrite(LTPin, HIGH); //left wheel forward digitalWrite(RCPin, HIGH); digitalWrite(RTPin, LOW); //right wheel backward }void moveRightBackward(){ digitalWrite(LCPin, HIGH); digitalWrite(LTPin, LOW); //left wheel backward digitalWrite(RCPin, HIGH); digitalWrite(RTPin, HIGH); //right wheel forward }