Simple program problem

maaca

New Member
I am doing a simple code for my grandsons railway project. I am nearly 80 years old and struggle a bit with these. The problem is at power up C.1 and C.2 immediately turn on and stay that way even though pin3 is low. The only way I can get C.0 and C.4 to light up and the others to go off is to physically connect pin 3 to negative. In picaxe editor if i run the code in simulation it works fine. I wired it up to a breadboard and just get this problem. I am completely lost and would appreciate some advice as to what i am doing wrong. Many thanks.
code:
#picaxe 08m2
Main: 'Ricks Traffic Lights'



if pin3 = 1 then 'actuated by button one on remote switch'
Low c.0 'This ensures light out to start'This is the big green light.
Low c.4 'This ensures light out to start.This is the flashing green light.
High c.1 'This turns on the big red light.
High c.2 'This turns on the flashing red light.

endif
If pin3 =0 then 'actuated by button one on remote switch'
Low c.1 'This turns off the big red light.
Low c.2 'This turns off the flashing red light.
endif


if pin3 = 0 then
High c.0 'This turns on the big green light'
High c.4 'This turns on the flashing green light.
endif
'To go back to start press button one on remote switch.This will turn green lights off.
goto Main:
 

bpowell

Senior Member
It sounds like you need a pull-down resistor on Pin 3 .... you could hook a 10kOhm resistor to pin 3, and that would give it a known state (low) then, you have your switch which is connected on one end to vcc, and the other end to Pin 3 (preferably through a 1k resistor).

Also, be sure to have a decoupling cap on the PICAXE (.1uf) across VCC and GND, as close to the chip as possible.

Lastly ... you have two if-statements with the same condition ( if pin3 = 0 ....) why not just add the statements in the second if-statement to the first?

Otherwise, great job!
 

erco

Senior Member
Welcome maaca! 80 years young and diving right in, great job! bpowell has given you solid advice, a pulldown resistor is in order, as well as decoupling caps, I use a .1uF and a 10 uF electrolytic too.

It appears your program just sets two different output pin combinations based on whether pinc.3 is pressed. As bpowell also stated, you can combine statements at the end. Here's a very crunched version of your code:

Code:
#picaxe 08m2
Main: 
if pin3 = 1 then low 0: low 4: high 1: high 2
else low 1: low 2: high 0: high 4 ' does all this if pin3 is NOT 1
endif
goto Main


Also be aware that your program is looping quickly and continuously (whether the button is pressed or not) since you have single if checks.
 

inglewoodpete

Senior Member
A couple of optional code simplifications
  • Do and Loop commands can be used instead of GoTo <label>
  • Two or more outputs can be listed after the High and Low commands.

Rich (BB code):
#picaxe 08m2
Do 
   if pin3 = 1 then low 0, 4: high 1, 2
   else low 1, 2: high 0, 4             ' does all this if pin3 is NOT 1
   endif
Loop
 

erco

Senior Member
TYVM, IWP, I did not know you could combine pins on high/low commands. Igneous! I mean ingenuous! I mean clever!
 

maaca

New Member
Thanks BPowell I did exactly as you said and it works a treat absolutely perfect. I have one very happy grandchild. I will change the code to what Inglewood Pete suggested as its so simple. I have saved the whole thread and placed into a page i have of codes for future referance. Never too old to learn I guess,just gets harder and takes longer. Many thanks to you all.
 
Top