Switch case with Serial interface

Hi all.

I'm running a super simple code and I can't understand what isn't working within it.

The purpose of the code is to turn a pin to HIGH when the 'q' key is pressed (and entered into the monitor), and then to turn the pin to LOW when the 'w' key is pressed. The default setting when any other key is pressed is to send a message that says "No command recognized."

As far as I can tell, the pin is responding appropriately and I am getting the "Turned off/on" statement, but when either 'q' or 'w' are pressed the default statement will appear in the monitor as well. If any other keys are pressed, two "No command recognized." statements come through.

What is causing this? And what other problems might I be running into?

(P.S For those wondering, the pin is connected to a transistor on a solenoid circuit almost identical to the one here: http://www.instructables.com/id/Controlling-solenoids-with-arduino/?ALLSTEPS)

void setup() {
  // put your setup code here, to run once:
pinMode(2, OUTPUT);

void loop() {
  // put your main code here, to run repeatedly:
if (Serial.available() > 0)
  int inByte = Serial.read();

  switch (inByte)
    case 'w':
      digitalWrite(2, LOW);
      Serial.println("Turned off");
    case 'q':
      digitalWrite(2, HIGH);
      Serial.println("Turned on");
      Serial.println("No command recognized.");


I realize its probably a simple mistake, but I'm stumped and appreciate the help. Thanks!

Check the bottom of your Serial Monitor - does it say No Line Ending, or something else (that will generate a Carriage Return and/or Line Feed)?

That was it. Thank you!

Would it be possible to get any input on the actual circuit? I’ve done everything as described in the tutorial and I keep running into the problem of engaging the solenoid circuit whether or not I run current to the base of the transistor. I know I’ve wired it exactly like in the schematic, and I’ve spent a few days on it. Again, any help would be appreciated.


Hi, Your frizty diagram is unreadable. Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What are you using for your transistor.

Tom.. :)

Sorry for the late reply, I had to get to a computer that I could draw on.

The transistor I am using is an NTE262, which is interchangeable with the TIP120 the tutorial used. The diode is NTE116, which is supposed to be interchangeable with the 1N4001 they used as well (the store only had one brand).

I am by no means an electrician, so the circuit I drew may be confusing. Basically, I reconstructed the circuit exactly as he had it in the colored png file I originally tried to post (I’ve attached a higher def jpeg). Then, when that didn’t work, I reconstructed the circuit according to my own understanding of how each component is supposed to work (the janky scheme I’ve attached).

I will try to post a photo of what I’ve done, as well.

The protection diode across the emitter - collector, you need to use the proper symbol, marking A and K on the schematic diagram

Fritzy has a schematic editor, use that to make things easy for you.

Tom… :slight_smile:

Your images aren't loading for me, Tom. Does the problem lie in the diode, then? I think I will post this question by itself in another thread.