servo left, middle and right

10 Feb 2012

hello,

i don´t find a code to control a servo over PWM into a DigitalIN.

my idea:

if DigitalIn left(p10)..............servo get to the left

if not DigitalIn left(p10)..........servo get to the middle

if DigitalIn right(p11..............servo get to the right

if not DigitalIn rigth(p11).........servo get to the middle

i have no ideas to do that? is this difficulty?

can anywhere help me??

thanks

10 Feb 2012

Checkout this page: http://mbed.org/cookbook/Servo

It should be easy to change this software into what you need by using 2 DigitalIn pins to control the servo instead of characters received on the serial port.

10 Feb 2012

i know.

my idea

  1. include "mbed.h"
  1. include "Servo.h"

DigitalIn taste(p10);

DigitalIn taste2(p12);

DigitalIn taste3(p11);

Servo myservo(p21);

int main() {

float range = 0.0005;

float position = 0.5;

while(1)

{

switch() {

case 'taste': position = 0.0; break;

case 'taste2': position = 0.5; break;

case 'taste3': position = 1.0; break;

myservo.calibrate(range, 45.0);

myservo = position;

} }

10 Feb 2012

it dosen´t work it

i am a absolute beginner

10 Feb 2012

For better visual code, using <<code>><</code>> tags is an option, just so you know it.

You want your servo to turn left, when told so, middle when told so, and right when told so?

include "mbed.h"
include "Servo.h"

DigitalIn taste(p10);
DigitalIn taste2(p12);
DigitalIn taste3(p11);
Servo myservo(p21);

int main() {
    float range = 0.0005;
    float position = 0.5;
    while (1) {
        switch () {
            case 'taste':
                position = 0.0;
                break;
            case 'taste2':
                position = 0.5;
                break;
            case 'taste3':
                position = 1.0;
                break;
                myservo.calibrate(range, 45.0);
                myservo = position;
        }
    }
}

That's your program. Try removing calibrate from the switch case, maybe that's doing something to it?

Lerche

10 Feb 2012

oh thanks for the tip.

jes this is the program that i written (copy/paste from servo page).

i don´t will use the usb serial with the key from keyboard (1,2,3...).

i will use the digital in options. I have 2 switches plugged to the mbed. the first for left forwarding, the second for right forwaring. is no switched to be used the servo go to the middle.

thanks for helping

11 Feb 2012

This should work. The switch statement above was wrong.

include "mbed.h"
include "Servo.h"

DigitalIn taste(p10);
DigitalIn taste2(p12);

Servo myservo(p21);

int main() {
   float range = 0.0005;
   float position = 0.5;
   myservo.calibrate(range, 45.0); // init the servo
 
   while (1) {  // Do the next actions forever
        position = 0.5;      // default is middle position (no or both buttons pressed)
                                           
        // test to see if any buttons are pressed
        if (( taste) & (!taste2)) position = 0.0; // left on only taste pressed 
        if ((!taste) & ( taste2)) position = 1.0; // right on only taste2 pressed

        myservo = position;             // activate the new position
  }
}

The switch statement could be used like this.

include "mbed.h"
include "Servo.h"

DigitalIn taste(p10);
DigitalIn taste2(p12);
DigitalIn taste3(p11);
Servo myservo(p21);

int main() {
    int code;
    float range = 0.0005;
    float position = 0.5;
    myservo.calibrate(range, 45.0);

    while (1) {
        code = (taste3 * 4) + (taste2 * 2) + (taste); //binary encoded buttons

        switch (code) {
            case 1:
                position = 0.0; //taste
                break;
            case 2:
                position = 1.0; //taste2  
                break;
            case 4:
                position = 0.5; //taste3
                break;
            default: 
                position = 0.5; //all other combinations
                break;
        }

        myservo = position;
    }
}
11 Feb 2012

thanks! thats good.

but in the code is a little failure :-)

in the code line taste3 :-)

I use your code later at home.

Greets and thanks