Go Down

Topic: Serial controlled led (Read 628 times) previous topic - next topic

Tvex

Jan 01, 2013, 12:07 am Last Edit: Jan 01, 2013, 01:21 am by Tvex Reason: 1
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: [Select]
// 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.
Device: Arduino Mega 2560 R3
Experience: Done blinking LEDs. Looking for something new.

CrossRoads

" 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.
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.

Tvex

#2
Jan 01, 2013, 01:20 am Last Edit: Jan 01, 2013, 01:31 am by Tvex Reason: 1
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!  8)
Device: Arduino Mega 2560 R3
Experience: Done blinking LEDs. Looking for something new.

Go Up