Basic Stamp

video2e5779197549.jpg

Traffic Light Simulation using the Basic Stamp 2

So, it took me long enough right? I know in my last post I left off by saying that I would try to have at least 1 project that is build with the Basic Stamp 2 and include it on my next post. I came up with a Traffic Light simulation which I thought was a very practical application and I am very confident that everyone reading this blog is familiar on how a traffic signal works.

Initially, I thought about posting screenshots of the Homework Board circuit; but I felt that these types of projects would be best to demonstrate with a video.

 

 

Here is the circuit schematic that I have constructed using ExpressPCB which is absolutely FREE!

 

SimpleTrafficLight

 

As you can see, the connections on the left represents the I/O pins of the Basic Stamp. Then there is a series connection of a 220 ohm resistor and the LED for the traffic signals and cathode or the minus (-) or the flat end of the LED is connected to GRND.  I think it’s worth mentioning that when an I/O pin goes HIGH each circuit would have a source voltage of +5 volts and when the I/O pin goes LOW then the source voltage goes down to 0 volts. In this circuit configuration, the LED is configured as Active High which means that when I send a HIGH signal to an I/O pin, the LED is turned on and a LOW turns the LED off.  According to the documentation for the Basic Stamp, each I/O pin can only source up to 20 mA of current if only 1 pin is connected to a circuit. However, if we are using a group of pins, we can only source up to 40 mA. So, we have to be careful not to supply our circuit with too much current or we can risk damaging our Basic Stamp that is the reason I included a 220 ohm resister to limit the current flowing through the circuit. Also, take into the account that the Basic Stamp Homework Board has built-in 220 ohm resistors as a safety feature but I didn’t want to take any changes so I added my own. But, if you don’t decide to use the resistor, then you LED would light up more brightly than in the video. Good. That is out of the way.

Ok. Now here is the code that I used to get all of this done. Remember, the Basic Stamp is programmed using PBASIC so the code should be very easy to read.

 

' =========================================================================
'   {$STAMP BS2}
'   {$PBASIC 2.5}
' =========================================================================

' -----[ Program Description ]---------------------------------------------
' SIMPLE TRAFFIC LIGHT CIRCUIT SIMULATION
' Traffic Light Transition:
'                       RED
'                       GREEN
'                       YELLOW

' -----[ I/O Definitions ]-------------------------------------------------

RedLED          PIN     15
YellowLED       PIN     10
GreenLED        PIN     5

RedDuration     CON     20000           '-- 20 sec
GreenDuration   CON     20000           '-- 20 sec
YellowDuration  CON     10000           '-- 10 sec

' -----[ Constants ]-------------------------------------------------------

' -----[ Variables ]-------------------------------------------------------

' -----[ EEPROM Data ]-----------------------------------------------------

' -----[ Initialization ]--------------------------------------------------

Reset:

' -----[ Program Code ]----------------------------------------------------

Main:

  DO

'-- turn only Red light on for 20sec
    HIGH  RedLED
    LOW   YellowLED
    LOW   GreenLED

    PAUSE RedDuration

'-- turn only Green light on for 20sec
    LOW   RedLED
    HIGH  GreenLED

    PAUSE GreenDuration

'-- turn only Yellow light on for 10sec
    LOW   GreenLED
    HIGH  YellowLED

    PAUSE YellowDuration

  LOOP

  END
' -----[ Subroutines ]-----------------------------------------------------

 

Hopefully, the code is strait forward.  As you can see I set up some variables to hold the numbers of the I/O pins I am using. Then I have some variables to represent the duration that each LED will be turned on. Then if you jump down to the main body of the code, you would see that this code would run in a continuous loop and will never end until the battery is disconnected or runs out.

Then, you can see that I first turn on the Red LED by sending a HIGH to that I/O pin while setting all the other pins to LOW. Then I pause for the duration specified in the variable and then everything else is pretty cut and dry. I just turn on and off the other lights in the sequence that I want. Pretty simple huh?

Hopefully, this was helpful to you and you can see how easy Microcontrollers are to work with these days. I welcome any questions or comments you may have. Until next time…

 

Happy Coding!

board-homework-bread.jpg

What I have been up to lately…

board_homework_bread

 

What is this? It’s a Basic Stamp Microcontroller board that is manufactured by Parallax, Inc.  Some of you may not know; but I originally got started with Programming as a Electronics Engineering student in college. Electronics was my first love.  However, when I graduated the market demands were for programmers and not electronic engineers so I decided to go where the demands were. This was over  6 years ago and I pretty much forgot everything, so I figured I would re-teach myself the basics of electronics and learn how to program microcontrollers in the process. I did have some experience programming the Motorola 68000 which were found in the early Apple computers. But, I wanted something that was a little easier to set up and have a better programming environment. The 68000 chip had to be programmed using Assembly Language and a EEPROM programmer. But of course, I didn’t want to go back to using Assembly Language after writing C# for all these years. To program the Basic Stamp all you need is a PC and a serial port. I do have to learn a new language called PBASIC, which looks as feel a lot like the original BASIC language. To start of with, I would advise anyone to purchase the Basic Stamp Activity Kit as it includes everything (except the 9V battery) you need to get started with.  The documentation is very easy to read and they have a great forum you can use to post any questions that you may have.

In my next post, I would like to have at least 1 project completed and I will take some pictures of the circuit board layout and post the code for you to see how easy it is. Obviously, the more complex circuit you have, the more code you have to write. So, I will start off with something simple.  Hopefully, this post has inspired you to start a new adventure in any area of your life. This is one adventure that I hope I can keep up with until something else comes along :-) .  Until next time…

 

Happy Coding!