March 9, 2011 (See also Companion Blog )
[Go BACK to Table of Contents] [ Next Experiment ] [ Last Experiment ]
After the success of the blinking light system of Experiment 013, I had to forge ahead and create the blinking bicycle light. This was one of my milestones (it was supposed to take six months to get to this stage) and I was incredulous that it turned out to be relatively simple. A word of caution here, please remember that I am a seasoned programmer, and I’m programming light years ahead of my ability to design and produce the circuitry shown in examples like this, and infinitely ahead of my soldering skills.
At the same time, the very nature of using a computer to control the workings of a mechanical device is precisely what I set out to do. In this case, it is blinking lights and it becomes difficult to imagine the thought that goes into the coding. Most people easily recognize that the same goals could be achieved electromechanically, but I am fascinated by the sheer control that digital processing can bring into play. This is the future, like it or not.
Most of us have seen those bicycle lights that flash differing patterns depending on how many times you push a button. I have not button, but I can fake one with a strip of wire. Again, notice the simplicity of the wiring, all the hard work is done in the code.
Naturally, in the future I hope to pair the two up in wizard proportions, you know, complicated circuits and complicated code. The casual reader should be reminded at this point I do not understand much of the theory and calculations in the physical circuit.
Look at the schematic. In an ordinary configuration, depressing the switch would turn on all five LEDs. Notice the switch is momentary, which means it is pressed and then released. It does not stay on like a light switch, that is, it is only on momentarily on, then it disconnects for the next press. This is important and is known as a “normally open” switch. I hope to create a better way of displaying the Arduino pin configurations shortly.
Theory:
When the switch is depressed, it causes a change in the system that depends on several factors, all inside the Arduino. The Arduino code is going to “count” the number of times the switch is depressed, changing the behavior of the 5 LEDs in four different patterns or “modes”. Therefore, the Arduino has to remember the last mode and replace it with the new mode. The only sophisticated component is this code, which always begins a cycle by turning all the LEDs off.
The first press will turn the LEDs on, the second press makes them blink, the third press makes them chase around, and the fourth press turns them off to start a new cycle. Exactly like your bicycle light, except I built this one in an hour. The nearby photo, the fuzzy one, shows the wiring of my circuit.
Practice:
It worked, but there were plenty of things to consider. You can examine the code included here to see that this is the most advanced program I’ve attempted so far. That was a lesson in itself. When the switch was pressed, the Arduino detected this external event and stepped the mode counter up one digit from 0 to 3, then back to 0. Even if you are a non-programmer, take a peek at my code below. Each digit caused a different pattern.
I do not know why the resistor is in the switch leg, but several diagrams showed it, so I put it there. I used 1000Ω because it was handy. It would seem any time the switch went “high” by allowing 5V to flow through pin 2 would work here, but I found that the switch had to be positioned between the pin and the ground, not before pin 2. I have not thought that one through, maybe you can.
One other new feature is the code shows that the status of the pin is read twice with a ten millisecond delay between the reads. There was a technical explanation in one of my sources that this is to prevent a “bounce”, which made sense, so there is the code snippet.
vPinStatus01 = digitalRead(SwitchPin); //store the SwitchPin value
delay(10); //bounce buffer
vPinStatus02 = digitalRead(SwitchPin); //store it again
Conclusion:
This is the most robotic of any project for me to date. The actions of the LEDs were controlled by programming code that reads external conditions, in this case a switch press. I prefer the big picture: that I succeeded in getting one physical component to control a different physical component, and where there is smoke, there is confusion.
The operation of the device involves illusion. At any given moment, only one LED is on. This winking effect is well known in ordinary light bulbs, which flash off and on 60 times per second. The Arduino is much faster and can control many more lights before the human eye could detect the flashing—although you can wave the Arduino back and forth rapidly and barely see a strobe effect.
Allow me to emphasize the less obvious aspects. In a conventional design, the Arduino’s position in the design would be taken by banks of transistors, relays and timing capacitors. In this instance, there is no tangible connection between the switch (input) and the LEDs (output). Even less obvious is that, like a computer, the Arduino is fiercely churning away even when nothing is happening. A failure in the microchip means a failure in the whole system.
The actual control of the system is tiny digital signals buried in the Arduino’s integrated circuit. I have no doubt there are transistors, relays and timing capacitors in there at a microscopic level, but these are not dedicated to the operation of this assembly of hardware. Without rearranging anything material, the hardware can be instructed to do something different.
And that is the precise facet of robotics that I wish to learn, that I set out to learn. I may be wrong in my definitions, thanks to lack of any bona fide research material on the Internet. Until I stumble on something better, to me, the above interaction via the microcontroller IS my concept of robotics.
The code is worth reviewing, although it suffers greatly in readability due to being C based.
// Experiment 014 - Blinking Bicycle Light
// Today is March 09, 2011
// This sketch corresponds to Minutes 014 blog
/* This is the bicycle light circuit. It was inspired by
* my $2 bicycle light, which flashes a pattern depending
* on how many times you've pressed a button.
* Note this button must be a momentary button, that is, once
* you press it, it releases and does not stay pressed.
* Based on Arduino Blinky Light Company.
*/
////////////////////////////////////////////////
//Begin Initialization & Variables
int SwitchPin = 2; //switch is connected to pin 2
int LED01pin = 12;
int LED02pin = 11;
int LED03pin = 10;
int LED04pin = 9;
int LED05pin = 8;
int vPinStatus01; //stores the pin 2 status after a read
int vPinStatus02; //bounce checker
int vButtonState; //stores the state of the button
int vLightMode = 0; //stores the on/off mode, initially 0
//End Initialization & Variables
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// Begin Main Program
/////////////////////////////////////////////////
void setup ()
{ //Begin setup
pinMode(SwitchPin,INPUT); //set input to accept SwitchPin
pinMode (LED01pin,OUTPUT);
pinMode (LED02pin,OUTPUT);
pinMode (LED03pin,OUTPUT);
pinMode (LED04pin,OUTPUT);
pinMode (LED05pin,OUTPUT);
Serial.begin(9600); //Arduino communication speed
vButtonState = digitalRead(SwitchPin); //read initial state
} //End setup
void loop()
{ // Begin void loop
vPinStatus01 = digitalRead(SwitchPin); //store the SwitchPin value
delay(10); //bounce buffer
vPinStatus02 = digitalRead(SwitchPin); //read the SwitchPin again
if (vPinStatus01 == vPinStatus02) //pin status has not changed
{ /////////////////////////////////////////////
if (vPinStatus01 != vButtonState) //pin status has changed
{ ///////////////////////////////////////////
if (vPinStatus01 == LOW) //check if button pressed
{ /////////////////////////////////////////
if (vLightMode == 0) //if light is off
{vLightMode = 1; //turn it on
Serial.print("Mode is ");
Serial.print(vLightMode);
Serial.println(" On");}
//----------------------------------------------------------------------
else
{
if (vLightMode == 1) //if light is on //
{vLightMode = 2; //make it blink //
Serial.print("Mode is ");
Serial.print(vLightMode);
Serial.println(" Blinking");}
//-----------------------------------------------------------------------
else
{
if (vLightMode == 2) //if light is blinking
{vLightMode = 3; //make it wave //
Serial.print("Mode is ");
Serial.print(vLightMode);
Serial.println(" Waving");}
//-----------------------------------------------------------------------
else
{
if (vLightMode == 3) //if on, blinking, waving
{vLightMode = 0; //turn it off //
Serial.print("Mode is ");
Serial.print(vLightMode);
Serial.println(" Off");}
}
//------------------------------------------------------------------------
} //////////////////////////////////
} //////////////////////////////////
} ////////////////////////////////////
} ///////////////////////////////////
vButtonState = vPinStatus01; //saves state of button
}
//This is the light blinking code
if (vLightMode == 0) // if mode is 0, turn all off
{
digitalWrite(LED01pin, LOW);
digitalWrite(LED02pin, LOW);
digitalWrite(LED03pin, LOW);
digitalWrite(LED04pin, LOW);
digitalWrite(LED05pin, LOW);
}
if (vLightMode == 1) // turn all on
{
digitalWrite(LED01pin, HIGH);
digitalWrite(LED02pin, HIGH);
digitalWrite(LED03pin, HIGH);
digitalWrite(LED04pin, HIGH);
digitalWrite(LED05pin, HIGH);
}
if (vLightMode == 2) // blink mode
{
digitalWrite(LED01pin, HIGH);
digitalWrite(LED02pin, HIGH);
digitalWrite(LED03pin, HIGH);
digitalWrite(LED04pin, HIGH);
digitalWrite(LED05pin, HIGH);
delay(100);
digitalWrite(LED01pin, LOW);
digitalWrite(LED02pin, LOW);
digitalWrite(LED03pin, LOW);
digitalWrite(LED04pin, LOW);
digitalWrite(LED05pin, LOW);
delay(100);
}
if (vLightMode == 3) // wave mode
{
digitalWrite(LED05pin,LOW);
digitalWrite(LED01pin,HIGH);
delay(50);
digitalWrite(LED01pin,LOW);
digitalWrite(LED02pin,HIGH);
delay(50);
digitalWrite(LED02pin,LOW);
digitalWrite(LED03pin,HIGH);
delay(50);
digitalWrite(LED03pin,LOW);
digitalWrite(LED04pin,HIGH);
delay(50);
digitalWrite(LED04pin,LOW);
digitalWrite(LED05pin,HIGH);
delay(50);
digitalWrite(LED05pin,LOW);
}
} // End void loop
////////////////////////////////////////////////////
// END MAIN PROGRAM
////////////////////////////////////////////////////
// Key entered by Anton da Bassguy 2011
// Verified March 09, 2011.
// Corresponds to meeting minutes project 014
// and schematic 014 - Blinking bicycle light.
////////////////////////////////////////////////////
Go BACK to Table of Contents
Official Use Only – I always wanted to have a section like that.
C:\Documents and Settings\Do Not Use\My Documents\8000 Series Files\8010 ARDUINO\2000 Minutes\Minutes 2011-03 (Mar)
C:\Documents and Settings\Do Not Use\My Documents\8000 Series Files\8010 ARDUINO\2000 Minutes\Jpegs 2011-03 (Mar)
C:\Documents and Settings\Do Not Use\My Documents\8000 Series Files\8010 ARDUINO\3002 Arduino Saved Sketches\A100414_14BicycleLite