Skip to content

Instantly share code, notes, and snippets.

@kitroed
Forked from bmorton/arduino.pde
Created February 3, 2012 13:48
Show Gist options
  • Save kitroed/1730249 to your computer and use it in GitHub Desktop.
Save kitroed/1730249 to your computer and use it in GitHub Desktop.
Arduino Experiment
/*
* 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