Pages: [1]   Go Down
Author Topic: Interact and control a LED using the Serial monitor  (Read 907 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I want to control a LED, set it to either HIGH or LOW, by sending values through the serial monitor.

Here is one example of something similar, but I dont know what the Arduino code he uses looks like:
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=37469

I tried with this code, but it does not work:

Code:

const int ledPin     = 9;
const int ledPin2   = 10;

void setup(){
  Serial.begin(9600);   
 
}

void loop () {

 
     if (Serial.available()>0) {
      //int d = 0;
      int d= Serial.read();
            if ( d == 10 )
            {
              digitalWrite(ledPin, HIGH);         
              digitalWrite(ledPin2, HIGH);
            }
            else if ( d == 20 )
            {
              digitalWrite(ledPin, LOW);
              digitalWrite(ledPin2, LOW);
            }
           
          }
}
   




I simply plugg the Arduino into my computer, open the serial monitor and put "1" into the send text field and send it. RX LED on the Arduino flashes when I do this, but the LED does not go on. Anybody know what im doing wrong here?
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 596
Posts: 33278
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The serial monitor sends code in ASCII, so when you type 1 it actually sends a value of 49.
Look at the example in the arduino IDE for this under serial communication.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you!
Logged

Pages: [1]   Go Up
Jump to: