Sunday, March 20th, 2016
Today I am experimenting with the Arduino C Code to see what limitations I might have on creating libraries for different routines within the robot.
I wanted to create a few simple routines to report the status of different functions while the robot is active and also when in a self-test mode.
The first thing I created was an SOS mode where I can flash and L.E.D. (Light Emitting Diode) in the Morse Code for S.O.S. (the Distress Signal). The S.O.S. was first introduced by Germany in 1905 as a distress signal adapted for Maritime Shipping. Being German in origin, I doubt it actually stands for Save Our Ship, but that was a common English adaptation of the meaning. It was likely adopted simply because the SOS are fast, easy, Morse Code symbols to generate (dot, dot, dot, dash, dash, dash, dot, dot, dot).
If you want to know more about Morse Code, there is plenty of information on Google.
So, getting an LED. to flash with an Arduino is not a difficult chore. In fact, they have a blink program in the examples library that will make one flash if you just want a test program. That is usually the most common program run.
What I wanted was a way to store a library of symbols and call them in code so that I used very little actual program space.
To make an LED blink, all you really need to do is connect one via a small breadboard with a resistor between ground and you are all set. Power the pin connected to your signal on the LED High and it will light up. Most Arduino's have a built in LED on Pin 13, so I used that for this first test.
Looking at the code, we will set the Signal Pin to 13 with:
int _sp(13);
So, now the variable
_sp means 'Use Pin 13' in the program.
Then in the Setup portion of the program, we make sure to set this pin to be an OUTPUT. That means we will either give it 5 Volts or take the 5 Volts away.
pinMode(_sp, OUTPUT);
Now that we have a variable describing what pin to give power to, we just need to tell the Arduino board when we want to give it power and in what kind of a pattern. By setting the pin 13 High and Low we can give the 5 Volts or set it to 0 Volts (take the 5 Volts away). This also means turning the LED ON (High, or +5 Volts) and turning the LED OFF (Low, or 0 Volts). We can use a Delay in between these to leave the LED on or off for a certain amount of time.
Using a Delay command with a time in milliseconds (1000 milliseconds is equal to 1 second) will give the dots or dashes needed to have 3 short flashes and 3 long flashes and 3 short flashes again.
The first version of the program I came up with to test this theory was the following.
/*
SOS Flash
This is an experiment with pulse for an LED
The aFlash will be turned into a library call.
The SOS will be turned into a library call.
*/
// Set the signal pin that has the L.E.D
int _sp(13);
// the setup function runs once when you press reset or power the robot up.
void setup() {
// initialize signal pin as an output.
pinMode(_sp, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
//Do an SOS (...---...)
_SOS();
}
void _SOS() {
/* Three short, Three long, Three short
* that is the SOS Signal (...---...)
*/
int x;
int y;
for( x = 0; x < 3; x++) {
for( y = 0; y < 3; y++) {
if(x == 1) {
_aFlash(800);
}
else {
_aFlash(250);
}
} //End Y Loop
delay(1000);
} //End X Loop
delay(2000);
}
//Sub for a flash with delay time between high and low as variable.
int _aFlash(int _dTime) {
digitalWrite(_sp, HIGH);
delay(_dTime);
digitalWrite(_sp, LOW);
delay(_dTime);
return 0;
}
If you read through this, it is not really too complicated.
After setting _sp to pin 13 and setting pin 13 as an output, we use the Loop to call our _SOS program.
// the loop function runs over and over again forever
void loop() {
//Do an SOS (...---...)
_SOS();
}
Next we have to define that _SOS function in the program. I am using it as a function type void since it will not return any values back to the program when it is called.
void _SOS() {
/* Three short, Three long, Three short
* that is the SOS Signal (...---...)
*/
The first thing in this function is to set two variables as integers using:
int x;
int y;
These are going to be our counters for the function so that we can control how many times to flash the LED and how many times we want to flash it either long or short.
Next we have 2 loops that will set up the flashes.
for( x = 0; x < 3; x++) {
for( y = 0; y < 3; y++) {
if(x == 1) {
_aFlash(800);
}
else {
_aFlash(250);
}
} //End Y Loop
delay(1000);
} //End X Loop
The first one with X starts by setting X to 0, then while X is less than 3, increase X by 1
for( x = 0; x < 3; x++) {
This means that on the first run though, X is now 0...
The next statement says to start Y at 0, and while Y is less than 3, increase Y by 1
for( y = 0; y < 3; y++) {
So, Y is now 0 also.
Then we check to see if X is equal to 1
if(x == 1) {
And since it is actually 0, that means that we go to the Else line (if X was equal to 1 we would go to the next line...but now we get to skip over that and to the the Else line.
The Else line calls a function called
aFlash with a value of 250. The aFlash program is a little further down and looks like this:
//Sub for a flash with delay time between high and low as variable.
int _aFlash(int _dTime) {
digitalWrite(_sp, HIGH);
delay(_dTime);
digitalWrite(_sp, LOW);
delay(_dTime);
return 0;
This sub declares that it will return an Integer if it returns anything at all and has an Argument (which means it requires you to give it a value when it is called) and the argument is a variable called dTime which is really the amount of the delay time we want to hold the LED either ON or OFF for.
Now, since we called _
aFlash(250) we used the (250) as the argument to supply the variable dTime with a value of 250 (which is a delay time that I mentioned earlier was in milliseconds...so 250 milliseconds or 1/4 of one second...really fast!).
The function _
aFlash then performs a digitalWrite on the _sp pin (pin 13, remember) and sets it to HIGH...this turns ON the LED.
Then it delays for 250 milliseconds, then does a digitalWrite on the _sp pin again and sets it LOW which turns it OFF, and keeps it off for 250 milliseconds.
The function in this case does not need to return anything back to our _SOS function so we set the return to 0 (which is actually an integer as we originally called _
aFlash as returning an integer).
We jump back to _SOS now after we have flashed the LED on and off really quick, and we then loop back to Y, which is now getting the Y++ so now it is 1.
Since there is no condition for Y, only X and X is still 0, we just call _
aFlash again with the same 250 ms delay time and flash the light fast a second time. Then do a Y++ to make Y 2, which is still less than 3 so we call _
aFlash again and flash the LED fast one more time...that is the first three fast blinks because now we do Y++ again and so Y would be 3 and that would end this loop since the For statement says to only loop while Y is Less than 3.
Now we delay for 1000 milliseconds (which you might remember is 1 second) and then we return to X. Now X gets the X++ and X is now equal to 1.
In the loop, if X is equal to 1 we are going to drop back to Y again and run through the Y loop again 3 time, calling
aFlash again, but now with a longer delay time of
800 milliseconds. This means that the LED will go ON, stay on for almost a full second (instead of 1/4 of a second like the first 3 times) and then go OFF, and stay off for almost a full second. This will give us the 3 LONG blinks, and then do a X++ again to set X to 2. Since X is now 2, which is less than 3 we will hit the Y loop again and since X is not equal to 1 anymore the delay time goes back to 250ms or 1/4 of a second for the 3 loops through Y, giving us 3 fast blinks. Then we update X with X++ and now it is equal to 3, which ends our X loop and so we drop out to the line after the X loop which gives us a delay of 2000 which means that we will do nothing for 2 seconds, and then we will go back to the start of the void loop() function and start the whole process over again doing 3 fast blinks, 3 long blinks then 3 fast blinks again...forever.
This code will actually get shortened a little but externalizing the _
aFlash() function and the _SOS() functions into header files that will become part of a library of headers we can use for the robot which means we will actually just call them at the beginning of the program and then we can have less code in the main loop.
More on that tomorrow...enough for tonight. Here is a short, dark video of the small Arduino board running the program.
In the next day or so, I will show how to externalize some of the functions so that the code is able to be called over and over again without having to list the whole function each time you want to use that piece of code.