switchState acting opposite

Hi All, I am completely new to Arduino and am a complete noob.

I am using the Arduino starter kit and on the project 2, Spaceship Interface, the lights and the switch are doing the opposite of what is intended.

At the "low" state (button not pressed), output 4 and 5 blink. When the button is pressed, output 3 lights up and 4 and 5 turn off.

To correct this, I changed switchState to HIGH and it works but I want to know what I did wrong to cause this mix up. Below with the code that makes it work as intended but with the altered switchState line Thanks!

int switchState = 0;

void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}

void loop(){
switchState = digitalRead(2);
// this is a comment
if(switchState == HIGH) {
// the button is not pressed

digitalWrite(3, HIGH); // green LED
digitalWrite(4, LOW); // red LED
digitalWrite(5, LOW); // red LED
}

else { // the button is pressed
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);

delay(250); // wait for a quarter second
// toggle the LEDs
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250); // wait for a quarter second

}
} // go back to the beginning of the loop

“ on the project 2”
Don’t know anything about project 2.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

Does the switch have a pullup or pulldown resistor so that it is in a known state when the switch is open. Do you know about the, optional, internal pullup on each digital pin enabled with the pinMode() function?

Sorry for the late reply. I had actually disconnected the wires but reconnected them and got the same result. I have included the photo.

For the Spaceship Interface project, pinMode 3 is the green LED and is on without the button pressed.

pinMode 4 and 5 are the red LEDs. When the button is pressed, green turns off and the red ones turn on and off alternately with 0.25 sec delay.

The pic shows the green LED on and the red LEDs off despite programming stating switchState == HIGH.

@groundFungus I have no idea about pullup or pulldown resistors.

Thanks for all your help.

edit: I don't see the photo on the post even though I see the ....{/img} on my edit. I also tried to upload on my Mac but the file type is not supported. Below is the direct link to google photo.

Google Photos

severos:
@groundFungus I have no idea about pullup or pulldown resistors.

Links were provided for that. Did you follow them?

At the "low" state (button not pressed), output 4 and 5 blink. When the button is pressed, output 3 lights up and 4 and 5 turn off.

This is backwards of normal. Normally a pressed button should read LOW. If you want active HIGH buttons then you need to include external pull-down resistors. Do you have those? How are they connected?

Read this: Gammon Forum : Electronics : Microprocessors : Switches tutorial and learn about pull-up and pull-down. They are incredibly important.

The pic shows the green LED on and the red LEDs off despite programming stating switchState == HIGH.

if(switchState == HIGH) {
  // the button is not pressed
 
  digitalWrite(3, HIGH); // green LED
  digitalWrite(4, LOW); // red LED
  digitalWrite(5, LOW); // red LED
}

This says if the switch reads HIGH turn the green led on and the red leds off. So is this not exactly what you said it is doing?

Also note that your comment here about button not pressed is opposite of what you described in your post. So which way is it actually wired? When you press the button, does it connect the pin to 5V or Ground?

@aarg

@Delta_G

Thank you both so much! I followed the pullup/pulldown links after your post and, they honestly weren't that much help. I even tried to enter code to change the input setting and it didn't change the behavior.

@Delta-G link to the Gammon Forum did it. In my setup, I mistakenly the button switch to ground and the button resistor to 5V. By reversing it, this resolved the issue. This is the first illustration on the Gammon link.

Again, thanks to everyone for helping me. This was a great start.

severos:
@Delta-G link to the Gammon Forum did it. In my setup, I mistakenly the button switch to ground and the button resistor to 5V. By reversing it, this resolved the issue. This is the first illustration on the Gammon link.

Then you completely misunderstood it and still did it backwards. This will work, but there are many good reasons to do it the other way.

Wire the button so that it connects to ground. Put the resistor back in your drawer, you don't need it. Use INPUT_PULLUP in pinMode instead of INPUT and then a pressed button should read LOW so test for LOW instead of HIGH as a pressed button.

The S3 switch in this schematic is how you want your switch wired to the Arduino.

Closing S3 places a GND (LOW) on Arduino pin D3.

@Delta_G
I appreciate your looking out for me but the context of this is that I am completely new and I am working from a starter kit that came with a projects book.

I did not write the code or design the use of the board, wires, resistors, buttons, etc. I simply tried to copy what was in the book and then figure out why it didn't work as intended. Figuring out how to correct the reversed switchState took longer than the project itself.

Hopefully, as I progress, I'll also figure out why I didn't need resistors each time or that I could have written something into the script to reverse the pinMode. Right now, I've just got to get through the beginner's guide.

@larryd
Thank you. I'll compare that guide to the way upcoming projects in the book design their circuits.

Ok sorry, if I see you making mistakes in the future I’ll just leave you to it. Some folks want to learn more, I’m just trying to help. If you don’t want to learn new things that’s cool. My bad.

@Delta_G

We all have to learn at our own pace. My pace is very slow. My original mistake was that I wired the button incorrectly and the 2 solutions that worked were to wire correctly (thanks to your link) or change the switchState setting despite the original code in the starter guide (my original solution).

You were also kind enough to introduce a different way to wire the button -- a topic different than the original post -- but I just wasn't ready for it.

Thanks yet again for all your help. Your help is always welcome but please don't feel any obligation. Have a nice day.

I keep a list of folks who don’t like my help or who want to keep doing things the hard way. I’ll just put you there and leave you to your mistakes in the future. Sorry for giving you information you weren’t ready for. To me more information is always a good thing. I can ignore what I don’t want. But if you like being in the dark then stay in the dark bud. I won’t stop you.

So, after I clearly stated you have no obligation to help me in the future, you make sure to point out that you put me on your "list."

That's clearly meant as intimidation, especially to someone new on the forum. Why would you do that?