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

I wrote a quick and dirty using the 3 switches on the BB2 and 3 servos… I used HServo. I set it up such that you can set MIN/MAX values differently for each servo. I did different things for different buttons. So you should be able to mix and match to do however you want.
Switch A : Moves 2 servos in the same direction. Always the same direction…
Switch B : moves 2 servos in opposite direction. If you release the button and press again, then the both go the opposite direction of the direction they were going the previous time you pressed the button.
Switch C: only moves the 3rd servo. It goes all the way up to max, reverses direction and goes all the way to min…

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

s1Mult var sword
s2Dir var bit

btnbPrev var bit

S0MIN con -12000
S0MAX con 12000
INCRVAL con 250
S1MIN con -12000
S1MAX con 12000
S2MIN con -12000
S2MAX con 12000

servo0 = 0
servo1 = 0
servo2 = 0
s2Dir = 0
s1Mult = -1
btnbPrev = 0 

sound 9, [100\880, 100\988, 100\1046, 100\1175]

start:
btnbPrev = buttonb ; save previous state.

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 start

button_a_process:
servo0 = servo0 + INCRVAL
servo1 = servo1 + INCRVAL
; sound 9, [60\4000]
goto servo_pulse_generation:

button_b_process:
if btnbPrev then
s1Mult = -s1Mult
endif
servo0 = servo0 + (INCRVAL * s1Mult)
servo1 = servo1 - (INCRVAL * s1Mult)
; sound 9, [60\4500]
goto servo_pulse_generation:

button_c_process:
; sound 9, [60\5000]
if s2Dir then
Servo2 = Servo2 + INCRVAL
if servo2 >= S2MAX then
servo2 = S2MAX
S2Dir = 0
endif
else
Servo2 = Servo2 - INCRVAL
if servo2 <= S2MIN then
servo2 = S2MIN
S2Dir = 1
endif
endif

servo_pulse_generation:
; make sure Servo 0 and 1 are in range…
servo0 = (servo0 max S0MAX) min S0MIN
servo1 = (servo1 max S1MAX) min S1MIN
HServo [p0\servo0, P1\servo1, P2\Servo2]
pause 25
goto start[/code]
I am not saying this is great code, but should get you moving.
Kurt

thank you very very much.if there is anything i can do…

[code]switch0 var word
switch1 var word
switch2 var word

low 10;servo1
low 11;servo2
low 12;servo3

start:
adin 0, switch0;my switches are only read analog
adin 1, switch1;
adin 2, switch2;

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

switch0 = ((switch0+switch1)4)+1000 ;working:moving servo0&servo1 in the same direction and stopping anywhere i want
switch1 = (switch0
4+1000)+(switch1*4-1000);not working:moving servo0&servo1 in the opposote direction and stopping anywhere i want
switch2 = (switch2 * 4)+1000

gosub makepulses
goto start

makepulses:
pulsout 10, switch0
pulsout 11, switch1
pulsout 12, switch2

pause 10
return[/code]
my professor wasn’t satisfied.he said “your switches should be read only analog.stick to what you already wrote and work it out”.one last try…

Sounds like you need to work with him more closely. I have no idea what he is trying to say. Analog switches… :unamused:

Me neither. My best guess is he is thinking instead of push buttons or the like, that he should have resistor pots, where you should use ADIN to get the values and then do things like add or subtract values to each other, to then figure out where each servo should be…

Kurt

Crosspoint switches?

But “Read only”??

Professor of what? Try to get an exact quote.

Alan KM6VV

And what version is he referring to here?

i tried to upload a video i took with my cellphone.it shows the type of switches and the wiring.from right to left(red,white black)
my power supply is ±5volts.the jumpers above the pins on the bot board are all set to 5v, not vs. the code i wrote so far is the following.

[code]switch0 var word
switch1 var word
switch2 var word

low 10;servo1
low 11;servo2
low 12;servo3

start:
adin 0, switch0;my switches are only read analog
adin 1, switch1;
adin 2, switch2;

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

switch0 = ((switch0+switch1)4)+1000 ;working:moving servo0&servo1 in the same direction and stopping anywhere i want
switch1 = (switch0
4+1000)+(switch1*4-1000);not working:moving servo0&servo1 in the opposote direction and stopping anywhere i want
switch2 = (switch2 * 4)+1000

gosub makepulses
goto start

makepulses:
pulsout 10, switch0
pulsout 11, switch1
pulsout 12, switch2

pause 10
return[/code].any ideas?how can i help you help me?and yes i think the switches are called resistor pots.

the video is around 2,7mb and the its not possibleto upload

If they are potentiometers (variable resistors), then you put a voltage across the ends of the pot and then read the wiper with an A/D channel. They aren’t switches. Call them pot1, pot2, etc.

Post video on youtube, an post link.

Alan KM6VV

yes they are called potentiometers.he wants me to make a project with a bot board II that has a basic atom pro 28 controller.the project is like this:
-with the first switch he wants to be able to move manually the first two servos anywhere from range -90deg to +90deg in the same direction.
-with the second switch he wants to be able to move manually the same to servos in opposite direction anywhere from range -90degr to +90degr but with the same rate and distance.
-with the third switch he wants to be able to move it manually anywhere fron range -90degr to +90degr.
the thing is i do not know how to write any better code than the following and it does not work

[code]switch0 var word
switch1 var word
switch2 var word

low 10;servo1
low 11;servo2
low 12;servo3

start:
adin 0, switch0;my switches are only read analog
adin 1, switch1;
adin 2, switch2;

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

switch0 = ((switch0+switch1)4)+1000 ;working:moving servo0&servo1 in the same direction and stopping anywhere i want
switch1 = (switch0
4+1000)+(switch1*4-1000);not working:moving servo0&servo1 in the opposote direction and stopping anywhere i want
switch2 = (switch2 * 4)+1000

gosub makepulses
goto start

makepulses:
pulsout 10, switch0
pulsout 11, switch1
pulsout 12, switch2

pause 10
return[/code]

They’re pots… Stop calling them switches. :unamused:

So, you know how to make servo pulses, and how to make the analog inputs work. 8)

Your description of how it is supposed to work is still confusing. I do not understand what the third pot does.

Do you have the programming manual?

Experiment with the min and max commands. So you can keep the servo pulses in range by limiting the math that is being done.

Example:

servo1 = servo1 + x (max 4000) ’ this will limit the result to not go above 4000.

Your program is solid, but it’s the math that needs to change. Like your professor said, stick to that program and work it out. Show us what you have tried.

It is probably because you are using the variables for two different things, that is if you look at this code:

switch0 = ((switch0+switch1)*4)+1000 ;working:moving servo0&servo1 in the same direction and stopping anywhere i want switch1 = (switch0*4+1000)+(switch1*4-1000);not working:moving servo0&servo1 in the opposote direction and stopping anywhere i want switch2 = (switch2 * 4)+1000
Lets assume for now you have a full range of ADIN and center is 512 and all three of your switches read in 512…
So: you run your first line of code here: switch0 = ((switch0+switch1)*4)+1000
Now the Value of switch0 is (512+512)*4+1000 which is: 5096

Now you execute: 2nd line: yout get switch1 = (50964 + 1000) + (5124 -1000) = 20336
Which is probably not what you want… Probalby don’t reuse the switch variables and introduce new variables for your calculations…

Kurt

I now understand the third pot. duh!

You are using two pots to control two servos. There will be times when one servo or another will hit a limit. I assume you want the servo that is not at it’s limit to keep moving. This is where the min and max commands will come into play. Probably 12 different ways to to the same thing.

you are absolutely right!!!
my variariable names must change but i can’t figure out a way not come across with a mathematical issue like the above

and probably not for the first time you come across with somebody who doesn’t know even one of them.i must find a way to make them both move from ie.-12000 to +12000 and with the second switch make them move in opposite direction.
i run the following programm.

Pot var word Main adin P0, pot ;read analog pin P0, load result into POT serout s_out, i9600, [0, “Analog Value = “, dec pot] Pause 50 Goto Main .
it gives a result of 0 to 1009 which are the “limits” of pot(?).can i use this to coordinate the servos moving isame or opposite direction?
how can i use these values to accomplish my goal

i already did that whith the followin programm i thinkPot var word Main adin P0, pot ;read analog pin P0, load result into POT serout s_out, i9600, [0, “Analog Value = “, dec pot] Pause 50 Goto Main .it gives a value of 0-1009.now what?

Now you’ve got a pot range, scale it as Kurt instructs above.

Alan KM6VV

i do not know how to do that in basic.
from this code:[code]#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there [/code]
i did this:

[code]servo1 var word
servo2 var word
servo3 var word
pot1 var word
pot2 var word
pot3 var word
val1 var word
val2 var word
val3 var word
start:
adin 0, pot1
serout s_out, i9600, "Analog_Read = ", dec3 pot1]
adin 1, pot2
serout s_out, i9600, "Analog_Read = ", dec3 pot2]
adin 3, pot3
serout s_out, i9600, "Analog_Read = ", dec3 pot3]

val1= pot1
val2= pot2
val3= pot3
servo1= val1
servo2=val2
servo3=val3
gosub makepulses

makepulses:
pulsout 0, servo1
pulsout 1, servo2
pulsout 2, servo3
pause 15
goto start
[/code].
i have stuck in the maping function…

The map function is just some simple math. Again from page 1 of this thread:

float map(float value, float istart, float istop, float ostart, float ostop) { return ostart + (ostop - ostart) * ((value - istart) / (istop - istart)); }
Note: with fixed point math, I would actually do the ()s a bit different that is:

int map(int value, int istart, int istop, int ostart, int ostop) { return ostart + ((ostop - ostart) * (value - istart)) / (istop - istart); }
Why? Because when you do the divide you simply get whole numbers and you lose your precession…
Could put this inline in Basic, could create function…

value var slong
istart var slong
istop var slong
ostart var slong
ostop var slong

map[value, istart, istop, ostart, ostop]:
    return ostart + ((ostop - ostart) * (value - istart)) / (istop - istart);

To call it in your case could:

gosub map[val, 0, 1023, 0, 179], val

Again I probably mistyped stuff. If so look it up in the manual…

Kurt