Help!newbie!trying to write code(basic)for servo controller

i use the basic atom pro28 chip. i know little about programming in basic.i tried to do sth like programming in micro basic studio but i didn’t do much!!!here is my problem!!!
there are 3 servos and 3 scwitches. with the 1st switch i want to control two of the servos. and to be able to turn both in the same direction. with the second switch i want to control the same 2 servos and make them rotate in opposite direction with eachother…and with the last switch iwat to rotate the last servo in both directionsss.plzzz sb help me or guide me so as to find a solution

[code]servo0 var word
servo1 var word
servo2 var word
servo3 var word
servo4 var word
servo5 var word
servo6 var word
buttona var bit

sound 9, [100\880, 100\988, 100\1046, 100\1175]
low 10
low 11
low 12
low 13
low 14
low 15
low 16
goto start
buttona = in4
if buttona = 1 then start

testing:
servo0 = 3048
servo1 = 3048
servo2 = 3048
servo3 = 3048
servo4 = 3048
servo5 = 3048
servo6 = 3048

gosub makepulses
goto testing

start:
adin16 0, servo0
adin16 1, servo1
adin16 2, servo2
adin16 3, servo3
adin16 16, servo4
adin16 17, servo5
adin16 18, servo6

serout S_OUT,i57600, [dec3 servo0\3," "]

servo0 = ((servo1+servo0)* 4)+1000
servo1 = (servo1* 4)+1000
servo2 = (servo2 * 4)+1000
servo3 = (servo3 * 4)+1000
servo4 = (servo4 * 4)+1000
servo5 = (servo5 * 4)+1000
servo6 = (servo6 * 4)+1000

gosub makepulses
goto start

makepulses:
pulsout 10, servo0
pulsout 11, servo1
pulsout 12, servo2
pulsout 13, servo3
pulsout 14, servo4
pulsout 15, servo5
pulsout 8, servo6
pause 10
return
[/code]

You can’t expect people to write your code. Spamming others threads and sending PM’s doesn’t help either.

Post the code you’ve written so far and some of us might be able to help you out.

that was not my intension.i am interested in learning and not making a fool of me…but i need a little help so as to understand what i am not doin right

As Zenta mentioned,

I am less likely to help write code when asked in a PM than I am for posts on forums. Why. When posted, it has the potential to help more than one person…

I am somewhat confused as you mentioned 3 servos, but your code is doing 7… Also not sure what is working or what is not. You might try using the HSERVO system instead of pulsout and see if that helps or not… Also start off with one switch and see if you can get it to work…

Something like:

sw1          var  word
sw1Prev   var  word

Loop:
    adin 0, sw1     ; gives a value between 0-1023 assuming you have 0-5 volts
    if sw1 <> sw1Prev then
        sw1Prev = sw1
        ; HServo will do all of the proper pulses, so you don't need to reissue unless something changes.
        ; Hservo - assumes 0 for mid (ie 1500us), so we should scale our switch value to a valid range. 
        hservo [10\(sw1-512)*20]  ; assume 512 center of range of switch with about a +-1000 ...
    endif

goto Loop

Note: I typed this in on the fly, so don’t know if it compiles or works, but you can try. With this it should move one servo directly with the switch… The mapping to HServo values was arbitrary. Could/Should do mapping function that maps the min and max values of the input switches to the min/max values for the servos… But best to take one step at a time.

Kurt

first, iwant to thank for the time you dedicated to help me.
you are absolutely right about the subject concerning the pm.
i use a 6volt power supply.
i had already tried the hservo command but in the manual syntax did not mention i had to use i.e.pin10, pin11…and not pin 1, pin2, pin3,…that is why i had written more than theservos i needed in the programm. where can i find thesedetails?
the code you wrote is working, but the servo mapping is terrible.how do i do that?
why use adin and not adin16?
hservo [10(sw1-512)*20] : why * and why “20”?“20” is the time the pulse i repeated?
adin 0, sw1 ; gives a value between 0-1023 assuming you have 0-5 volts:can i adjust it or i don’t need to?if yes how do i do that?
with a little bit reading i did wrote the following:

[code]sw1 var word
sw1Prev var word
sw2 var word
sw2prev var word
Loop:
adin 0, sw1 ; gives a value between 0-1023 assuming you have 0-5 volts
adin 1, sw2
if sw1 <> sw1Prev and sw2 <> sw2prev then
sw1Prev = sw1 and sw2Prev = sw2

    hservo [10\(sw1-512)*20,11\(sw2-512)*20]
endif

goto Loop[/code]

Yep, you are getting the idea. Not sure about

sw1Prev = sw1 and sw2Prev = sw2 

I would break those into two lines

sw1Prev = sw1 sw2Prev = sw2

Why *20? Just a random number :laughing: Since your values were ±512, the multiplied values get you somewhere still within the valid range for most servos. Normally I would use some form of map function. Example the one for Arduino looks like:

float map(float value, float istart, float istop, float ostart, float ostop) { return ostart + (ostop - ostart) * ((value - istart) / (istop - istart)); }
Not hard to convert to Basic… The idea is you find out the valid ranges of your switch and get a switch Min and Max (those would by the istart and iend of the above. You play around with the HSERVO command on the servo and find out what the min and max values are for the servo range you desire and you plug those into ostart and oend and the then you plug the value returned by the switch and the output from the equation should give you a good value, if you want a linear direct movement of your servo…

Hope that helps.

Kurt

i do not know how to find out the valid ranges of my servo.it’s brand is futaba S3003.i am searching the net but not much.is there a way to get accurate nalues through programming?is there an equation that has to do with angle and get values from there?

Write a quick and dirty program, something like:

val   var sword

Loop:
    serin s_in, i9600, [sdec val]
    hservo [10\val]
    goto Loop

Then start entering numbers and see where the servo goes to. Just don’t go too far, that is once it looks like you are getting near an edge don’t enter much larger numbers as to make the servo try to go beyond it’s end points…

Kurt

can anyone help me and see what am i doing wrong here and can’t get both servos to work

[code]sw1 var word
sw1Prev var word
sw2 var word
sw2prev var word
ENABLEHSERVO
Loop:
adin 0, sw1
adin 1, sw2
if sw1 <> sw1Prev then
sw1Prev = sw1
ENDIF
if sw2 <> sw2Prev then
sw2Prev = sw2
hservo [12(sw1-512)*20,13(sw2-512)*20]
endif

goto Loop[/code]

Maybe try this…

[code]sw1 var word
sw1Prev var word
sw2 var word
sw2prev var word
ENABLEHSERVO
Loop:
adin 0, sw1
adin 1, sw2
if sw1 <> sw1Prev then
sw1Prev = sw1
ENDIF
if sw2 <> sw2Prev then
sw2Prev = sw2
endif
hservo [12(sw1-512)*20,13(sw2-512)*20]

goto Loop[/code]

i changed the position of the servo pin as you wrote in your code but nothing.they keep moving seperately

i changed the position of the pins as you wrote and tried it out but nothing.i have uploaded a picture of my bot board an bap pro 28 connections if it has anything to do with that and the code i had wrote at the beggining.i have read the manual and studied the code.it’s not that i am lazy and want it ready by others
asdforiginale.bas (1.02 KB)

it’s a pity to lose another semester …i just want to make 2 servos move in the same direction with one switch and in opposite direction with the second switch

Here is a super simple program you can hack away at…

[code]servo0 var word
servo1 var word
servo2 var word
buttona var bit
buttonb var bit
buttonc var bit

sound 9, [100\880, 100\988, 100\1046, 100\1175]
low 0
low 1
low 2

start:
buttona = in12
buttonb = in13
buttonc = in14
if buttona = 0 then button_a_process
if buttonb = 0 then button_b_process
if buttonc = 0 then button_c_process
goto servo_pulse_generation

button_a_process:
servo0 = 2000
servo1 = 4000
servo2 = 4000
goto servo_pulse_generation:

button_b_process:
servo0 = 4000
servo1 = 4000
servo2 = 2000
goto servo_pulse_generation:

button_c_process:
servo0 = 2000
servo1 = 2000
servo2 = 4000

servo_pulse_generation:
pulsout 0, servo0
pulsout 1, servo1
pulsout 2, servo2
pause 10
goto start[/code]

thank you very much for the time you are dedicating!with the hservo commands this code works[code]hservo [p0\0,p1\0] ;set initial servo positions(centered)
main
hservo [p0\1000\100,p1-1000\100]
pause 1000
hservo [p0-1000\100,p1\1000\100]
pause 1000
goto main

[code]
but when i try with the switches its dead!!!
the programm you wrote is very goog but its still dead.i sending a photo of my swithes.they are very simple and aren’t buttons

This picture is no help. Do you have a schematic of how the switches are wired?

I have read this thread from the top. You have changed the descriptions of how it is supposed to work.

What is it supposed to do exactly…

I don’t use Hservo. So I can’t help you with that.

If you want to try my program then I will try to help you get it to work.

ok!i want to try your programm!definitely!i have switch1 in p0,switch2 in p1 and switch 3 in p2. the output is the following:p10=servo0,p11=servo1 and p12= servo2. it’s a project.it doesn’t do anything.switch1 must move servo1 and servo2 in the same direction from 0-180 degrees.the same must do and servo2 but in opposite directions and servo3 must move independent from 0-180 degrees

[code]buttona = in12 ’ this tells you where to plug in the switches.
buttonb = in13
buttonc = in14

pulsout 0, servo0 'this tells you where the servos are plugged in.
pulsout 1, servo1
pulsout 2, servo2[/code]

You MUST understand this is not enough…

“switch1 must move servo1 and servo2 in the same direction from 0-180 degrees.the same must do and servo2 but in opposite directions and servo3 must move independent from 0-180 degrees”

When you press the button what happens? Does the servo move quickly or slowly? Can’t you make a table or something?

i did the wiring as you told me and when i turn switch1,switch2 or switch3 it does nothing.