Finally my first code.. Its more like help.

xtech007

Senior Member
Wow !
I spend hours reading manuals and the forum and this is my first code.
The Manuals are very explanatory when it comes to the programing editor software. I got to use it within few days. But the code, the code , the code,its killing me.

I adventure to try and this is what I came up with.

I can get it to run cam someone please help.. or guide me ?
Thank you.
 

Attachments

manuka

Senior Member
Welcome-this looks an ambitious start however! Did you flash a few LEDs to get the hang of things first? Ahem- you'll need to tell us just what you are trying to do- it looks like vehicle control, but that "cam" may mean a camera pan? How about a picture or/& schematic? Most of us regulars are lateral thinkers, but we can't usually visualise your creation just from such coding.

Great start with the ;, but you really need more -it's normal practise of course to abundantly comment code lines (most of us use '), & also include preceeded headers outlining the project etc. This forum usefully lets code be displayed in postings using square [ ] brackets around the word code, & a final slash before this word at the end - the end result is as below

Code:
;turn left & Right using Picaxe 28X1 
		
		
Symbol Right=b1 
Symbol Left=b2
Symbol Speed_1=b6
Symbol Speed_2=b7

let Left=1
let Right=2
let Speed_1=100
Let Speed_2=200

main:
	if pin1=1 then turn_right
	if pin2=1 then turn_left
	
Turn_right:

	HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  
	; %10110000 = $B0 = default MD03 Slave Address
HI2COUT 0, Right			
HI2COUT 2, Speed_1

Turn_left:
	HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  
	; %10110000 = $B0 = default MD03 Slave Address
HI2COUT 0, Left
HI2COUT 2, Speed_2

goto main
 
Last edited:

Pekari

Senior Member
You don't need to use variables if you are NOT changing value.
You can write like this:

Code:
Symbol Left=1
Symbol Right=2
Because usually there is not too many variable to use.
 

BCJKiwi

Senior Member
True Pekari, you don't NEED to use symbols but it's a good habit to get into!

Also on the hi2csetup command, this is only required once in the program unless there is more than one i2c device (and therefore different addresses - but even then you can include the newslave address in the hi2cout line).

In this case, a single hi2csetup line before the main loop will set up the i2c bus parameters and then the hi2couts are all that is required in the repeating part of the program. The hi2couts also require ( )s around the data.

Note also that every time there is a turn right there will also be a turn left as when the turn right branch is executed the code will simply pass on to the turn left section. So the code needs to return to Main after each subsection. This may be achieved by placing another goto main at the end of turn right, or, alternatively change the GOTOs into gosubs, and put a goto main after the first chunk of logic.

i.e.
1. there is a setup section - the symbol declarations and anything else that only happens once on startup

2. there is a main logic loop

3. there are any other logic blocks

4. there are all the subroutines

I also recommend declaring the PICAXE that is to be used at the beginning of the code. This ensures the programmer knows which PICAXE the code is for so if you swap between different picaxes, you don't need to manually change the default in the PE and you don't get errors because the 28X1 code is trying to run in a 14M because that was the last chip you were programming! ( I figure this should be a requirement but Rev-Ed obviously don't agree).

e.g.
Code:
#Picaxe 28X1
;turn left & Right using Picaxe 28X1 

'###################  
'SETUP  
'###################  
Symbol Right=b1 
Symbol Left=b2
Symbol Speed_1=b6
Symbol Speed_2=b7
let Left=1
let Right=2
let Speed_1=100
Let Speed_2=200
 
HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  
; %10110000 = $B0 = default MD03 Slave Address

'###################  
'MAIN LOOP
'###################  
main:
 if pin1=1 then gosub turn_right
 if pin2=1 then gosub turn_left
goto main
 
'###################  
'SUBROUTINES
'###################  
Turn_right:
HI2COUT 0, (Right)
HI2COUT 2, (Speed_1)
return
 
Turn_left:
HI2COUT 0, (Left)
HI2COUT 2, (Speed_2)
return
 

InvaderZim

Senior Member
As Pekari said, if you need to make a symbol for a constant, you can use
Code:
symbol thing=1
instead of
Code:
symbol thing=b1
let b1=1
Your code checks for a right turn, then a left turn; but if neither check works, it will *still* make a right turn and a left turn. You should have a "goto main" at the end of your main section.
Code:
main:
	if pin1=1 then turn_right
	if pin2=1 then turn_left
goto main
Try simulating, and 'step' through each program line.
 

westaust55

Moderator
xtech007 has unfortunately not advanced greatly greatly from his earlier thread on the exactly same topic in which he was given examples that included comments and some advice on symbols plus the examples had brackets around the data portion of i2c commands:
http://www.picaxeforum.co.uk/showthread.php?t=10540

BCJKiwi has hopefully put Xtech007 on the right track this time . . .
 
Last edited:

Pekari

Senior Member
BCJKiwi;81359]True Pekari, you don't NEED to use symbols but it's a good habit to get into![/QUOTE]

Read again my post.
 

xtech007

Senior Member
Once again thank you so much for your post and help!

I will try each and make modifications.
I will re-write the code and let you guys of any progress.
OHH and I will post wiring and pictures of the project i'm working.

Thanks..
 

westaust55

Moderator
what would be for address 0xB2 ??
$B2 = %10110010



Xtech,
It would be worth learning the conversions.
A hexadecimal number uses the digits/characters 0 to 9 and A to F, where A = 10 through to F = 15
The conversion between decimal hex and binary is:
0 = $0 = %0000
1 = $1 = %0001
2 = $2 = %0010
3 = $3 = %0011
4 = $4 = %0100
5 = $5 = %0101
6 = $6 = %0110
7 = $7 = %0111
8 = $8 = %1000
9 = $9 = %1001
10 = $A = %1010
11 = $B = %1011
12 = $C = %1100
13 = $D = %1101
14 = $E = %1110
15 = $F = %1111
Have a study of these and understand how the bits roll/toggle to increment the numbers.
A byte variable has a value from 0 to 255 or $0 to $FF or %0000000 to %11111111


EDIT:
It so happens that I have recently suggested to Rev Ed to include a lookup/conversion table in their Manuals as I can understand that many newcomers to computing do not understand the bases systems and conversion as readily as those who have spent 40+ years working with them.
 
Last edited:

BeanieBots

Moderator
Good idea westaust. Might also be worth including ASCII in the lookup.
Decimal 0 to 255.
Dec - ASCII - Hex - Bin
Would fit nicely on one appendix page with a few columns.
 

westaust55

Moderator
Hi BB,

yes, 0 to 255 was exactly what I had suggested to Rev Ed (along with some other edits/clarificatiosn).
base conversions do seem to be a stumbling block


To incorporate the ASCII codes, as you suggest, would be even better and still fit on a page.

Technical, are you reading ? ? . . . . .
 

BeanieBots

Moderator
Indeed there are many such tables out there. However, it would be much better and easier to be able to refer 'newbies', (especially those that don't like doing their own research) to a manual page.
 

xtech007

Senior Member
YEahh ! Something new to learn ..

I had read something like that on the manual but didn't know where to start.
specially why the change from address 0xB0 to $B0 and then to %10110000.
Now I know when the character shows %10110000 its hex and binary conversion. I will take a better look at that section.

Once again thanks a Million..
Trust me I ask for help becouse I have exausted my own resourses not that I'm lazy. i have confused my self many times.
The code I wrote at the beggining of the thread took me almost a week to understan. But I'm Proud I did it .
So far All I have to show is what the forum have contributed to my post.
I really thank you....
I have learned plenty. and willing to go farther..
You guys are great !!!!!!
 

xtech007

Senior Member
Yeahhh its done !!!

this is it. and yes! its like a vehicle control.
turn left & right
Go up and down.
if anyone was currious of what i'm making. Well its a Flight/Racing Simulator.
The reason I'm Using the Picaxe its to make my own DC motor controller to interface with the racing wheel with forceback and Joystick from the PC or console controllers.

The picaxe hopefully will control the motion for the structure. (fingers crossed) the inputs to the game will be from the game controller. Well hacked I will say. I will post some picture if anyone is interested.

I will finish some welding and will post what the stucture looks like.
This is what the code looks like:
Code:
#Picaxe 28X1
;turn left & Right using Picaxe 28X1 

'###################  
'SETUP  
'###################  
Symbol Right=1
Symbol Left=2
Symbol Up=1
Symbol Down=2
Symbol Speed_1=100
Symbol Speed_2=200


'###################  
'MAIN LOOP
'###################  
main:
 if pin1=1 then gosub turn_right
 if pin2=1 then gosub turn_left
 if pin6=1 then gosub turn_up
 if pin7=1 then gosub turn_down
goto main
 
'###################  
'SUBROUTINES
'###################  
Turn_right:
HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  ; %10110000 = $B0 = default MD03 Slave Address
HI2COUT 0, (Right)
HI2COUT 2, (Speed_1)
return
 
Turn_left:
HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  ; %10110000 = $B0 = default MD03 Slave Address
HI2COUT 0, (Left)
HI2COUT 2, (Speed_1)
return

Turn_up:
HI2CSETUP i2cmaster, %10110010, i2cfast, i2cbyte  ; %10110000 = $B2 = default MD03 Slave Address
HI2COUT 0, (Up)
HI2COUT 2, (Speed_1)
return
 
Turn_down:
HI2CSETUP i2cmaster, %10110010, i2cfast, i2cbyte  ; %10110000 = $B2 = default MD03 Slave Address
HI2COUT 0, (Down)
HI2COUT 2, (Speed_1)
return
- the end
 

BCJKiwi

Senior Member
See you missed the point about only needing the Hi2csetup once and using the [newslave] with hi2cout!

put a single
HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte
line in the setup section.

in the other lines in the subroutines, eliminate the hi2csetup lines and use;
Code:
Turn_left:
HI2COUT [%10110000], 0, (Left)
HI2COUT 2, (Speed_1)
return
just like it says in the manual.

You only need to use the [newslave] part each time the slave changes. As these are all subroutines and could be called in any order, use [newslave] in the first hi2cout in every subroutine.

You should also replace the last line;
- the end

with a line the only contains;
End

There is actually an END command which will termintate the code rather than have the chip go play with the faeries if it loses its way.
 
Last edited:

xtech007

Senior Member
I think i made a mistake..

-the end statement i placed was a mistake it was to post the code. not end the code..
Jajajajajaj.. I see the confision.. :))

the reason I used the line
HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte '%10110000 = $B0
&
HI2CSETUP i2cmaster, %10110010, i2cfast, i2cbyte '%10110010 = $B2

was becouse I tought that when you have 2 different MD03 Slave Address
you had to write to each comand.

or is there a more efficient way?
like the way you described abobe?
 

xtech007

Senior Member
How about now!

Cool.. Now it looks more Cleaner..
Code:
#Picaxe 28X1
;Turn Left,Right,Up & Down

'###################  
'SETUP  
'###################  
Symbol Right=1
Symbol Left=2
Symbol Up=1
Symbol Down=2
Symbol Speed_1=100
Symbol Speed_2=200

HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  ; %10110000 = $B0 = default MD03 Slave Address
HI2CSETUP i2cmaster, %10110010, i2cfast, i2cbyte  ; %10110000 = $B2 = default MD03 Slave Address

'###################  
'MAIN LOOP
'###################  
main:
 if pin1=1 then gosub turn_right
 if pin2=1 then gosub turn_left
 if pin6=1 then gosub turn_up
 if pin7=1 then gosub turn_down
goto main
 
'###################  
'SUBROUTINES
'###################  
Turn_right:
HI2COUT [%10110000], 0, (Right)
HI2COUT 2, (Speed_1)
return
 
Turn_left:
HI2COUT [%10110000], 0, (Left)
HI2COUT 2, (Speed_1)
return

Turn_up:
HI2COUT [%10110010], 0, (Up)
HI2COUT 2, (Speed_1)
return
 
Turn_down:
HI2COUT  [%10110010],0, (Down)
HI2COUT 2, (Speed_1)
return
END
 

westaust55

Moderator
Okay that is getting better however a couple more comments for you:

1. You only need one HI2CSETUP command in the initialisation part at the top.
Only the last HI2CSETUP command is made current and used. So delete the green line

2. You can (not essential) use SYMBOL commands to give some meaningful descriptions to the i2c device slave addresses. See what I have done in red
these do not use up variables they are just constants that the programming editor inserts the vales whereever the name is used when the program is downloaded into the PICAXE.

3. to make your program code appear in a subwindow put the instruuction [*code] at start and [*/code] at the end of your section of code.
DO remove the asterirks (*) I put in here to stop them being used as instructions within this sentence.
This is what BCJKiwi was trying to get you to do.


Code:
#Picaxe 28X1
;Turn Left,Right,Up & Down

'################### 
'SETUP 
'################### 
Symbol Right=1
Symbol Left=2
Symbol Up=1
Symbol Down=2
Symbol Speed_1=100
Symbol Speed_2=200

[B][COLOR="Red"]SYMBOL leftright = %10110000
SYMBOL updown = %10110010[/COLOR][/B]

HI2CSETUP i2cmaster, %[COLOR="Red"][B]leftright[/B][/COLOR], i2cfast, i2cbyte ; %10110000 = $B0 = default MD03 Slave Address
; next line is not needed
[COLOR="Green"]HI2CSETUP i2cmaster, %10110010, i2cfast, i2cbyte ; %101100[/COLOR][B][COLOR="red"]1[/COLOR][/B][COLOR="green"]0 = $B2 = default MD03 Slave Address[/COLOR]
'################### 
'MAIN LOOP
'################### 
main:
if pin1=1 then gosub turn_right
if pin2=1 then gosub turn_left
if pin6=1 then gosub turn_up
if pin7=1 then gosub turn_down
goto main

'################### 
'SUBROUTINES
'################### 
Turn_right:
HI2COUT [[B][COLOR="Red"]leftright[/COLOR][/B]], 0, (Right)
HI2COUT 2, (Speed_1)
return

Turn_left:
HI2COUT [[B][COLOR="red"]leftright[/COLOR][/B]], 0, (Left)
HI2COUT 2, (Speed_1)
return

Turn_up:
HI2COUT [[COLOR="red"][B]updown[/B][/COLOR]], 0, (Up)
HI2COUT 2, (Speed_1)
return

Turn_down:
HI2COUT [[COLOR="red"]updown[/COLOR]],0, (Down)
HI2COUT 2, (Speed_1)
return
END
 
Last edited:
Top