As a starting point we used the code from a Sunfounder project, a very simple one that fades a single LED in and out.
Copying the code from the Sunfounder website and pasting it into the Arduino IDE for Mac caused an encoding error that broke the code – the greater than (>) and less than (<) symbols were replaced with html equivalents. Once that was fixed the code worked fine:
const int ledPin = 9; // the pin that the LED is attached to void setup () { pinMode(ledPin, OUTPUT); // declare pin 9 to be an output: } void loop() { for (int a = 0; a <= 255; a++) //loop from 0 to 255 { analogWrite(ledPin, a); // set the brightness of pin 9: delay(8); //wait for 8 microseconds } for (int a = 255; a >= 0; a--) //loop from 255 down to 0 { analogWrite(ledPin, a); // set the brightness of pin 9: delay(8); //wait for 8 microseconds } delay(800); //wait for 800 microseconds }
The goal here was to teach my kids something about finding code that acts a a good starting point, and modifying it to suit your needs. A first step in that is cleaning it up and optimizing it before making any big changes.
Right away there was something in this code that could be improved. There are both minimum (0) and maximum (255) brightness level values for the LED. Since a big part of this project will be about tweaking those values to get the best effect, it makes sense to declare them as constants up at the top so they can be adjusted easily.
Naming convention... almost all of my life is spent managing the ongoing development of a pretty huge solution. It's far more complex than any of us that work on it can keep in our heads, so I'm a huge fan of highly descriptive names. Yes – it's easier to use a, b, c, x, y, etc., but when you are down on line 500 it can tough to remember the comment on line three that explained what c was. Devising elaborate short names can be a fun challenge, they're rarely easier to remember than single letters when you revisit them months later.
So with that in mind the constants we added were called brightness_minimum and brightness_maximum. We also replaced the variable "a" in the source code with brightness_current. Now, after some adjustments to the comments, the code looks like this, verifies, and produces the same result as the source file.
// BRIGHTNESS // minimum brightness level for the led int brightness_minimum = 0; // maximum brightness level for the led int brightness_maximum = 255; const int ledPin = 9; // the pin that the LED is attached to void setup () { pinMode(ledPin, OUTPUT); // declare pin 9 to be an output: } void loop() { //loop from minimum brightness up to max for (int brightness_current = brightness_minimum; brightness_current <= brightness_maximum; brightness_current++) { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(8); } //loop from maximum brightness down to min for (int brightness_current = brightness_maximum; brightness_current >= brightness_minimum; brightness_current--) { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(8); } delay(800); }
Next up are the pauses. These two will be adjusted quite a bit as we try and find the values that provide the best effect, so it makes sense to make them constants that are declared up at the top. Again we use very descriptive names that leave no room for confusion, and also the exact same numerical values. We don't to make changes to the output just yet, we're just moving the code around. Introducing too many changes at once makes debugging more complicated.
Again the code verifies fine and produces the same effect with the LED.
// BRIGHTNESS // minimum brightness level for the led int brightness_minimum = 0; // maximum brightness level for the led int brightness_maximum = 255; // PAUSES // the pause between each increase in brightness int pause_between_increase = 8; // the pause between each decrease in brightness int pause_between_decrease = 8; // the pause between cycles int pause_between_cycles = 800; const int ledPin = 9; // the pin that the LED is attached to void setup () { pinMode(ledPin, OUTPUT); // declare pin 9 to be an output: } void loop() { //loop from minimum brightness up to max for (int brightness_current = brightness_minimum; brightness_current <= brightness_maximum; brightness_current++) { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(pause_between_increase); } //loop from maximum brightness down to min for (int brightness_current = brightness_maximum; brightness_current >= brightness_minimum; brightness_current--) { analogWrite(ledPin, brightness_current); // set the brightness of pin 9: delay(pause_between_decrease); } delay(pause_between_cycles); }
Now we have code that is, hopefully, more readable, and the kids can easily adjust all parameters of the effect – the minimum and maximum brightness, the pauses, etc. The next thing may be to have different pause value between cycles – one value for the pause at maximum brightness, another at minimum brightness.