When writing code there are almost always several ways to do things, but one thing that most programmers try to achieve is to get their code compact and to use as little space as they can.
This code is available on our File Cabinet page, or through a
direct link here...
All of the comments to explain the code are in the sketch and follow these signs //
This mean that the Arduino does not read anything after the two symbols:
// The arduino recognises this as a comment.
It is very simple to set up...
This diagram was produced using Fritzing. A link to a download for Fritzing is available on our File Cabinet page. Note that the positive power rail at the bottom is not needed, but it is good practise to include it in case your project develops and needs use of it.
Here is the sketch...
___________________________________________________
/*
This Sketch shows two useful concepts in writing sketches for Arduino.
Firstly it demonstrates how to use FUNCTIONS and then call them in the sketch.
It also demonstrates how to use a WHILE loop, using a count to add to a
predefined variable
*/
// Give Pin 13 a name...
int led = 13;
int count = 0;
// the setup routine runs once only, when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
Reset(); // Call the "Reset" function declared at the bottom of this sketch
SlowBLINK(); // Call the "SlowBLINK" function declared at the bottom of this sketch
Reset();
MediumBLINK(); // Call the "MediumBLINK" function declared at the bottom of this sketch
Reset();
FastBLINK(); // Call the "FastBLINK" function declared at the bottom of this sketch
}
void SlowBLINK() // Funtion named "SlowBlink"
{
while(count < 5) // Repeat whilst the value of "count" is less than 5
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
count++; // add "1" to the value of "count"
}
}
void MediumBLINK() // Funtion named "MediumBlink"
{
while(count < 10) // Repeat whilst the value of "count" is less than 10
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for 500ms
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for 500ms
count++; // add "1" to the value of "count"
}
}
void FastBLINK() // Funtion named "FastBlink"
{
while(count < 50) // Repeat whilst the value of "count" is less than 10
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for 100ms
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 100ms
count++; // add "1" to the value of "count"
}
}
void Reset() // Funtion named "Reset"
{
count = 0; // Reset the value of the variable "count" to "0"
// this needs to be done to make the "while" functions work.
}