Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MeArm_Servo_Calibration by
main.cpp
- Committer:
- eencae
- Date:
- 2017-07-04
- Revision:
- 0:68133f5cdfa0
- Child:
- 1:b9119bdd3ca9
File content as of revision 0:68133f5cdfa0:
/* MeArm Servo Calibration
(c) Dr Craig A. Evans, University of Leeds
July 2017
*/
#include "mbed.h"
// pins for potentiometers
AnalogIn m_pot(p17);
AnalogIn l_pot(p18);
AnalogIn r_pot(p19);
AnalogIn c_pot(p20);
// PWM pins for servos
PwmOut m_servo(p21);
PwmOut l_servo(p22);
PwmOut r_servo(p23);
PwmOut c_servo(p24);
int main()
{
    m_servo.period_ms(20);  // 20 ms = 50 Hz
    // all PWM channels share the same period and so only need to set one of them
    // keep looping forever
    while(1) {
        // read val in range 0.0 to 1.0
        float m_pot_val = m_pot.read();
        // create a pulse-width value in range 400 to 3000us
        int m_pot_pulse_us = int(400.0 + m_pot_val*2600.0);  
        m_servo.pulsewidth_us(m_pot_pulse_us);
        // repeat for other servos
        float l_pot_val = l_pot.read();
        int l_pot_pulse_us = int(600.0 + l_pot_val*2600.0);  
        l_servo.pulsewidth_us(l_pot_pulse_us);
        float r_pot_val = r_pot.read();
        int r_pot_pulse_us = int(600.0 + r_pot_val*2600.0); 
        r_servo.pulsewidth_us(r_pot_pulse_us);
        float c_pot_val = c_pot.read();
        int c_pot_pulse_us = int(600.0 + c_pot_val*2600.0); 
        c_servo.pulsewidth_us(c_pot_pulse_us);
        
        // print the values to the PC terminal
        printf("M=%i L=%i R=%i C=%i\n",m_pot_pulse_us,l_pot_pulse_us,r_pot_pulse_us,c_pot_pulse_us);
        // delay as we don't want to update the servos too often
        wait_ms(200);
    }
}
            
    