ELCT 302 / Mbed 2 deprecated drivelab3

Dependencies:   mbed

main.cpp

Committer:
benjaminstone
Date:
2021-02-01
Revision:
1:128dc77d025a
Parent:
0:bd04e6ac5a2c
Child:
2:01372422b0c7

File content as of revision 1:128dc77d025a:

// open-loop control of the servo motor
// reads analog input and converts it to a pwm signal
 
#include "mbed.h"
#include <iostream>
 
AnalogIn input(PTB0);
PwmOut output(PTE21);
Ticker updater;

float v = 0.0;
float d = 0.0;
 
void pwmUpdate()
{
    v = input.read();
    d = v; // sets DC to voltage
    if( abs(d - output.read()) > 0.001) //only updates the duty cycle if there is a change in the duty cycle greater than 0.3%
         output.write(d);
}
 
int main(void)
{
    output.period(0.00005f); // 20 kHz control signal
    // attaches pwmUpdate function to the ticker with a period of 20ms
    updater.attach(&pwmUpdate,0.00005f);
    std::cout << "AnalogIn: " << v << " Duty Cycle: " << d << std::endl;
    while(1) {} // do nothing
}