Ticker added

Dependencies:   HIDScope MODSERIAL QEI mbed

Fork of Tau_new_ticker_toevoegen by V D

main.cpp

Committer:
ARGroenenboom
Date:
2017-11-02
Revision:
9:23fa7b03e2fe
Parent:
8:62c7083f12d9
Child:
10:cab7ca2f144a

File content as of revision 9:23fa7b03e2fe:

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

//Leds
DigitalOut ledb(LED_BLUE);
DigitalOut ledr(LED_RED);
DigitalOut ledg(LED_GREEN);

//Motor
DigitalOut motor1DC(D7);
PwmOut motor1PWM(D6);

//Button
DigitalIn   button1(SW2);

// HIDscope
HIDScope scope(1);

//Ticker
Ticker tick;
float R = 3;
float F = 0.2;
int counter=0;

// Encoder
float X = 32.0;
float N = 131.0;
float pulse_count;
float angle_motor;
QEI Encoder(D8,D9,NC,N);
MODSERIAL pc(USBTX,USBRX);

// Send angle to HIDScope and pc function
void Calculate()
{
    // Get pulses and calculate angle
    pulse_count = Encoder.getPulses();
    float angle_pp =360/(X * N);
    float angle = pulse_count*angle_pp;

    // Send angle to HIDScope and pc
    scope.set(0, angle);
    scope.send();
    pc.printf(" pulse count: %f angle: %f \r \n ", pulse_count, angle);
    
    counter++;
    
    // Turn motor off and reset encoder to reset pules count
    
    if(counter==R/F)
    {
        motor1PWM = 0; // motor off
    }  
}

void Measure()
{
    tick.attach(&Calculate, F);
}

// Roll function
void Roll()
{ 
    // Show rolling state through leds
    ledg = 1; // green led off
    ledb = 1; // blue led off
    ledr = 0; // red led on = rolling

    // Turn motor on for x time and turn it off again
    motor1PWM = 1; // motor on
    
    // Reset counter and start measuring
    Encoder.reset();
    counter = 0;
    Measure(); 
}


int main()
{
    // Initialize system
    pc.baud(9600);
    motor1DC = -1;
    ledb = 1;
    ledr = 1;
    ledg = 0;
    
    // Dice roll code
    while(true){
        Encoder.reset();
        
        // Roll dice if button is pressed
        if(button1==0) 
        {
            Encoder.reset();
            Roll();
        }
    }
}