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:
6:d8c4c54e1077
Parent:
5:9b5ab304fe64
Child:
7:05bef8a64a44

File content as of revision 6:d8c4c54e1077:

#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 f = 0.2;

// 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 Send()
{
    // 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);
    
    // Reset encoder to reset pules count
    Encoder.reset();
}


// Roll function
void Roll()
{ 
    tick.attach(&Send, f);
    
    // 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
    wait(3.00); // Let motor turn for x time
    motor1PWM = 0; // motor off
    
    // Show ready state through leds
    wait(1.0); // wait before next roll to protect motors
    ledr = 1; // red led off
    ledb = 1; // blue led off
    ledg = 0; // green led on = ready to roll
    
}


int main()
{
    // Initialize system
    pc.baud(9600);
    motor1DC = -1;
    ledb = 1;
    ledr = 1;
    ledg = 0;
    
    // Dice code
    if(button1==false) 
    {
        Roll();
    }
    

    // Perform main continuously
    while(true) {}
}

// screen /dev/tty.usbmodem1412 9600