Coding Problems

g0bl1n

Member
I have been using the arduino for a while now, and figured I would try the picaxe. It seemed that the picaxe would be like the arduino coding except in basic, but I guess I'm just not getting it. I have a simple robot with two motors and an ir detector. I know all of the above works because I double checked them on an arduino, so the problem lie in my coding.

Its not so much my coding isn't working, as it isn't doing what I want. I have an h-bridge connected to the picaxe controlling two motors. These are the pins for the current setup:
Picaxe 20x2 (Based on page 27 of manual #1)
Pin 18 - IR Detector
Pin 17 - Motor 1 Forwards
Pin 16 - Motor 1 Backwards
Pin 15 - Motor 2 Forwards
Pin 14 - Motor 2 Backwards

I then wrote this program for this bot:
Code:
main:
	readadc 1,b0
	if b0>200 then
		gosub m_forward
		pause 100
	else
		gosub m_stop
		pause 10
		gosub m_reverse
		pause 100
		gosub m_stop
		pause 10
		gosub m_right
		pause 100
		gosub m_stop
		pause 10
		if b0>200 then
			goto main
		else
			gosub m_left
			pause 200
			gosub m_stop
			pause 10
			if b0>200 then
				goto main
			else
				gosub m_left
				pause 100
				gosub m_stop
				pause 10
				goto main
			endif
		endif
	endif
goto main

m_stop:
	low 1
	low 2
	low 3
	low 4
	return

m_forward:
	high 1
	low 2
	high 3
	low 4
	return

m_reverse:
	low 1
	high 2
	low 3
	high 4
	return

m_right:
	low 1
	high 2
	high 3
	low 4
	return
m_left:
	high 1
	low 2
	low 3
	high 4
	return
When this did not work (motors were working, but not when they were supposed to) I tried smaller programs just to see what pin was which. I tried the following:
Code:
main:
	high b.1
	pause 10000
	low b.1
goto main
Code:
main:
	high b1
	pause 10000
	low b1
goto main
With several different numbers of course. I then tried to use 17 to 14 but the programmer says that these pins do not exist on a 20x2. I've done reading in all three manuals but I have yet to figure out why this is not working. I know it has to be the way I am using my pins, but I do not know in what way.

The more I read I guess I am supposed to use portb 1 to portb 4? When I was looking at the 28x2 information it says all of it's ports are fixed as output only, so I guess if that is true for the 20x2 as well, then I'm going to have to move the IR input to another pin...or all of my stuff to port c?

So just to sum this post up my two questions are:
How do I use the pins in my code?
Do I have to move everything?
 

hippy

Ex-Staff (retired)
The following should put a 1Hz signal out on B.1 ( leg 17 ). It works for me anyway ...

#Picaxe 20X2
#No_Table
#No_Data
Do
Pause 500 : High B.1
Pause 500 : Low B.1
Loop
 

womai

Senior Member
Note that in your code

main:
high b.1
pause 10000
low b.1
goto main

the pin will be high virtually all the time because there is no pause between low and high (only between high and low). What you reall want is probably

main:
high b.1
pause 10000
low b.1
pause 10000
goto main
 

g0bl1n

Member
Can I not set the pin to high and leave high? Such as:
Code:
main:
high b.1
goto main
If I do a pause and turn it to low it works perfectly, however if I try and set it to high it wont come on at all. Is this not working because of something I did or because it is how the picaxe works?
 

hippy

Ex-Staff (retired)
main:
high b.1
goto main


Works fine for me, pin B.1 ( leg 17 ) goes high and stays high.
 

g0bl1n

Member
Interesting, when I tried it last night it wasn't working, but tonight it is. Weird... About my second inquiry, I've been playing around with trying to read the IR detector through ADC on b.0, but I can't seem to get it to work. Pin b.0 is ADC1, so looking through some examples this is what I came up with:
Code:
#picaxe 20x2
#no_table
#no_data

main:
	readadc 1,b0
	if b0>50 then
		high b.1
	else
		low b.1
	endif
goto main
However that motor always seems to be on. I know that b0 in the above code isn't the pin just a variable. On the arduino I had the IR on an analog port and read it from 0-1023. As I understand it I can do the same by using w0, w1, w2, etc... instead of the above b0. However I still cannot get it to work. So I'm guessing the all of the b pins are output only then? If so could I successfully read the IR detector on the a.0 pin(its analog I believe)? Or would I have to move the IR detector to one of the c. pins with ADC?
 

inglewoodpete

Senior Member
Try temporarily adding the debug command:
Code:
#picaxe 20x2
#no_table
#no_data

main:
	readadc 1,b0
	Debug
	if b0>50 then
		high b.1
	else
		low b.1
	endif
goto main
 

g0bl1n

Member
The problem with using the debug command is that I have to program the picaxe in a programmer then put it in my project, there wasn't enough room to add in everything for programming. But since you say use the debug command can I assume that pin b.0 on the 20x2 can be an input?
 

Technical

Technical Support
Staff member
When pin b.1 (leg 18) is used with the ReadADC command as "ReadADC 1, ....", it automatically becomes an (analogue) input.
Actually on X2 parts it doesn't, due to the greater configuration options on all i/o pins. You need to set it up as an input pin and use 'adcsetup' - see adcsetup in part2 of the manual.

ADCs will all work without adcsetup being correctly configured, but may not be so accurate.
 
Top