I wanted to create a button and some code that would toggle an LED's flashing sequence on and off.

I wanted to create a button and some code that would toggle an LED's flashing sequence on and off. In other words, starting and stopping "something". The LEDs don't do anything terribly interesting (ooh, flashy, flashy!) and the code to switch colours is quite crude but the point was the logic behind the starting and stopping of the process and getting a good solid button circuit that I can use again.

 

The button circuit is from a very good tutorial by LadyAda and the LED circuits are from the standard tutorials.

 

As I am a complete novice, it took most of a day - on and off to figure it out but it works, and works well. It is very reliable now that I have ironed out all the bugs and my own ignorance!

 

The trick (as I'm sure most people will have noticed) is to figure out exactly when the switch will turn the LEDs on and when it will turn them off.

 

In this case, I wanted the switch to happen whenever the switch was pressed in then released. Hence, I had to write the code to only switch when the signal went from 0 to 1 (in this case). 

 

So if the button state has changed from 0 to 1 (buttonState / buttonStateLast) AND the lights ARE NOT running THEN we tell them to run (myRunning to 1)

 

If the button state has changed from 0 to 1 (buttonState / buttonStateLast) AND the lights ARE running THEN we tell them NOT to run (myRunning to 0)!

 

Made using a clone arduino with ch34x Serial - https://www.amazon.co.uk/gp/product/B00SR4FLMI/ref=oh_aui_detailpage_o02_s00?ie=UTF8&psc=1

 

Uses part of button tutorial from http://www.ladyada.net/learn/arduino/lesson5.html

 

Also uses the Bounce2 debouncing library - https://github.com/thomasfredericks/Bounce2

 

As a beginner, I have some bigger projects in mind already - even if I'm late to the whole Arduino party!

 

EDIT: This is also my first time using Fritzing so apologies for any errors - I can confidently say they're all mine!

 

One of the .fzz  files might have a resistor and hookup wire on the wrong track on the breadboard :-(