Created
August 12, 2018 03:51
-
-
Save reefwing/3b427900539bf499bf9afe271a114f56 to your computer and use it in GitHub Desktop.
Control DFRobot Devastator Tank via a Romeo BLE (Uno equivalent) and the Arduino IDE Serial Monitor.
This file contains 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
/* | |
* SerialControl: Control a DFRobot Devastator tank via a Romeo BLE | |
* (Uno equivalent) and the Arduino IDE Serial Monitor. | |
* Based on the DFRobot Devastator Tank Mobile Platform sample | |
* code written by Phoebe. | |
* | |
* | |
* Version 0.1, August, 2018 | |
* Copyright 2018 David Such | |
* | |
*/ | |
#define NO_DATA -1 | |
const byte M1_DIRECTION = 4; // M1 Direction Control | |
const byte M1_SPEED = 5; // M1 Speed Control | |
const byte M2_SPEED = 6; // M2 Speed Control | |
const byte M2_DIRECTION = 7; // M2 Direction Control | |
// Motor Control | |
////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Functions | |
void stop() | |
{ | |
digitalWrite(M1_SPEED, LOW); | |
digitalWrite(M1_DIRECTION, LOW); | |
digitalWrite(M2_SPEED, LOW); | |
digitalWrite(M2_DIRECTION, LOW); | |
} | |
void forward(int m1DutyCycle, int m2DutyCycle) | |
{ | |
analogWrite (M1_SPEED, m1DutyCycle); | |
digitalWrite(M1_DIRECTION, HIGH); | |
analogWrite (M2_SPEED, m2DutyCycle); | |
digitalWrite(M2_DIRECTION, HIGH); | |
} | |
void back (int m1DutyCycle, int m2DutyCycle) | |
{ | |
analogWrite (M1_SPEED, m1DutyCycle); | |
digitalWrite(M1_DIRECTION, LOW); | |
analogWrite (M2_SPEED, m2DutyCycle); | |
digitalWrite(M2_DIRECTION, LOW); | |
} | |
void left (int m1DutyCycle, int m2DutyCycle) | |
{ | |
analogWrite (M1_SPEED, m1DutyCycle); | |
digitalWrite(M1_DIRECTION, LOW); | |
analogWrite (M2_SPEED, m2DutyCycle); | |
digitalWrite(M2_DIRECTION, HIGH); | |
} | |
void right (int m1DutyCycle, int m2DutyCycle) | |
{ | |
analogWrite (M1_SPEED, m1DutyCycle); | |
digitalWrite(M1_DIRECTION, HIGH); | |
analogWrite (M2_SPEED, m2DutyCycle); | |
digitalWrite(M2_DIRECTION, LOW); | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
void setup(void) | |
{ | |
// Serial Monitor | |
Serial.begin(115200); | |
Serial.println("Romeo BLE Device"); | |
Serial.println("Initializing..."); | |
Serial.println("Serial connected - awaiting command (w, a, s, d, t, x): "); | |
// PIN Definitions | |
pinMode(M1_DIRECTION, OUTPUT); | |
pinMode(M1_SPEED, OUTPUT); | |
pinMode(M2_DIRECTION, OUTPUT); | |
pinMode(M2_SPEED, OUTPUT); | |
// Initial State - stopped | |
digitalWrite(M1_SPEED, LOW); | |
digitalWrite(M2_SPEED, LOW); | |
} | |
void loop(void) | |
{ | |
if (Serial.available()) { | |
// Get the number of bytes (characters) available for reading from the serial port. | |
// This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes). | |
char incomingByte = Serial.read(); | |
if (incomingByte != NO_DATA) | |
{ | |
switch(incomingByte) | |
{ | |
case 'w': | |
Serial.println("Forward Maximum Speed."); | |
forward(255, 255); | |
break; | |
case 's': | |
Serial.println("Backward Maximum Speed."); | |
back(255, 255); | |
break; | |
case 'a': | |
Serial.println("Turning Left."); | |
left(100, 100); | |
break; | |
case 'd': | |
Serial.println("Turning Right."); | |
right(100, 100); | |
break; | |
case 't': | |
Serial.println("Hello"); | |
break; | |
case 'x': | |
Serial.println("Stopped."); | |
stop(); | |
break; | |
default: | |
Serial.println("Unknown Command..."); | |
Serial.println("Valid Commands are: w = forward, a = left, s = back, d = right, t = test message, x = stop."); | |
break; | |
} | |
} | |
else stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment