Help Troubleshooting Vol. 2

ahage16

New Member
Alright, Ignore everything prior to this post, I commented all my code and I am going to explain what this is being used for so it will make more sense:

I have a vehicle that I have to program to go a certain distance between 5 to 10 meters in .5 meter increments. I will not know how far it needs to go until I need to run it, so I need it to be adjustable. I am using a 10cm wheel, so the minimum number of revolutions is 50. The .5 meter increments mean I need to increase the number of revolutions 5 times which I do by pressing the input button once per .5 meter increase from 5 meters. It then waits for me to press the button once more to start the motor so I have time to align the vehicle on the track. There is another button on the wheels that works in parallel that is pressed once every revolutions. After the number of revolutions is equal to the number of revolutions wanted, it should go into reverse for 2 milliseconds to cancel out the momentum of the vehicle.

Now the code:
Code:
#picaxe 08
Main:
	let b1 = 50  `min. rev needed
	high 4	`LED on, program started	
	pause 1000
	low 4		`LED off
Counter1:
	for b0=1 to 13  `Loop 13 times
	high 2		`warn light, press if needed   
	pause 1000
	low 2
	if pin3 = 1 then Counter2 `Counter2 adds # of rev needed
	next b0	 `end loop
	goto start	 `goto start after loops
Counter2:
	high 4	 `let you know it counted
	let b1 = b1 + 5	`adds 5 rev 
	pause 1000
	low 4
	goto counter1	`goes back to counter
Start:
	if pin3 = 0 then Start	'Wait for pin3=1
	let b2 = 0		`Clear rev counter
	do			`Start 1st loop
	do			`Start 2nd loop
	high 0		`turn on motor
	loop until pin3 = 1	`second input is on axel/ end of 1st loop until one rev is completed
	let b2= b2 + 1		`adds 1 to # of rev
	loop until b2 = b1	`end of 2nd loop
	low 0		`turn off motor
	high 1	`put motor in reverse (instead of breaks)
	pause 2
	low 1
	goto main	`loop to beginning
The problem starts at "start". Once I press the button to start, the motor will turn on and stay on for one revolution and as soon as it is pressed one time the motor will turn off and the program will restart.
 
Last edited:

inglewoodpete

Senior Member
The lack of response may be related to the difficulty people are having understanding your code. The structure is a bit 'loose' and also needs lots of comments so we can all see what you're trying to do with each step.

I have been a programmer for many years and am struggling to fully understand your code.

Some programming tips:
1. First, I think the following code turns the motor on for 2 milliseconds!
Code:
	high 1
	pause 2
	low 1
2. The following code could be a lot tidier:
Code:
Start:	if pin3 = 1 then variable
	goto start
	 
variable:let b2 = 0
	goto Run

Run:	do.....
Replace it with:
Code:
Start:	if pin3 = 0 then Start	'Wait for pin3=1
	let b2 = 0		'Clear rev counter
	do....
3. When planning a program, I suggest drawing up a flow diagram before you start. It will help you see flaws with your program structure.

Good luck with your program. ;)
 

ahage16

New Member
Thanks for the help! I realized I probably should leave comments along the way because explaining what I need to do is kind of tough in paragraph, so I started doing so in the latest version. The two milliseconds part is intentional but I see how I could have tidied up the code a bit. I'll add some comments to the code so it's easier to read and edit it back in asap.

It;s just kind of odd it doesn't work because the simulation runs perfectly.
 

adub

New Member
I'd use the pin4 you have hooked up to an led as another input.

I've rewritten your program a little.
Warning: Untested Code.

Code:
#picaxe 08
   intput 4
Main:
   let b1 = 50  `min. rev needed
CheckButtons:
   if pin4 = 1 then Start
   if pin3 = 1 then Counter2 `Counter2 adds # of rev needed
   goto CheckButtons
Counter2:
   'high 4	 `let you know it counted
   let b1 = b1 + 5	`adds 5 rev 
   pause 1000'''''I'd also use a smaller value here. 
   ' Waiting a second to push again would be irritating.
   'low 4
   goto CheckButtons	`goes back to counter
Start:
   if pin3 = 0 then Start	'Wait for pin3=1
   let b2 = 0		`Clear rev counter
   do			`Start 1st loop
      do			`Start 2nd loop
         high 0		`turn on motor
      loop until pin3 = 1	`second input is on axle/ end of 1st loop until one rev is completed
      let b2= b2 + 1		`adds 1 to # of rev
   loop until b2 = b1	`end of 2nd loop
   low 0		`turn off motor
   high 1	`put motor in reverse (instead of breaks)
   pause 2
   low 1
   goto main	`loop to beginning
 

ahage16

New Member
hmmm, well I tried it and it would never goto start. It would be able to go to counter2 when I press pin 3 but it wouldn't go to start when I pressed pin 4


Merry Christmas!
 

BrendanP

Senior Member
Have you got the inputs wired as per the manual instructions for digital inputs? You can't leave the inputs floating or they will false trigger.
 

jonphenry

New Member
Ahage, I dont know if I followed you right but here it goes.

When your ready to run, you find out you have to travel 7.5 meters. You flip on your machine which starts a 13 second timer in which time you have to press the button corresponding to pin3. That buttons status is read every second and each time it is pressed it adds 5 to the programmed rev variable(b1), sends an LED on out4 high for a second then turns it off and restarts the 13 second timer. When the timer expends itself, the machine then watches for for pin3 to be high again signaling motor start. AT this time each turn od the wheel pusjes a button on pin 3 indicating 1 rev. When actual revs and programmed revs are equal the moto stops and goes into reverse for to milliseconds to combat inertia.

IE. Turn the machine on. Hold pin3 button down, out4 LED flashes once every second for the next five seconds indicating you require 75 revs from your machine. When you let the button go, 13 seconds go by then your machine watches for the button to be pushed again signaling to start the motor. The motor then starts(high 0) and continues until actual revs(b2) equals programmed revs(b1) then the motor turns off(low 0) and goes into reverse(High 1) for 2 milliseconds.

This is the operation I get from what Ive read. If this is indeed what you want, the following code should work for you.

Code:
#picaxe 08
main:

let b1=50
high 4
pause 1000
low 4

Program:

for b0=1 to 13
  high 2:pause 1000
  if pin3=1 then gosub addrev
next b0
low 2
goto readystart

addrev:

let b1=b1+5
high 4:pause 1000:low 4
let b0=0
return

readystart:

if pin3=1 then start
goto readystart

start:

let b2=0
do
  high 0
  if pin3=1 then inc b2 endif
loop until b2=b1
low 0:high 1:pause 2:low 1
goto main
Hope it works for you.
 
Last edited:

ahage16

New Member
You understood what I was trying to do perfectly, but the program does the same thing that my program did. It will go to start and when I press pin3 the motor flickers on for a second then program restarts

And I'm using a project board, doesn't that take care of floating?
 

hippy

Ex-Staff (retired)
You understood what I was trying to do perfectly, but the program does the same thing that my program did. It will go to start and when I press pin3 the motor flickers on for a second then program restarts
Sound like it could be interference more than anything else. You could try running with a LED+R instead of motor and see if that works better.
 

papaof2

Senior Member
You understood what I was trying to do perfectly, but the program does the same thing that my program did. It will go to start and when I press pin3 the motor flickers on for a second then program restarts

And I'm using a project board, doesn't that take care of floating?
Are you powering the motor from the same supply as the PICAXE? If so, the electrical noise from the motor is probably affecting the PICAXE.

Unless you have looked at the project board diagram and have found the pins to have pull up/down resistors, assume that all the pins are floating.

John
 

BrendanP

Senior Member
I havnt used the project board but decoupling capacitors, 100nf and 10nf across the supply pins might help and alos use them on the on the reset pin. I dont know if the project board has them as standard.

Also, use a green cap across the terminals of the motor you're using to help supress the noise generated into the supply lines by the arcing of the brushes in the motor. The manual recommends a value for that application.
 

ahage16

New Member
Sweet, I figured it out, I was using the wrong type of cap, I didn't realize that it would make that big of a difference. Here's my final code which I tested and works perfectly:
Code:
#picaxe 08
main:
	let b1=50
	high 4
	pause 1000
	low 4
	for b0=1 to 5
  		high 2:pause 500:low 2
  		if pin3=1 then gosub addrev
	next b0
readystart:
	if pin3=0 then readystart
	let b2=0
	high 0
	do
  		if pin3=1 then inc b2 endif
	loop until b2=b1
	low 0:high 1:pause 250:low 1
	goto main
addrev:
	let b1=b1+5
	high 4:pause 500:low 4
	let b0=0
	return
Thanks for all the help, everyone!
 
Top