Skip to content

Instantly share code, notes, and snippets.

@tyggerjai
Created December 9, 2014 01:05
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 tyggerjai/8a6438c62c390041ec7e to your computer and use it in GitHub Desktop.
Save tyggerjai/8a6438c62c390041ec7e to your computer and use it in GitHub Desktop.
/* ************
* Simple code to listen for serial data and turn LEDs on appropriately.
* I've split the code into "walk" and "turn" functions to give an idea
* of how to use it for a hexapod, but we just use LEDs hooked up to
* pins 6 and 7 for this.
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2014 Jai Cornes (tyggerjai@gmail.com). All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*************** */
int control_byte;
char ser_data;
#define LED1 6
#define LED2 7
void stop_walking(){
// Turn off both LEDs
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
void start_walking(){
// Turn on both LEDs
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
}
void turn( int direction){
if (direction == 1){
// Turn on one LED
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
}else if (direction == 2){
//Turn on the other LED
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
}
}
void setup(){
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
stop_walking()
Serial.begin(9600);
}
void loop(){
// Check for incoming data
if (Serial.available() > 0 ){
// The PC ultimately sends strings "1", not byte values
// So we need to convert back to a number
ser_data = Serial.read();
control_byte = atoi(&ser_data);
// If it's 1 or 2, turn appropriately
if (control_byte == 1 || control_byte == 2){
turn(control_byte);
}else if (control_byte == 3){
// Start
start_walking();
}else{
// Stop
stop_walking();
}
}
// Feedback on what we think we got
Serial.println(control_byte, DEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment