Created
September 6, 2020 19:19
-
-
Save aldajo92/b70afcbc55ad98e52f0d70a7d07a0b8a to your computer and use it in GitHub Desktop.
Zumo V1.2 Serial Communication
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
#include <ZumoShield.h> | |
// format: | |
// <vel_l>;<vel_r> | |
// input example: | |
// 100;100 | |
ZumoMotors motors; | |
String inputString = ""; | |
bool stringComplete = false; | |
void setup() { | |
Serial.begin(9600); | |
inputString.reserve(200); | |
} | |
void moveF(int vel_l, int vel_r) { | |
motors.setLeftSpeed(vel_l); | |
motors.setRightSpeed(vel_r); | |
} | |
void logVel(int vel_l, int vel_r){ | |
Serial.print("vel_l: "); | |
Serial.println(vel_l); | |
Serial.print("vel_r: "); | |
Serial.println(vel_r); | |
} | |
void parseCommand(String com) { | |
String part1; | |
String part2; | |
part1 = com.substring(0, com.indexOf(";")); | |
part2 = com.substring(com.indexOf(";") + 1); | |
logVel(part1.toInt(), part2.toInt()); | |
moveF(part1.toInt(), part2.toInt()); | |
} | |
void loop() { | |
if (stringComplete) { | |
Serial.print("input: " + inputString); | |
parseCommand(inputString); | |
inputString = ""; | |
stringComplete = false; | |
} | |
} | |
void serialEvent() { | |
while (Serial.available()) { | |
char inChar = (char)Serial.read(); | |
inputString += inChar; | |
if (inChar == '\n') { | |
stringComplete = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment