tau new

Dependencies:   HIDScope QEI mbed

Fork of Tau by V D

main.cpp

Committer:
ARGroenenboom
Date:
2017-10-31
Revision:
0:cf88b023a080
Child:
1:d2fe9abf5082

File content as of revision 0:cf88b023a080:

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

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

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

//Button
DigitalIn   button1(SW2);

//PC
Serial pc(USBTX, USBRX);

// HIDscope
HIDScope scope(1);

// Encoder
int counts;
QEI Encoder(A0,A1,NC,32);
Ticker tick;
float F = 0.02;

//Minimum wait time between rolls
float t = 1;

// Encoder measurement function
void Measure()
{
    // Get pulses and send to HIDScope
    counts = Encoder.getPulses();
    
    float angle_pc = 1/87;
    float angle = counts * angle_pc;
    
    scope.set(0, angle);
    scope.send();
    
    pc.printf("Counts:%i. \n Angle:%i. \n",counts,angle);
}

//Dice rolling function
void Roll()
{  
    ledg = 1; // green led off
    ledr = 0; // red led on = rolling
    
    motor1PWM = 1; // motor on -> roll dice
    wait(1.5);
    motor1PWM = 0; // motor off

    motor1DC = abs(motor1DC-1); // rotate other way next time
    
    ledr = 1; // red led off
    ledb = 0; // blue led on
}

int main()
{
    motor1DC = 1;
    ledb = 1;
    ledr = 1;
    ledg = 0;
    
    
    
    while (true) 
    {
        if(button1==0)
        { 
            Roll();
            wait(t); // wait before next roll to protect motors
            ledb = 1;
            ledg = 0; // green led on = ready to roll
        } 
        Measure();     
    }
}