tau new

Dependencies:   HIDScope QEI mbed

Fork of Tau by V D

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HIDScope.h"
00003 #include "QEI.h"
00004 #include "iostream"
00005 
00006 //Leds
00007 DigitalOut ledb(LED_BLUE);
00008 DigitalOut ledr(LED_RED);
00009 DigitalOut ledg(LED_GREEN);
00010 
00011 //Motor
00012 DigitalOut motor1DC(D7);
00013 PwmOut motor1PWM(D6);
00014 
00015 //Button
00016 DigitalIn   button1(SW2);
00017 
00018 // HIDscope
00019 HIDScope scope(1);
00020 
00021 // Encoder
00022 //Ticker tick;
00023 float X = 32;
00024 float N = 131;
00025 float pulse_count;
00026 float angle_motor;
00027 QEI Encoder(A0,A1,NC,N);
00028 
00029 //Minimum wait time between rolls
00030 float t = 1;
00031 
00032 // Encoder measurement function
00033 void Measure()
00034 {
00035     // Get pulses and send to HIDScope
00036     pulse_count = Encoder.getPulses();
00037     
00038     int angle_pp =360/(X * N);
00039     
00040    int angle = pulse_count*angle_pp;
00041     
00042     scope.set(0, angle);
00043     scope.send();
00044 }
00045 
00046 //Dice rolling function
00047 void Roll()
00048 {  
00049     //motor1DC = 0;
00050     
00051     ledg = 1; // green led off
00052     ledr = 0; // red led on = rolling
00053     
00054     motor1PWM = 1; // motor on -> roll dice
00055     wait(3.0);
00056     motor1PWM = 0; // motor off
00057 
00058 
00059     //motor1DC = abs(motor1DC-1); // rotate other way next time
00060     
00061     //ledr = 1; // red led off
00062     //ledb = 0; // blue led on
00063     
00064     //Encoder.reset();
00065 }
00066 
00067 int main()
00068 {
00069     motor1DC = 0;
00070     ledb = 1;
00071     ledr = 1;
00072     ledg = 0;
00073     
00074     while (true) 
00075     {
00076         if(button1==0)
00077         { 
00078             Roll();
00079             wait(t); // wait before next roll to protect motors
00080             ledb = 1;
00081             ledg = 0; // green led on = ready to roll
00082         } 
00083         Measure();     
00084     }
00085 }