Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:20 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

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 Kingdom
Posts: 420
Posted: 08:49pm 11 Aug 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 420
Posted: 09:19pm 11 Aug 2020
Copy link to clipboard 
Print this post

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 States
Posts: 1427
Posted: 09:22pm 11 Aug 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 420
Posted: 09:33pm 11 Aug 2020
Copy link to clipboard 
Print this post

  CircuitGizmos said  What is your hardware circuit? Do you have a resistor pulling the pin to an inactive state?


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 Kingdom
Posts: 420
Posted: 09:43pm 11 Aug 2020
Copy link to clipboard 
Print this post

 
  CircuitGizmos said  What is your hardware circuit? Do you have a resistor pulling the pin to an inactive state?


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: Australia
Posts: 6283
Posted: 09:54pm 11 Aug 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 420
Posted: 09:59pm 11 Aug 2020
Copy link to clipboard 
Print this post

  TassyJim said  One useful thing to remember when connecting to the real world.


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


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: Australia
Posts: 6283
Posted: 10:56pm 11 Aug 2020
Copy link to clipboard 
Print this post

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: Australia
Posts: 871
Posted: 11:41pm 11 Aug 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 420
Posted: 09:30am 12 Aug 2020
Copy link to clipboard 
Print this post

This >>>

Doh.

Revising pin usage.  Good spot.

Other good practise incorporated.

Cheers
Nim

  TassyJim said  
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.
Jim

Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 09:35am 12 Aug 2020
Copy link to clipboard 
Print this post

  Andrew_G said  Hi Nim (again),
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...


  Andrew_G said  
Certainly doing it in software on screen will be much easier than using LEDs etc but see how you go.


I wanted them to code away from the hardware first - then to move to setting pins etc.  CMM2 makes that really easy.

  Andrew_G said  
These may be useful but are more expensive than separate LEDs. . .


I was today years old when I knew I needed these.... thanks for sharing.

  Andrew_G said  
(I've always thought of writing a student-level tome about traffic signals and link to the www for more information)


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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025