Skip to content

Instantly share code, notes, and snippets.

@alotaiba
Created January 24, 2011 16:20
Show Gist options
  • Save alotaiba/793461 to your computer and use it in GitHub Desktop.
Save alotaiba/793461 to your computer and use it in GitHub Desktop.
Arduino LED graph bar sketch, which receives the signals from serial port.
const int baudRate = 9600;
const int ledCount = 10;
int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
char msg = ' ';
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
Serial.begin(baudRate);
Serial.print("Hi! This is Arduino\n");
}
void loop() {
// loop over the LED array:
while (Serial.available() > 0) {
//ASCII to int conversion ex. 49(1) - 48(0) = 1
msg = Serial.read() - '0';
//Number between 0 = ASCII 48 and 9 ASCII 57
if ( msg >= 0 && msg < 10 ) {
int ledsLit = msg + 1;
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (thisLed + 1 <= ledsLit) {
digitalWrite(ledPins[thisLed], HIGH);
} else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment