Hello, everyone! I've got some relatively finished code for controlling an led on pin 13 with serial. To use it, you just upload to your Arduino, open the serial monitor, and type 0 and 1 to turn the led on or off. You can also type a sequence of zeros and ones to make it blink in a certain pattern. "01010101110001110001110101010" blinks "SOS" in Morse code.
// LEDSerialToggle
// Turns on an LED on for one second, then off for one second, repeatedly.
// Based on DarthTater's Morse code for serial.
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int LEDPin = 13;
int incomingByte = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(LEDPin, OUTPUT);
Serial.begin(9600);
Serial.flush();
}
// Let's have it turn the LED on (or off) when it gets input:
void LEDToggle(int c){
switch(c)
{
case 1:
digitalWrite(LEDPin, HIGH); // This is the LED turning on when you press "1"
return;
case 0:
digitalWrite(LEDPin, LOW); // This is the LED turning off when you press "0"
return;
}
}
// the loop routine runs over and over again forever:
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
// read the incoming byte:
LEDToggle(incomingByte);
delay(400); // wait 0.4 seconds before going on to our next character.
}
}
If you want to download it, you can get it at Dropbox - Error - Simplify your life.