Getting led to not blink

I am trying to get my led to be on or off, not blinking, which I think is caused by the loop, and I would like to know the proper way to do this (not a quick hack).

Here is my code so far.

int ledPin = 11;                     //Set led pin to 11
int last;
void setup(){
 pinMode(ledPin, OUTPUT);           //sets ledpin as output
 pinMode(13, OUTPUT);
 Serial.begin(9600);                //establish serial communication
 digitalWrite(13, LOW);
}

void loop(){
 while (Serial.available() == 0);   //continouly search for serial comm 
 int val = Serial.read();           //set val as integer from serial comm
 if (val == 1) {                    //if value is 1
   if (last == 0) {
   Serial.println("Led is On");
 }
   digitalWrite(ledPin, HIGH);      //turn on led
   last = 1;
 }
 else if (val == 0)                 //if value is 0
{ 
  if (last == 1) {
  Serial.println("Led is Off"); 
  }
  digitalWrite(ledPin, LOW);        //turn off led
  last = 0;
 }
}

Thanks for the help.

You probably meant to write

if (val == [color=red]'1'[/color]) {...}
else if (val == [color=red]'0'[/color]){...}

Please use code tags and indent your code (ctrl-T in the IDE)

I changed it, but it doesn't seem to have done anything.

Viper360:
I changed it, but it doesn't seem to have done anything.

Formatting doesn't change things. It just make figuring out what's going on a lot easier. And, if your aasking for help, its the polite and sensible thing to do.

-jim lee

jimLee:
Formatting doesn't change things. It just make figuring out what's going on a lot easier. And, if your aasking for help, its the polite and sensible thing to do.

-jim lee

I'm sorry, I am new here. When I said I changed it I meant I added the ' ' in my program. I formatted the original post as well. Could you explain what the ' ' is, does and means?

The '1' means the ascii code for symbol character 1 (when you type 1 in the arduino serial monitor, what gets received by Serial.read() is the ascii code of 1, not the quantity/number 1)

Which led is blinking by the way? because if you have that code with 0 not '0' and are really typing the character '0' or '1' in the serial monitor of the IDE then your code should do nothing. No blinking, no change... the only thing blinking would be the LEDs on the board for the serial communication

Viper360:
I'm sorry, I am new here. When I said I changed it I meant I added the ' ' in my program. I formatted the original post as well. Could you explain what the ' ' is, does and means?

Oh! Took me a bit to realize what you were asking.

in c code.. int is an integer, a counting number. so..

int lastVal; is a holder for an integer. And not really what you want. P.S. you never initialized it anyway.

But, when you get stuff from the serial port from the terminal, it doesn't don't come as integers, it comes in as characters. Bytes of text. the ASCII for the character 1 is actually 31.

In c code, characters are type char. so..

char val; would be a holder for a char. And, if you want to tell the compiler that something should be thought of as a char, you put ' ' around it. like 'x'.

Hence, where he says if (val == '1') he's saying, take the char that came in and compare it to the char '1' and that's how you can compare chars.

Note strings, arrays of chars, are different. == does not compare them in a meaningful way and to denote something as a string is "a string" not 'x' a char.

Hope this helps!

-jim lee

@jimLee actually:

  • last being a global variable, it is initialized to 0 by the compiler automatically. So that part is implicit and kinda right. (Agree best practice for code readability would be to declare it and set it to 0 with a comment)

  • char val is usually indeed a good idea to hold a char, but I would advise against this because val is set by calling Serial.read() which returns an int, not a char. That int will be -1 if a read error occurred otherwise the LSB will hold indeed the byte read; also char are signed so that might introduce weird behavior if you read binary. so while I see tons of codes ignoring this, I would say the best practice for good code behavior would be indeed to read as an int, check against -1 and if read was fine, then use the LSB which will work fine through auto casting if you do things like (val == '1') or explicitly cast ((char) val == '1')

Hi,
Your code with an extra Serial.prints to show difference between the two values, int and char.

int ledPin = 11;                     //Set led pin to 11
int last;
int val;
int val1;

void setup() {
  pinMode(ledPin, OUTPUT);           //sets ledpin as output
  pinMode(13, OUTPUT);
  Serial.begin(9600);                //establish serial communication
  digitalWrite(13, LOW);
}

void loop() {
  while (Serial.available() == 0);   //continouly search for serial comm
  val = Serial.read();           //set val as integer from serial comm
  Serial.print(val);
  Serial.print("   ");
  Serial.println((char) val);
  if ((char) val == '1') {                    //if value is 1
    if (last == 0) {
      Serial.println("Led is On");

    }
    digitalWrite(ledPin, HIGH);      //turn on led
    last = 1;
  }
  else if ((char) val == '0')                 //if value is 0
  {
    if (last == 1) {
      Serial.println("Led is Off");

    }
    digitalWrite(ledPin, LOW);        //turn off led
    last = 0;
  }
}

Tom… :slight_smile:

Thanks for the amazing help all of you, I think I'm getting the difference between the different types of variables.

My last question I guess would be, when it receives the correct input from the serial port the led turns on and off like should, but it is blinking, not just on.

And it's a resistor and led connected to pin 11, I have the on board led at pin 13 turned off.

what type of LED do you have? is that the auto-blinking type?

J-M-L:
what type of LED do you have? is that the auto-blinking type?

Looking at the packaging, it would seem so, 2.4 hz, I feel really dumb lol, I didn't mean to buy an auto blinking one. Thanks for all the help guys!

LOL :slight_smile: