Simple Code for a Police Car
Updated: Feb 16, 2020
When looking back on my code, I realised that there was a way of simplifying it to enable me to make quick changes as and when needed.
I did this by adding variables to my code. For example, instead of having:
'pinMode(13, OUTPUT); //redLed'
I created a variable called 'redLed', then added this variable to my code:
This makes it clearer to not only others, but myself. Allowing me to quickly pinpoint and identify snippets of code. I also created a variable called 'flashSpeed'. This allowed me to quickly change the speed by adjusting one number within the code, rather than having to look down the code, and manually adjusting every delay.
After adding all of my variables to my code, this is what my code looked like.
When executing the code on the Arduino, there was no change to the way the LED's behaved. Even though the code has now been written in a totally different way, it provides the same end result (and has now left room for simplifying any changes that need to be made).
The value of the variable 'FlashSpeed' can be changed at any point. Changing FlashSpeed from 1400 to 1000 will reduce the amount of time it is delayed for, and will therefore make the LED's flash quicker. Wherever 'delay(FlashSpeed)' is shown in the code, will insert the value '1400'.
If the code had this repeated 7 times, the value would only have to change once, rather than changing the value in 7 different places.