This version does not work on my pc however it should work if i look at all the data send to hidscope and de led which does it correct. We should tray another pc because my pc does give still some errors on the libaries

Dependencies:   HIDScope MODSERIAL mbed

Fork of Milestone_1_Motor_DualDirection_potmeter_v2 by Michel Vos

main.cpp

Committer:
CasperK
Date:
2018-09-28
Revision:
2:735ca8577f31
Parent:
1:aa505856416d
Child:
3:f00ddd66fe39

File content as of revision 2:735ca8577f31:

#include "mbed.h"
#include "MODSERIAL.h"
#include "HIDScope.h"

PwmOut pwmpin(D6);
PwmOut led(D10);
AnalogIn potmeter(A5);
DigitalIn button(D2);
DigitalOut directionpin(D5);

HIDScope scope(2);
Ticker ticker;
enum states{forward, stop, backwards};
states CurrentState;

volatile float x;
volatile float y;
volatile float p = potmeter;

void sendData() {
    scope.set(0,potmeter); //set the potmeter data to the first scope
    scope.set(1,x);
    scope.send(); //send the datapoints of the 2 motors
}

int main() {
      
//    float u = -0.3f; //determineusefulvalue, -0.3f is justanexample
//    directionpin = u > 0; //if bigger than 0, gives true, otherwise gives false   
    x = 1; //placeholder value for potmeter of second motor
    
    float scaled_potmeter = (p*2)-1; //scale potmeter from 0-1 to (-1 to 1)
    float c; //value to be used in negative scaled potmeter value
    bool forward = scaled_potmeter > 0;
    bool stop = scaled_potmeter == 0;
    bool backwards = scaled_potmeter < 0;
    
    pwmpin.period_us(60); //60 microseconds PWM period, 16.7 kHz
    led.period_us(60); //60 microseconds
    ticker.attach(&sendData, 0.005f); //send data to hidscope at 200Hz

    while (true) {        
        switch (CurrentState){       
            case forward:
                directionpin = true;
                pwmpin.write(scaled_potmeter); //pwm of motor is potmeter value
                led.write(scaled_potmeter); //led is potmeter value  
                break;
            case stop:
                // do nothing
                break;
            case backwards:
                directionpin = false;
                c = scaled_potmeter*-1;
                pwmpin.write(c); //pwm of motor is potmeter value
                led.write(c); //led is potmeter value 
                break; 
        }            
        wait(1.0f);
    }
}