Using a momentary switch with an arduino

I purchased an arduino a little while ago and got the starter kit with it.
It comes with most of the stuff you need to get into some fun projects and understanding how the whole thing works.

20m 66ft 20awg Extension Cable Wire Cord
HeroNeo 10pcs Connector Adapter Cable
JACKYLED New DC 12V 2A 2.0A
SunFounder 8 Channel 5V Relay Shield Module
Sunfounder Project Super Starter Kit with Tutorial Book
Arduino Mega 2560 Board Model
16mm High Round Cap Momentary Metal Push Button Switch
Triangle Bulbs 3528-IP65-White-60L Pure White LED Strip Light, Waterproof LED Flexible Light Strip 12V with 300 SMD LED

Well my first real project was to get led lights to work. I asked for the led lights for christmas as a stocking stuff.
After that I bought the relay and power supply as a gift to myself for my birthday.

Then it was off to wiring it up.
Plug the 12v into one side of the bread board.
Plug the negative for the LEDs into the same row as the power supply.
Then run a wire from the positive to the Common (center) plug of the relay you would like to use.
Take the positive of the LEDs and plug that into the Normally Closed channel of the relay.
The idea with that is when the relay is triggered the circuit if created.

Now to wire up the relay to the arduino.
Run a wire from the 5v power into the VCC of the relay. This is the part that actually powers the physical switch/magnet to close the circuit on the other side of the relay.
Run a wire from the ground power into the ground of the relay.
Use any of the none PWM channels on the arduino since PWM is NOT what we want for controlling the relay.
I picked ch 22 and plug that into ch 1 of the relay.
Note this relay has all the resistors and circuitry setup so you don’t need to do any of that yourself. You can purchase a relay by itself and do all that yourself.

We now have a fully wired up circuit to control the relay.

Lets try this first code to make sure it worked:

// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(22, OUTPUT);

// pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(22, HIGH);
delay(2000);

digitalWrite(22, LOW);
}

That worked so we know can now save that file incase we need it later.
Now we can do the logic for the momentary switch. I picked a momentary switch because of the look in the physical world. It returns to a flat state after its pushed instead of like a light switch that moves and stays in the position you put it on. The problem with the momentary is now you have to keep track of states, the time it takes to push the button and how it needs to be wired.

Continue reading Using a momentary switch with an arduino