Created
April 12, 2013 08:47
-
-
Save BorisKourt/5370580 to your computer and use it in GitHub Desktop.
A small bit of code to gran data from Bluetooth via serial and update an LED state.
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
long previousPrint = 0; | |
long intervalPrint = 10000; | |
boolean red = false; | |
boolean blu = false; | |
boolean gre = false; | |
boolean all = false; | |
char val = 'a'; | |
void setup() { | |
Serial1.begin(115200); | |
while (!Serial1); | |
pinMode(3, OUTPUT); //red | |
pinMode(4, OUTPUT); //blue | |
pinMode(5, OUTPUT); //green | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
val = (char)Serial1.read(); | |
if (val == 'r') { | |
blu = false; | |
red = true; | |
gre = false; | |
all = false; | |
} | |
if (val == 'b') { | |
blu = true; | |
red = false; | |
gre = false; | |
all = false; | |
} | |
if (val == 'g') { | |
blu = false; | |
red = false; | |
gre = true; | |
all = false; | |
} | |
if (val == 'a') { | |
blu = false; | |
red = false; | |
gre = false; | |
all = true; | |
} | |
if (gre) { | |
digitalWrite(5, HIGH); | |
digitalWrite(4, LOW); | |
digitalWrite(3, LOW); | |
} | |
if (red) { | |
digitalWrite(3, HIGH); | |
digitalWrite(4, LOW); | |
digitalWrite(5, LOW); | |
} | |
if (blu) { | |
digitalWrite(4, HIGH); | |
digitalWrite(5, LOW); | |
digitalWrite(3, LOW); | |
} | |
if (all) { | |
if(currentMillis - previousPrint > intervalPrint) { | |
previousPrint = currentMillis; | |
analogWrite(3, random(0, 255)); | |
analogWrite(4, random(0, 255)); | |
analogWrite(5, random(0, 255)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment