Problems with a Picaxe 14M motor control program

stevecoulson

New Member
Hi, I have written a simple motor control program that uses a magnetic sensor. I have written the program and run it on the simulator and all seems fine. When I try it in the Picaxe 14M though nothing happens. I have tried 3 different 14Ms, all the same. I have made a copy of the circuit in breadboard and it still does nothing. When I run the program in 'Simulation' I sometimes get a pop up that says 'Gosub stack too large' but reducing the Gosub commands by 2 made no difference.
This is the code:


Code:
'variables

 symbol  Go = Input4

 symbol MagSen = Input3

Main:

     Gosub  WaitForSignal
     Gosub  TableClockWise
     Gosub  TableGo
     Gosub  SensorWait
     Gosub  TableStop
     Gosub  TableTarry
     Gosub  TableAntiClockwise
     Gosub  TableGo
     Gosub  TumbrelStart
     Gosub  SensorWait
     Gosub  TableStop
     Gosub  Main
     
     'Subroutines
     
  WaitForSignal:
     If Go = 0  then WaitForSignal
     Return
     
  TableClockWise:
     High 1 'sets the H bridge clockwise
     Low 2
     Return
     
  TableGo:
     High 0
     return
     
  SensorWait:
     If Magsen = 0 then SensorWait
     Return  
     
  TableStop:
     Low 0
     Return
     
  TableTarry:
     Pause 4000  
     Return
     
  TableAntiClockwise:
     Low 1
     High 2
     Return
     
  TumbrelStart:
     High 5
     Pause 300
     Low 5
     Return
The final subroutine (TumbrelStart) is a signal back to the 28M2 to continue with it's program. I thought that using Output0 might be a problem as this is the
Serial Out pin but moving it made no difference.
I am not an experienced programmer and placing everything in a Gosub stack seemed like an easy way to do things. Have I done wrong somewhere? SC
 
Last edited by a moderator:

nick12ab

Senior Member
Using gosub main to return to the main label is very bad, but the other uses of gosub are correct.

Use goto main instead.
 

hippy

Ex-Staff (retired)
That "GOSUB Main" should be a "GOTO Main", but even so it looks like it should work on a real chip because of the way the subroutine stack is implemented.

That suggests there is more to the problem than just that issue. It may be best to add a "#TERMINAL 4800" and SERTXD commands in the subroutines so you can tell where the code is getting to when run in a physical chip, for example -

Code:
WaitForSignal:
[b]SerTxd( "WaitForSignal", CR, LF )[/b]
If Go = 0 then WaitForSignal
Return
And similar for other subroutines.

It may be that the code only ever sees "Go = 0" so never progresses beyond WaitForSignal. The challenge is in determining where it does get to, and whether it does what it is meant to at the various points it passes through. It could be that it does everything it should but just isn't controlling the hardware connected to the outputs. Being able to trace code execution will help narrow down what might be wrong.

And welcome to the PICAXE forum.
 

stevecoulson

New Member
Hi, Thanks for your replies. I have changed the Gosub Main command to Goto Main. I also tried the Sertxd commands too but, as you suggested Technical the program did not move beyond the first command in the program. Inserting Sertxd into each sub-program is a good wheeze though, I will remember it for future use.
Not knowing quite what to do, I breadboarded the circuit again and when that didnt work I built another. This went on for some time. I did eventually get one circuit to work but on this occasion I added the two download resistors (10k and 22k). I hadnt put them in in the other circuits because I had recently made a download cable following a design in an old copy of Nuts and Volts Picaxe Primer I changed the design slightly by putting the two resistor upstream of the mini-jack plug meaning that when the mini-jack was removed the resistors were taken out of circuit. I didnt think that the resistors were important but maybe I was wrong? I thought they were just a voltage divider. SC
 

hippy

Ex-Staff (retired)
I didnt think that the resistors were important but maybe I was wrong? I thought they were just a voltage divider.
Having a download interface or pull-down on the download serial in is essential for correct PICAXE operation. Leaving download serial in floating will cause unpredictable behaviour and behaviour such as you experienced.

We recommend fitting the full download circuit to all projects because that allows in-situ programming and will also aid debugging. It will also reduce the risk of damage when moving chips for programming and reduces the risk of the PICAXE being re-inserted back into the circuit wrongly.

We have the AXE029 download cable adapter which makes it easy to add a download socket to breadboard.

The download circuit itself is not a voltage divider though it may at first glance appear to be so. It is actually a current limiting 22K with a 10K pull-down before the 22K ( rather than after it ) which prevents the download serial in from floating.
 

premelec

Senior Member
From page 27 Manual #1:

" The 10k/22k resistors must be included for reliable operation.
DO NOT leave the serial in pin floating as THE PROGRAM WILL NOT RUN!"

Probably the 3300 time this has been mentioned... a lot of time wasted by people leaving it out...
 

inglewoodpete

Senior Member
From page 27 Manual #1:

" The 10k/22k resistors must be included for reliable operation.
DO NOT leave the serial in pin floating as THE PROGRAM WILL NOT RUN!"

Probably the 3300 time this has been mentioned... a lot of time wasted by people leaving it out...
... and page 27 is followed by several more pages for different PICAXE models, all saying the same message. There's even a Minimum Operating Circuit diagram for each model too.
 
Top