Author Topic: Arduino - linear speed / servo control with potentiometers  (Read 2526 times)

0 Members and 1 Guest are viewing this topic.

Offline Matthew98Topic starter

  • Regular Contributor
  • *
  • Posts: 74
  • Country: cz
Arduino - linear speed / servo control with potentiometers
« on: April 21, 2017, 03:01:33 pm »
Hello.
How can i change PWM lineally with potentiometer ? Im using PWM for servo and motor speed on my RC formula I made. :)  There are two potentiometers in remote control (x and y axis) which values i want to transfer via 2.4Ghz transmitter to the RC car and change lineally speed of motor and position of servo (with dead zones at middle and max values )
I have no idea how to do it, though... any help ? :)
 

Online CatalinaWOW

  • Super Contributor
  • ***
  • Posts: 5226
  • Country: us
Re: Arduino - linear speed / servo control with potentiometers
« Reply #1 on: April 21, 2017, 03:10:43 pm »
Servo control with pots is easy on Arduino.  I think examples are posted.  You just hook the pots across the power supply, feed the wiper to an analogue input (the 10 bit A-D), scale down to 8 bits and feed the PWM software.

Going through the air at 2.4 GHz makes it slightly more difficult.  And somewhat pointless.  You need a receiver with the right frequency hop etc to receive the transmitted signal.  You could then convert the PWM of the appropriate receiver channel to a DC with an RC filter and feed the analog input.  But since the receiver already drives servos there is little to be gained by going through the Arduino.  You can set dead zones and the like, but going through the Arduino will add latency.  Something most are trying to avoid.
 

Offline Matthew98Topic starter

  • Regular Contributor
  • *
  • Posts: 74
  • Country: cz
Re: Arduino - linear speed / servo control with potentiometers
« Reply #2 on: April 21, 2017, 06:14:09 pm »
Well i know how to do all the other stuff... :) im just asking on how to change the pwm according to the potentiometers ... because the numbers wont fit my needs and i probably cant figure out the algorythm to make the numbers I need from them. Im I suppose to make a map ? I really dont know how to do only this part...
 

Online CatalinaWOW

  • Super Contributor
  • ***
  • Posts: 5226
  • Country: us
Re: Arduino - linear speed / servo control with potentiometers
« Reply #3 on: April 21, 2017, 07:03:42 pm »
Are you using the SERVO package?  Which wants a number from 0 to 180 as an input.  The units are degrees, with 0 representing 90 degrees left rotation, 90 represents the center position and 180 representing 90 degrees right rotation.  So the problem is to scale the output of the A/D which has a value range of 0 to 1023 to this range.  If you are using floating point math it is simply a matter of multiplying by a scale factor. Floating point uses a lot of time/resources on the Arduino so using a slightly more complex, but acceptable result can be achieved by converting the A/D reading to a long variable type, then multiplying by 180 and dividing by 1023. 

If you are using the microseconds input of servo the math is slightly more complex, but the ideas are very similar.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Re: Arduino - linear speed / servo control with potentiometers
« Reply #4 on: April 24, 2017, 03:48:23 pm »
You may find that dividing by 1024 is close enough.

Take your reading (0..1023) and multiply by 180 and shift right 10 places rather than dividing by 1023.  That will give you an output range of 0..179.8 and you could always round up by adding 512 before dividing by 1024 because, ultimately, you need an integer <= 180.

Yes, you will have to do 32 bit arithmetic, probably 'unsigned long'.

« Last Edit: April 24, 2017, 04:02:21 pm by rstofer »
 


Online CatalinaWOW

  • Super Contributor
  • ***
  • Posts: 5226
  • Country: us
Re: Arduino - linear speed / servo control with potentiometers
« Reply #6 on: April 24, 2017, 05:17:56 pm »
I agree.  Fingers engaged before brain.

It really only matters for computation if doing the divide by shifting.  I defy you to find a servo application where one part in 1024 makes a real difference.  To engineering precision the numbers are identical.
« Last Edit: April 24, 2017, 05:20:11 pm by CatalinaWOW »
 

Offline retrolefty

  • Super Contributor
  • ***
  • Posts: 1648
  • Country: us
  • measurement changes behavior
Re: Arduino - linear speed / servo control with potentiometers
« Reply #7 on: April 24, 2017, 05:56:49 pm »
Well i know how to do all the other stuff... :) im just asking on how to change the pwm according to the potentiometers ... because the numbers wont fit my needs and i probably cant figure out the algorythm to make the numbers I need from them. Im I suppose to make a map ? I really dont know how to do only this part...

 The Arduino IDE does provide a map function.
fragment minus includes and setup.

Code: [Select]
{
  int val = analogRead(0);
  int angle = map(val, 0, 1023, 0, 180);  // 0-1023 is analog input pot range. 0-180 is servo position
                                                       // range.
  servo.write(angle);
}
 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: Arduino - linear speed / servo control with potentiometers
« Reply #8 on: April 24, 2017, 06:18:52 pm »
 Keep in mind not all the servos, especially the cheap ones, don' actually go all the way from 0 to 180 degrees. When commanded to 0 (or 180) they go most of the way and then buzz, drawing a ton of current and heating up the motor. So you may want o check and find out what the usable range of your servos actually is and use that in the conversion or in the MAP function.
 
 

Offline Matthew98Topic starter

  • Regular Contributor
  • *
  • Posts: 74
  • Country: cz
Re: Arduino - linear speed / servo control with potentiometers
« Reply #9 on: April 24, 2017, 07:17:02 pm »
Thank you all guys for responses and sorry for myself not responding. Im in the middle of very important exams and its hard to slip arduino in :) Will take a look at it this weekend. Thanks !
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf