Skip to content

Instantly share code, notes, and snippets.

@hbldh
Last active February 16, 2016 13:58
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 hbldh/a187622c3df92a4f2586 to your computer and use it in GitHub Desktop.
Save hbldh/a187622c3df92a4f2586 to your computer and use it in GitHub Desktop.
Sweet Chariot Code
int incomingByte; // a variable to read incoming serial data into
int pwm_a = 5; //PWM control for motor outputs 1 and 2
int pwm_b = 9; //PWM control for motor outputs 3 and 4
int dir_a = 4; //direction control for motor outputs 1 and 2
int dir_b = 8; //direction control for motor outputs 3 and 4
void setup() {
// Initialize serial communication on UART TX/RX
Serial.begin(9600);
// LED on Arduino
pinMode(13, OUTPUT);
// Set control pins to be outputs
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
}
void loop() {
// See if there's incoming serial data
if (Serial.available() > 0) {
// Read the oldest byte in the serial buffer
incomingByte = Serial.read();
// If it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(13, HIGH);
}
// If it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(13, LOW);
}
if (incomingByte == 'D') {
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
delay(1000);
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
}
if (incomingByte == 'F') {
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, HIGH);
}
if (incomingByte == 'R') {
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, LOW);
}
}
}
/* Function prototypes -------------------------------------------------------*/
int sendLightOn(String dummy);
int sendLightOff(String dummy);
int drive(String dummy);
int reverse(String dummy);
int forward(String dummy);
void setup()
{
// Initiate Serial Communication on UART TX/RX.
Serial1.begin(9600);
//Register all the Tinker functions
Spark.function("sendlighton", sendLightOn);
Spark.function("sendlightoff", sendLightOff);
Spark.function("drive", drive);
Spark.function("reverse", reverse);
Spark.function("forward", forward);
}
void loop()
{
}
int sendLightOn(String dummy)
{
Serial1.write('H');
return -1;
}
int sendLightOff(String dummy)
{
Serial1.write('L');
return -1;
}
int drive(String dummy)
{
Serial1.write('D');
return -1;
}
int reverse(String dummy)
{
Serial1.write('R');
return -1;
}
int forward(String dummy)
{
Serial1.write('F');
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment