A little help with code please.. Thanks !

xtech007

Senior Member
Once again,
Thank you all for taking the time to help me.
I have read the manuals, and thanks to the many people like westaust55, InvaderZim , BeanieBots , and many others I have been able to get this Far.

I'm using a potentiometer (b1)to get a reading from a steering wheel to the 28X1, according to the reading the 28X1 communicates with I2C to the MD03 controllers moving the motors Left (forward) or Right (reverse).
Also another Potentiometer (b2) to Act as Limit Switches for the motors.

Problem:part 1
How can I make the Motors stop at a Designated b2 Reading?
Can it be done with just b1 only?

Problem:part 2
When I go right (reverse) lets say all the way and turn left (forward), how can I accomplish this movement in the code when b1 still reading <120.

I hope I Explained my trouble so anyone willing to help me can understand.
this is the code I have so far:
Code:
'############################################
'Picaxe 28X1                                                                       
'Using a 10K Potentiometer for Input                                      
'2 DC Motors 24V and an MD03 as H-bridge                              
'############################################


'###################  
'SETUP  
'###################  

Symbol Right=1
Symbol Left=2
Symbol E_right=0
Symbol E_left=0
Symbol Speed_1=100
Symbol Speed_2=200
Symbol Speed_0=0
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:
readadc 0,b1					' read value on ADC0 into variable b1 from pot on steering wheel
readadc 1,b2  	      	  		        ' read value on ADC1 into variable b2 from pot to Stop movement in that direction
if b1<120 then gosub turn_right 		' Go Right
if b2<40  then gosub Far_right		' Stop end travel right
if b1>135 then gosub turn_left 		' Go Left
if b2>215 then gosub Far_left		        ' Stop end travel left
goto main
 

'###################  
'SUBROUTINES
'###################  

Turn_right:
HI2COUT [%10110000], 0, (Right)
HI2COUT 2, (Speed_1)
return

Far_right:
HI2COUT [%10110000], 0, (E_right)
HI2COUT 2, (Speed_0)
return

Turn_left:
HI2COUT [%10110000], 0, (Left)
HI2COUT 2, (Speed_1)
return

Far_left:
HI2COUT [%10110000], 0, (E_left)
HI2COUT 2, (Speed_0)
return
Once again thanks for your time and Help !!!
 
Last edited:

westaust55

Moderator
For part 1:

Your READADC value into the variable b2 many not always increase in increments/steps of 1 due to speed, tolerance etc.

So you really need to compare b1 with b2 +/- say 5 so it will stop if you are close to the desire value. How close or small a tolerance may require you to do some testing.


Try something like:

Code:
SYMBOL proximity = b3
SYMBOL tolerance = 5
:
:
IF  b1 >  b2 THEN
  proximity = b1 &#8211; b2
ELSE
  Proximilty = b2 &#8211; b1
ENDIF

IF proximity < tolerance THEN
  ; do the stopping stuff
ELSE
  ; continue turning motor

For Part 2:
Not sure I fully understand your problem. are you saying the potentiometer only registers down to 120?
ENDIF
You should have the code written so that you can change direction anywhere throught the range of values received from the READADC command.

Maybe you ned to explain a little more fully how and when a turn is to occur.
 
Last edited:

xtech007

Senior Member
Thanks ! for the reply westaust55..

I apologize I did not explain my self on what I'm trying to accomplish.
You mention I should have the code written so that I can change direction anywhere throught the range of values received from the READADC command.
How would I Acomplish that?

I though i did this by:

readadc 0,b1
readadc 1,b2
if b1<120 then gosub turn_right
if b2<40 then gosub Far_right
if b1>135 then gosub turn_left
if b2>215 then gosub Far_left

but like you Mention I can not go back from left to right.

to clearify:
For Part 2:
Not sure I fully understand your problem. are you saying the potentiometer only registers down to 120?
Answer: I was trying to give just an example.. but it ranges from 0-255 is a 10K pot..
thanks for your time.
 

xtech007

Senior Member
I will try to explain it better this time..

to clearify:
For Part 2:
Not sure I fully understand your problem. are you saying the potentiometer only registers down to 120?

Answer: I was trying to give just an example..
It ranges from 0-255 is a 10K pot. where 0 is all the way to the right, 127 center, and 255 all the way to the Right. I used:
if b1<120 then gosub turn_right so when I move the steering wheel off the center towards the right causing the potentiometer attached to the wheel to rotate and start to move the motors at start point from (Start)120-40 at a constant speed.
Same goes for the left, starting from (Start)135-215 at the same speed.
I Don't know how to make it turn in the opposite direction example:
I turn the steering wheel to the Right, pot reading is 80 causing the motor to move in that direction. I slightly turn the steering wheel to the Left now pot is reading 110 but will not rotate in that direction because of the code I used; if b1<120 then gosub turn_right it will only turn in the opposite direction when the pot reads 135 or higher.

the second pot (b2) is attached to the structure reading from the center, So when the structure moves to the right and the pot gets a reading of 40 or less I want the motors to stop. ( to act as a limit switch) but be functional so I can go back the opposite way.
Same goes for the Opposite direction.

I'm really new to the coding part, I hope I explained my self Better... :(

thanks for your time and patience !!!
 

inglewoodpete

Senior Member
My understanding of the following says that the following 2 commands (post #1) will not give you the results you expect:
HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte ; %10110000 = $B0 = default MD03 Slave Address
HI2CSETUP i2cmaster, %10110010, i2cfast, i2cbyte ; %10110000 = $B2 = default MD03 Slave Address
You can only initialise 1 i2c slave address in a master device. Please refer to the 3rd sentence in the first paragraph under "Hi2cSetup - master mode" in the command manual.
 

westaust55

Moderator
12c comms

Well spotted IWP.

I should have picked that after going thru the same issue when I first started using i2c comms.



xtech007,
you only need the first of the two mentioned HI2CSETUP MASTER . . . .
program lines when addressing the MD03 devices.

then when in a subroutine you are addressing the first device use the sub as you already have:
Code:
Turn_right:
    HI2COUT [%101100[B][COLOR="red"]0[/COLOR][/B]0], 0, (Right)
    HI2COUT 2, (Speed_1)
    RETURN
and when you want to address the second MD03 device use code like:
Code:
device2:
    HI2COUT [%101100[B][COLOR="Red"]1[/COLOR][/B]0], 0, (Right2)
    HI2COUT 2, (Speed_2)
    RETURN
 

westaust55

Moderator
xtech007,

Is this code doing what you want?


Code:
'############################################
'Picaxe 28X1                                                                       
'Using a 10K Potentiometer for Input                                      
'2 DC Motors 24V and an MD03 as H-bridge                              
'############################################


'###################  
'SETUP  
'###################  

Symbol Right=1
Symbol Left=2

Symbol M_Stop=0
Symbol Speed_1=100
Symbol Speed_2=200
Symbol Speed_0=0
SYMBOL proximity = b3
SYMBOL tolerance = 5

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


'###################  
'MAIN LOOP
'###################  

Main:
        READADC 0, b1				      	' read value on ADC0 into variable b1 from pot on steering wheel = desired position
        READADC 1, b2  	      	  		      ' read value on ADC1 into variable b2 from pot = actual position



        IF b2 > 40 AND b2 < 215 THEN Check             ' Not at either end stop
        
        GOSUB Motor_stop  ' end travel left or right so STOP
	  GOTO Main

Check:	        
        IF  b1 >  b2 THEN
          proximity = b1 - b2
        ELSE
          proximity = b2 - b1
        ENDIF

        IF proximity < tolerance THEN 
          GOSUB Motor_stop                             ' stop motor as actual position = selected +/- tolerance
        
        ELSEIF b1 > b2 THEN
          GOSUB turn_left                             ' If steering wheel is further left than feedback (b2) then Go Left
        
        ELSEIF B1 < b2 THEN
          GOSUB turn_right  		                  ' If steering wheel is further right than feedback (b2) thenGo Right
        
        ENDIF

        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

Motor_stop:
        HI2COUT [%10110000], 0, (M_Stop)
        HI2COUT 2, (Speed_0)
        RETURN
 

xtech007

Senior Member
Once again Thank you for your Fast response..

Mr westaust55 thank you so much for your patience, I understand your time is very valuable. I will test your code and will post the result. Most important I will
ck each step you added so I can learn more in detail on my own..
I read the post earlier but could not answer anything back (work related issues)
Till I got home. Once again thank you !

you guys are Great..

and yes, I messed up with the second MD03 I2c Master thing.
:rolleyes:
 

xtech007

Senior Member
Sorry for the Delay !!

Mr.westaust55.
Sorry for the Late response.
I have tested the program with small dc motors and seemed to work just fine.

I have bought 2 large Dc motor from Wheel chairs, just waiting to arrive.
I will install them and let you know the damage.. :)

Thanks for the Support.
 
Top