Skip to content

Instantly share code, notes, and snippets.

@bassdread
Last active August 29, 2015 14:11
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 bassdread/952e9df9d820c16841f3 to your computer and use it in GitHub Desktop.
Save bassdread/952e9df9d820c16841f3 to your computer and use it in GitHub Desktop.
Analog Dial Control Code
/*
I started with the sample from this guy
Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo output_dial; // create servo object to control a servo
Servo input_dial; // twelve servo objects can be created on most boards
boolean stringComplete = false;
String inputString = "";
int led_green_input = 2;
int led_blue_input = 3;
int led_red_input = 4;
int led_green_output = 6;
int led_blue_output = 7;
int led_red_output = 8;
void setup()
{
Serial.begin(9600);
input_dial.attach(9); // left
output_dial.attach(10); // right
pinMode(led_green_input, OUTPUT);
pinMode(led_blue_input, OUTPUT);
pinMode(led_red_input, OUTPUT);
pinMode(led_green_output, OUTPUT);
pinMode(led_blue_output, OUTPUT);
pinMode(led_red_output, OUTPUT);
output_dial.write(135); // reset to left side of the dial
input_dial.write(145); // reset to left side of the dial
// set green as LED colour default
digitalWrite(led_red_input, LOW);
digitalWrite(led_blue_input, LOW);
digitalWrite(led_green_input, HIGH);
digitalWrite(led_red_output, LOW);
digitalWrite(led_blue_output, LOW);
digitalWrite(led_green_output, HIGH);
}
void loop()
{
if (stringComplete) {
inputString.replace("\n","");
int len = inputString.length();
String servo = inputString.substring(0,1);
String colour = inputString.substring(2,3);
String movement = inputString.substring(4,len);
Serial.println(stringComplete);
// limit how far we move the meter to make suer we don't
// damage the needle on the side of the casing.
// right dial
if (servo.toInt() == 1 && movement.toInt() <= 135 && movement.toInt() >= 30){
output_dial.write(movement.toInt());
}
// left dial
if (servo.toInt() == 0 && movement.toInt() <= 145 && movement.toInt() >= 55){
input_dial.write(movement.toInt());
}
if (servo == "1"){
if (colour.toInt() == 0){
digitalWrite(led_red_output, LOW);
digitalWrite(led_blue_output, LOW);
digitalWrite(led_green_output, HIGH);
}
if (colour.toInt() == 1){
digitalWrite(led_red_output, LOW);
digitalWrite(led_blue_output, HIGH);
digitalWrite(led_green_output, LOW);
}
if (colour.toInt() == 2){
digitalWrite(led_blue_output, LOW);
digitalWrite(led_red_output, HIGH);
digitalWrite(led_green_output, LOW);
}
}
if (servo == "0"){
if (colour.toInt() == 0){
digitalWrite(led_red_input, LOW);
digitalWrite(led_blue_input, LOW);
digitalWrite(led_green_input, HIGH);
}
if (colour.toInt() == 1){
digitalWrite(led_red_input, LOW);
digitalWrite(led_blue_input, HIGH);
digitalWrite(led_green_input, LOW);
}
if (colour.toInt() == 2){
digitalWrite(led_blue_input, LOW);
digitalWrite(led_red_input, HIGH);
digitalWrite(led_green_input, LOW);
}
}
// clear the string:
inputString = "";
stringComplete = false;
}
delay(1000);
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment