Skip to content

Instantly share code, notes, and snippets.

@KMurphs
Last active July 6, 2022 21:12
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 KMurphs/5710d8a79f89a3c4a6d0c5cb985cd905 to your computer and use it in GitHub Desktop.
Save KMurphs/5710d8a79f89a3c4a6d0c5cb985cd905 to your computer and use it in GitHub Desktop.
Arduino Update LED
/*
  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 through 220 ohm resistor
  - green anode: digital pin 5 through 220 ohm resistor
  - blue anode: digital pin 6 through 220 ohm resistor
  - cathode: GND

  created 13 Apr 2012
  by Tom Igoe
  modified 14 Mar 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/ReadASCIIString
*/

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(LED_BUILTIN, 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 led = Serial.parseInt();

    // look for the newline. That's the end of your sentence:
    if (Serial.read() == '\n') {
      digitalWrite(LED_BUILTIN, led && 0x01 ? HIGH : LOW);
      Serial.println(led && 0x01 ? "LED STATE: ON" : "LED STATE: OFF");
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment