Last active
February 28, 2019 10:46
-
-
Save suadanwar/e0d773591836849a32687a6f05463b62 to your computer and use it in GitHub Desktop.
This is a sample code for Bluetooth Mobile Robot with Maker Drive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define BT_TX 4 | |
| #define BT_RX 2 | |
| #include "SoftwareSerial.h" | |
| SoftwareSerial BTSerial(BT_TX, BT_RX); // Maker UNO RX, TX | |
| #include "CytronMotorDriver.h" | |
| // Configure the motor driver. | |
| CytronMD motor1(PWM_PWM, 3, 9); // PWM 1A = Pin 3, PWM 1B = Pin 9. | |
| CytronMD motor2(PWM_PWM, 10, 11); // PWM 2A = Pin 10, PWM 2B = Pin 11. | |
| #define BUTTON 2 | |
| #define PIEZO 8 | |
| #define NOTE_G4 392 | |
| #define NOTE_C5 523 | |
| #define NOTE_G5 784 | |
| #define NOTE_C6 1047 | |
| int btConnect[] = {NOTE_G5, NOTE_C6}; | |
| int btConnectNoteDurations[] = {12, 8}; | |
| int btDisconnect[] = {NOTE_C5, NOTE_G4}; | |
| int btDisconnectNoteDurations[] = {12, 8}; | |
| #define playBtConnectMelody() playMelody(btConnect, btConnectNoteDurations, 2) | |
| #define playBtDisconnectMelody() playMelody(btDisconnect, btDisconnectNoteDurations, 2) | |
| boolean BTConnect = false; | |
| char inChar; | |
| String inString; | |
| void setup() | |
| { | |
| pinMode(BUTTON, INPUT_PULLUP); | |
| Serial.begin(9600); | |
| BTSerial.begin(9600); | |
| delay(1000); | |
| } | |
| void loop() | |
| { | |
| if (BTSerial.available()) { | |
| if (BTConnect == false) { | |
| BTConnect = true; | |
| playBtConnectMelody(); | |
| } | |
| inString = ""; | |
| while (BTSerial.available()) { | |
| inChar = BTSerial.read(); | |
| inString = inString + inChar; | |
| } | |
| Serial.println(inString); | |
| if (inString == "#b=0#") { | |
| robotStop(); | |
| } | |
| else if (inString == "#b=9#" || | |
| inString == "#b=19#" || | |
| inString == "#b=29#" || | |
| inString == "#b=39#" || | |
| inString == "#b=49#") { | |
| robotBreak(); | |
| } | |
| else if (inString == "#b=1#") { | |
| robotForward(); | |
| } | |
| else if (inString == "#b=2#") { | |
| robotReverse(); | |
| } | |
| else if (inString == "#b=3#") { | |
| robotTurnLeft(); | |
| } | |
| else if (inString == "#b=4#") { | |
| robotTurnRight(); | |
| } | |
| else if (inString.startsWith("+DISC")) { | |
| BTConnect = false; | |
| delay(1000); | |
| while (BTSerial.available()) { | |
| BTSerial.read(); | |
| } | |
| playBtDisconnectMelody(); | |
| } | |
| } | |
| } | |
| void playMelody(int *melody, int *noteDurations, int notesLength) | |
| { | |
| pinMode(PIEZO, OUTPUT); | |
| for (int thisNote = 0; thisNote < notesLength; thisNote++) { | |
| int noteDuration = 1000 / noteDurations[thisNote]; | |
| tone(PIEZO, melody[thisNote], noteDuration); | |
| int pauseBetweenNotes = noteDuration * 1.30; | |
| delay(pauseBetweenNotes); | |
| noTone(PIEZO); | |
| } | |
| } | |
| void robotStop() | |
| { | |
| motor1.setSpeed(0); // Motor 1 stops. | |
| motor2.setSpeed(0); // Motor 2 stops. | |
| } | |
| void robotForward() | |
| { | |
| motor1.setSpeed(200); // Motor 1 runs forward. | |
| motor2.setSpeed(200); // Motor 2 runs forward. | |
| } | |
| void robotReverse() | |
| { | |
| motor1.setSpeed(-200); // Motor 1 runs backward. | |
| motor2.setSpeed(-200); // Motor 2 runs backward. | |
| } | |
| void robotTurnLeft() | |
| { | |
| motor1.setSpeed(-200); // Motor 1 runs forward. | |
| motor2.setSpeed(200); // Motor 2 runs backward. | |
| } | |
| void robotTurnRight() | |
| { | |
| motor1.setSpeed(200); // Motor 1 runs backward. | |
| motor2.setSpeed(-200); // Motor 2 runs forkward. | |
| } | |
| void robotBreak() | |
| { | |
| motor1.setSpeed(0); // Motor 1 stops. | |
| motor2.setSpeed(0); // Motor 2 stops. | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment