Created
July 22, 2019 23:12
-
-
Save bboyho/5e5310c9ac5f30b7f35a53a6ce55b068 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Reading a serial ASCII-encoded string. | |
This sketch demonstrates the Serial parseInt() function. | |
It looks for an ASCII string of comma-separated values. | |
It parses them into ints, and uses those to fade an RGB LED. | |
Circuit: Common-Cathode RGB LED wired like so: | |
- red anode: digital pin 3 | |
- green anode: digital pin 5 | |
- blue anode: digital pin 6 | |
- cathode: GND | |
created 13 Apr 2012 | |
by Tom Igoe | |
modified 14 Mar 2016 | |
by Arturo Guadalupi | |
This example code is in the public domain. | |
*/ | |
// pins for the LEDs: | |
const int redPin = 9; | |
const int greenPin = 10; | |
const int bluePin = 11; | |
void setup() { | |
// initialize serial: | |
Serial.begin(9600); | |
Serial.println("Test RGB"); | |
// make the pins outputs: | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
} | |
void loop() { | |
// if there's any serial available, read it: | |
while (Serial.available() > 0) { | |
// look for the next valid integer in the incoming serial stream: | |
int red = Serial.parseInt(); | |
// do it again: | |
int green = Serial.parseInt(); | |
// do it again: | |
int blue = Serial.parseInt(); | |
// look for the newline. That's the end of your sentence: | |
//if (Serial.read() == '\n') { | |
// constrain the values to 0 - 255 and invert | |
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);" | |
//red = 255 - constrain(red, 0, 255); | |
//green = 255 - constrain(green, 0, 255); | |
//blue = 255 - constrain(blue, 0, 255); | |
// fade the red, green, and blue legs of the LED: | |
analogWrite(redPin, red); | |
analogWrite(greenPin, green); | |
analogWrite(bluePin, blue); | |
// print the three numbers in one string as hexadecimal: | |
Serial.print(red); | |
Serial.print(","); | |
Serial.print(green); | |
Serial.print(","); | |
Serial.println(blue); | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment