![]() |
Forum Index : Microcontroller and PC projects : CMM2 "Traffic Lights" on screen and LEDs - LEDs flash on RUN
Author | Message | ||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Hi all Testing linking on screen "action" to the physical, so I built some traffic lights that draw a crude set on screen and light some LEDs in time. All works well and as expected -- except, when the program is RUN for a brief moment (and I mean a literal twinkle) -- the LEDs flash. My code has only the GREEN LED set as I wondered if it was my wiring -- but with just the green present, it still flashes briefly on run. Is this default behaviour as PIN 5 is not set until the program actually runs? Happy if that is the case (just wondering). Is it possible to set some default values so that the output from all pins is LOW before they are initialised (not sure if this is a daft question). Anyway my code -- happy for imprvements: mode 1,8 cls 'Here I will define 3 pins for LEDs SETPIN 5,DOUT PIN(5)=0 'Draw the traffic light frame box 220,70,60,160,2,rgb(white),rgb(black) 'Subs for the states of the lights Sub RED ' SETPIN 7,DOUT circle 250,100,25,2,,rgb(red),rgb(red) circle 250,150,25,2,,rgb(black),rgb(black) circle 250,200,25,2,,rgb(black),rgb(black) PIN(5)=0 end sub Sub RedAmber circle 250,100,25,2,,rgb(red),rgb(red) circle 250,150,25,2,,rgb(255,165,0),rgb(255,165,0) circle 250,200,25,2,,rgb(black),rgb(black) PIN(5)=0 end sub Sub Green circle 250,100,25,2,,rgb(black),rgb(black) circle 250,150,25,2,,rgb(black),rgb(black) circle 250,200,25,2,,rgb(green),rgb(green) pin(5)=1 end sub sub Amber circle 250,100,25,2,,rgb(black),rgb(black) circle 250,150,25,2,,rgb(255,165,0),rgb(255,165,0) circle 250,200,25,2,,rgb(black),rgb(black) pin(5)=0 end sub Do ' cycle the lights forever RED pause 2000 RedAmber pause 2000 Green pause 2000 Amber Pause 2000 Red Loop Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Ok, so not solved but have a solution for my students.... On first run, the traffic lights need to enter a self test to make sure they all light -- so, on RUN light all and FLASH them to prove they work. Then enter the real sequence. Still be interested in the reason they briefly flash.... Nim Entropy is not what it used to be |
||||
CircuitGizmos![]() Guru ![]() Joined: 08/09/2011 Location: United StatesPosts: 1427 |
What is your hardware circuit? Do you have a resistor pulling the pin to an inactive state? Micromites and Maximites! - Beginning Maximite |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Pin(5) to 2k resister through to LED and then through to ground Am I missing some electronic-fu? It's been a long time since I learned all this..... pull up / pull down.... N Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
![]() ![]() Yes missing a pulldown. Added 1K to pull down -- all fine now. As I said, been a long time. Thanks for the steer. Nim Edited 2020-08-12 07:43 by Nimue Entropy is not what it used to be |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
One useful thing to remember when connecting to the real world. SETPIN 5,DOUT PIN(5)=0 You will have a short period between the two lines when the output is whatever the CPU decides, not necessarily what you want. This will be too fast to see but can trigger unwanted events in external circuitry. If you swap the order of the commands, PIN(5)=0 SETPIN 5,DOUT You will be sure that the pin initializes in the low state. There is still the time from first applying power to running the program that might cause issues so external pull-downs/ups etc are still worth considering. Jim VK7JH MMedit |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
![]() Done - plus pull down added. Now behaving properly. I left the "self test" idea in -- the students will, I am sure have "fun" designing a test pattern for the lights -- I assume that in the real world, there is a test pattern that lights have --- need a traffic engineer to ask! Thanks all. Nim Entropy is not what it used to be |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I just realised what was bothering me. Your simple LED circuit shouldn't be troubled by a powerup 'high' There is a good reason why it is suffering. Pins 3 and 5, 27 and 28 are all used for I2C (as well as general IO). They all have 10k pullup resistors fitted between the pin and 3.3V. It is those 10k pullup resistors that caused your LED to flash. Using a different pin would not have the problem, although setting the pin level before setting it as output is still a good idea. I remember making traffic light during a PLC training course, many years ago. One of the posters here made an elevator control system using a micromite. There are lots of real-life things for your students to play with. Jim VK7JH MMedit |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Hi Nim (again), You said you might need input from a Traffic Engineer, how about one with 40+ years experience (I won't bore you with the details but trust me I DO know traffic signals) including those on the Manchester Outer Ring Road, Denton to Middleton section. There is not a test pattern as such but on every re-start they have steady "all red" for say 8 seconds then flash "yellow" (not "amber") for say 8 seconds and then the green for the main traffic movement (best to check what the UK practice is - the above timings are programmable). (If I forget, the UK and its colonies, use a short Yellow/Red sequence before the green - in Australia we no longer do (some councils in Victoria used to). Certainly doing it in software on screen will be much easier than using LEDs etc but see how you go. These may be useful but are more expensive than separate LEDs. . . Cheers, Andrew (I've always thought of writing a student-level tome about traffic signals and link to the www for more information) Edited 2020-08-12 10:01 by Andrew_G |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
This >>> Doh. Revising pin usage. Good spot. Other good practise incorporated. Cheers Nim Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
![]() I wanted them to code away from the hardware first - then to move to setting pins etc. CMM2 makes that really easy. I was today years old when I knew I needed these.... thanks for sharing. What's really interesting is just how unaware of the world around them students are -- do it!! I miss good "old fashioned" (and I mean that positively) text books. Nim Entropy is not what it used to be |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |