Skip to content

Instantly share code, notes, and snippets.

@mithi
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithi/f3c3f60ca22981d87bbd to your computer and use it in GitHub Desktop.
Save mithi/f3c3f60ca22981d87bbd to your computer and use it in GitHub Desktop.
testing the bluetooth module with sparki
#include <Sparki.h>
void showServoCommands(){
Serial1.println("...");
Serial1.println("ready to receive orders!");
Serial1.println("enter 1, 2, or 3 to face left, front, or right.");
}
int userServoCommand(){
while (!Serial1.available());
int inByte = Serial1.read();
Serial1.flush();
Serial1.print("command: ");
Serial1.println((char)inByte);
Serial1.println("busy... please wait... ");
return inByte;
}
void obeyServoCommand(char inByte){
switch(inByte){
case '1': sparki.servo(SERVO_RIGHT); sparki.RGB(RGB_BLUE); break;
case '2': sparki.servo(SERVO_CENTER); sparki.RGB(RGB_GREEN); break;
case '3': sparki.servo(SERVO_LEFT); sparki.RGB(RGB_RED); break;
default: Serial1.println("invalid!"); break;
}
delay(500);
}
int distanceReading(){
int dist;
while (true){
dist = sparki.ping();
if (dist != -1){
break;
}
}
return dist;
}
void showDistanceReading(int dist){
Serial1.print("distance detected: ");
Serial1.print(dist);
Serial1.println(" cm");
Serial1.println("busy... please wait... ");
}
void setup(){
Serial1.begin(9600);
}
void loop(){
showServoCommands();
int inByte = userServoCommand();
obeyServoCommand((char)inByte);
int dist = distanceReading();
showDistanceReading(dist);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment