Analog In Question

02 Apr 2012

Hello people,

i have "create" ( copy and paste) the following program.

1 <<code>> #include "mbed.h"

2

3 AnalogIn position(p20);

4 PwmOut servo(p21);

5

6 int main() {

7 servo.period(0.020); servo requires a 20ms period

8 while (1) {

9 servo.pulsewidth(0.001 + 0.001 * position); servo position determined by a pulsewidth between 1-2ms

10 }

11 }

<<code>>

you see here on the youtube movie this example.

http://www.youtube.com/watch?v=Kyq61sRF5Og&feature=g-upl&context=G207b7c2AUAAAAAAAAAA

if I move the Jostik to the right, goes of the Servo to the right.

if I leave the Joystik off goes of the Servo again in the middle.

If I move the Joystik to the left, goes of the Servo to the left.

and after him let go again in the middle.

how can I realise this??

I had thought,I say, with 3 V places servo on 0.0 (on the left)

With 2 V places the Servo on 0.5 (middles)

With 1 V places servo on 1.0 (on the right)

Is this thought wrong?

02 Apr 2012

AnalogIn inputs provide 0.0 when the voltage is 0V and provide 1.0 when the voltage is 3.3V. That means about .3 per volt input. In your case you have a range of 1V-3V. That results in AnalogIn values ranging from .3 to .9 The controlvalue for your servo computation should be between 0 and 1. So you have to correct the analog input value by adjusting for the lower limit (0.3) and scale it for the reduced range (0.9 - 0.3 = 0.6)

float temp;

while (1) {
  temp = (position - 0.3) * 1.66; // 1.66 = 1/0.6

  servo.pulsewidth(0.001 + 0.001 * temp); //servo position determined by a pulsewidth between 1-2ms

}

Note: you may want to do some additional checking to make sure the input values dont exceed the expected range due to noise or disturbances.

02 Apr 2012

Thats good! Tanks for your quickly help. I post a New Movie when i programmed The microcontroller New.