Program Help

Guys, I'm still experimenting with arduino and make this sketch to light an led when l is pressed( and released) and then switch off when it is pressed again. On the arduino simulator it works as intended but when I use the arduino nothing happens pls help asap :slight_smile:
Thanks in advance :slight_smile:

This is the program : int ledPin = 13;
boolean ledState = LOW;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, ledState);
int key;
if (Serial.available() >0)
{

key = Serial.read();
Serial.print("Key Pressed: ");
Serial.println(key, DEC);
if (key == 108)
{
if (ledState == HIGH)
{
boolean ledState = LOW;
digitalWrite(ledPin, ledState);
Serial.println("LED is Off");
}
else
{
boolean ledState = HIGH;
digitalWrite(ledPin, ledState);
Serial.println("LED is On");
}
}

}
}

Tell me if I have mistakes please

How to use this forum

Read this before posting a programming question

Code tags please.

What is 108? Why are you checking for it?

boolean ledState = LOW;   // <----- first variable

...

        if (ledState == HIGH)
        {
          boolean ledState = LOW;    // <----- second variable
          digitalWrite(ledPin, ledState);
          Serial.println("LED is Off");
        }
        else
        {
          boolean ledState = HIGH;     // <----- third variable

You have three ledState variables there, not one.

Leave "boolean" off the second two. That is declaring a new variable, not assigning to an existing one.

108 is the ascii decimal value of the letter l which is the letter that i want the led to turn on when it is pressed. Thanks for your reply

I found the mistake it was that I confused = with ==. Thank You for your help :slight_smile:

What about the numerous different variables called ledState ? Have you corrected that mistake ?

Yes that one was also a mistake that I made when the sketch was not working because I though that was the reason of the sketch not working as intended, and after I tried it with no result I forgot to arrange the changes.

108 is the ascii decimal value of the letter l which is the letter that i want the led to turn on when it is pressed

Will you remember that in six weeks time?
No.
So, write it: if (key == 'l')

Thanks for the interest but I wrote the function of the sketch in the title so I will know what 108 stand for, but I will take your advice better safe than sorry. :slight_smile:

I just saw this

On the arduino simulator it works as intended

.
That's worrying.

I think that even the simulator does not work as intended lol :slight_smile:

AWOL:
That's worrying.

Not surprising however. :slight_smile:

jusspit:
Thanks for the interest but I wrote the function of the sketch in the title so I will know what 108 stand for, but I will take your advice better safe than sorry. :slight_smile:

So your instructions tell you to press the key corresponding to 108 on the keyboard, is that it? Try to think ahead a few years.

I wrote the function of the sketch in the title so I will know what 108 stand for

Frankly, I don't believe that.

AWOL:
I just saw this

On the arduino simulator it works as intended

.
That's worrying.

I think what's worrying is that an Arduino 'simulator' (emulator?) costs more than an actual Arduino...!??

The list price for Simulator for Arduino will be around $50 for v1.00. For the pre-release stage, the Pro version is available at a discount which will be increased gradually as milestones are reached, and a discount renewal is available when the support period page expires. This rewards early adopters.

Oh yes, spend $50 to save paying for an Arduino! Cool. And get the wrong results.

I'd be interested to read these instructions. Of all letters to choose to press, that wouldn't have been my first choice.

Is it: l, 1 or I ? (all different)

I chosen l for lights and my program was made so that to see if I can apply the same concept for a bigger project but thanks for your interest

jusspit:
I chosen l for lights and my program was made so that to see if I can apply the same concept for a bigger project but thanks for your interest

Yeah, I had to lookup "ASCII character 108" to find it is lower case "L"

Figured it probably stood for "LED" or "Light" or maybe "Let it work, please!"
:sweat_smile: