Basic Code.

To me,

This should not blink on pin 13 unless I type 111 or 222. Currently it blinks at the timing set by the delay as soon as it fires up. It does respond when I type 111 or 222. However as stated Im wondering why its blinking with no input from the serial monitor... thanks in advance!

int led13 = 13;


void setup()
{
  pinMode(led13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int input = (Serial.parseInt());
  if (input == 111)
  {
    digitalWrite(led13, HIGH);
    delay(3300);
    digitalWrite(led13, LOW);
    delay(3300);
  }

  else if (input = 222)
  {
    digitalWrite(led13, HIGH);
    delay(3300);
    digitalWrite(led13, LOW);
    delay(3300);
  }

}

if (input = 222)basic mistake

  else if (input = 222)

Look carefully at this line …

I tried that just now Groove. Still blinking and now unresponsive to serial input. idk what im doing wrong yet. Thanks though.

int led13 = 13;


void setup()
{
  pinMode(led13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int input = (Serial.parseInt());
  if (input == 111)
  {
    digitalWrite(led13, HIGH);
    delay(3300);
    digitalWrite(led13, LOW);
    delay(3300);
  }

  if (input = 222)
  {
    digitalWrite(led13, HIGH);
    delay(3300);
    digitalWrite(led13, LOW);
    delay(3300);
  }

}
  if (input = 222)

Still a very basic mistake.

idk what im doing wrong yet.

Did you do as I suggested an look carefully at that line of code ? Is it different to the test for 111 ?

Hint: if you treat programming as a massive game of "spot-the-difference", you won't go far wrong.

Yes 111 by itself seems to act accordingly?

    delay(3300);
    digitalWrite(led13, LOW);
    delay(3300);

Unresponsive, you say? About six and a half seconds worth of unresponsive?

Dang. I hate that it was a typo. Sorry guys. I see now why you guys where expecting me to figure it out. ill try not to let that happen often. Thanks.

Hello,

I am using this bit of code:

int led13 = 13;


void setup()
{
  pinMode(led13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int input = (Serial.parseInt());
  if (input == 111)
  {
    digitalWrite(led13, HIGH);
    delay(30);
    digitalWrite(led13, LOW);
    delay(30);
  }

  if (input = 222)
  {
    digitalWrite(led13, HIGH);
    delay(30);
    digitalWrite(led13, LOW);
    delay(30);
  }

}

… and I am wondering and thinking the answer might be no.
With this code I am sure you can see. When I input 111 or 222
into my serial monitor. It turns 13 on and off real fast. Works fine.
However I am wondering and thinking not. Is there a way to go
from toggle to momentary? If so I would like to learn how…

Thanks.

By momentary do you mean when 111 is entered, the light turns on and the next time 111 is entered, the light turns off? If not, please describe what you mean by "momentary" and "toggle".

^ ditto..

I am not clear of your desired outcome.

If you mean by typing '111' into the serial monitor the led turns on.. and stays on for 3 seconds.. then automatically turns off (without the need for any button event/interaction)..

then yes. for sure..

Some things I noticed.

1) if (input = 222) should be if (input == 222)

2) by adding a debug print at the end of your code like below...

Serial.print("input: ");
  Serial.println(input);

you can see that the code is continuously inputing "222" Adding print outs to your code is a great way to see what is going on and problem solve.

Here is your code as is, except with the debug printing line.

int led13 = 13;


void setup()
{
  pinMode(led13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int input = (Serial.parseInt());
  if (input == 111)
  {
    digitalWrite(led13, HIGH);
    delay(30);
    digitalWrite(led13, LOW);
    delay(30);
  }

  if (input = 222)
  {
    digitalWrite(led13, HIGH);
    delay(30);
    digitalWrite(led13, LOW);
    delay(30);
  }
  Serial.print("input: ");
  Serial.println(input);
}

First try this code with the prints so you can see what is happening . Then correct your if statement and see what happens. When you get it working simply comment out or delete the printouts.

If you want your code to wait for you to input add...while (Serial.available() == 0) {} above your parse.

Code was just a typo. Um Toggle: = button press ---> stays on

button press ---> stays off

Momentary = button press ---> on/off following button position.

Thanks!

jgreene33: It turns 13 on and off real fast. Works fine. However I am wondering and thinking not. Is there a way to go from toggle to momentary? If so I would like to learn how...

Another way I use to understand what is going on is to take out a piece of paper and go through your code line by line. When a variable changes, change it on your scratch paper. When you get to the bottom of the loop go back through the loop again with the values you are tracking.

You will see that if you enter "111" all you are doing is turning you LED on once, then back off. Then your code finishes the loop, and when it starts over, it changes "input" to whatever is waiting in serial. a "0" in this case. So it will bypass both of your IF statements and go back to get another "0" which does nothing to your LEDs, until you enter something.

Ya thanks but anyone know about the question I was asking? That great and all but I dont see the answer so far. Thanks.

@jgreene33, please do not cross-post. Threads merged.

jgreene33: Ya thanks but anyone know about the question I was asking? That great and all but I dont see the answer so far. Thanks.

I tried to help you discover it. You didn't even have time to try what I suggested, or soak it in. If you expect to post your desires and get responses of working sketches, you will not learn anything, and it will be a very bumpy ride.

So you want the light to be on IF the button is pushed or ELSE turn off when the button is released?

Edit: I don't know where I got buttons. Oops. Thanks, birddog.