tau
Dependencies: HIDScope QEI mbed
Fork of Tau by
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 F = 0.02; 00024 float X = 32; 00025 float N = 131; 00026 float pulse_count; 00027 float angle_motor; 00028 QEI Encoder(A0,A1,NC,N); 00029 00030 //Minimum wait time between rolls 00031 float t = 1.5; 00032 00033 // Encoder measurement function 00034 void Measure() 00035 { 00036 // Get pulses and send to HIDScope 00037 pulse_count = Encoder.getPulses(); 00038 00039 int angle_pp =360/(X * N); 00040 00041 int angle = pulse_count*angle_pp; 00042 00043 scope.set(0, angle); 00044 scope.send(); 00045 } 00046 00047 //Dice rolling function 00048 void Roll() 00049 { 00050 ledg = 1; // green led off 00051 ledr = 0; // red led on = rolling 00052 00053 motor1PWM = 1; // motor on -> roll dice 00054 wait(3); 00055 motor1PWM = 0; // motor off 00056 00057 //motor1DC = abs(motor1DC-1); // rotate other way next time 00058 00059 //ledr = 1; // red led off 00060 //ledb = 0; // blue led on 00061 00062 //Encoder.reset(); 00063 } 00064 00065 int main() 00066 { 00067 motor1DC = 1; 00068 ledb = 1; 00069 ledr = 1; 00070 ledg = 0; 00071 00072 while (true) 00073 { 00074 if(button1==0) 00075 { 00076 Roll(); 00077 wait(t); // wait before next roll to protect motors 00078 ledb = 1; 00079 ledg = 0; // green led on = ready to roll 00080 } 00081 Measure(); 00082 } 00083 }
Generated on Sun Jul 31 2022 19:54:36 by
1.7.2
