-
-
Save kitroed/1730249 to your computer and use it in GitHub Desktop.
Arduino Experiment
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
/* | |
* Set LED brightness by serial write | |
* An Arduino experiment | |
* by Brian Morton | |
*/ | |
int ledPin = 9; | |
int brightness = 0; | |
void setup() { | |
pinMode(ledPin, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("Waiting for input..."); | |
} | |
char buffer[3]; | |
int x; | |
void loop() { | |
if (Serial.available() > 0) { | |
delay(5); // make sure we have all the data | |
// Buffer a possible 3 digits for our integer | |
for (int i = 0; i < 3; i++) { | |
buffer[i] = Serial.read(); | |
} | |
Serial.flush(); | |
x = atoi(buffer); | |
Serial.print("Setting brightness to: "); | |
Serial.println(x); | |
brightness = x; | |
} | |
analogWrite(ledPin, brightness); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment