Pages: [1]   Go Down
Author Topic: Serial controlled led  (Read 525 times)
0 Members and 1 Guest are viewing this topic.
If I'm online, I'm in front of the computer. If else, I'm not.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
I do not help for personal status or gain. The karma button is there because it's pretty.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
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 https://www.dropbox.com/sh/dnunzuk1q3q6hsw/ek_Lv2HzIF.
« Last Edit: December 31, 2012, 07:21:41 pm by Tvex » Logged

Device: Arduino Mega 2560 R3
Experience: Done blinking LEDs. Looking for something new.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 512
Posts: 26215
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

" delay(400); // wait a fourth of a second before going on to our next character."

This is 0.4 seconds, not 1/4 (250ms) of a second.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

If I'm online, I'm in front of the computer. If else, I'm not.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
I do not help for personal status or gain. The karma button is there because it's pretty.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, glad you caught that!
That is why I use Dropbox, so I can update my code, typos, etc. Without actually having to redo and re-upload the whole thing again.
Hey, your designs are pretty cool!  smiley-cool
« Last Edit: December 31, 2012, 07:31:36 pm by Tvex » Logged

Device: Arduino Mega 2560 R3
Experience: Done blinking LEDs. Looking for something new.

Pages: [1]   Go Up
Jump to: