Dice roll

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 // Led control outputs
00004 DigitalOut ledb(LED_BLUE);
00005 DigitalOut ledr(LED_RED);
00006 DigitalOut ledg(LED_GREEN);
00007 
00008 // Motor control outputs
00009 DigitalOut motor1DC(D7);
00010 PwmOut motor1PWM(D6);
00011 
00012 // Input signals. Can either be pressing switch2 or EMG control.
00013 DigitalIn   button1(SW2);
00014 AnalogIn EMGDice(A0);
00015 
00016 void Roll()
00017 {  
00018     ledg = 1; // green led off
00019     ledr = 0; // red led on = rolling
00020     
00021     motor1PWM = 1; // motor on -> roll dice
00022     wait(1.05);
00023     motor1PWM = 0; // motor off
00024 
00025     motor1DC = abs(motor1DC-1); // rotate other way next time
00026     
00027     ledr = 1; // red led off
00028     ledb = 0; // blue led on -> inidcates wait time to protect motors
00029 }
00030 
00031 int main()
00032 {
00033     // Initialize system
00034     motor1DC = 1;
00035     ledb = 1;
00036     ledr = 1;
00037     ledg = 0;
00038     float t = 1.5; // Wait time (s) between rolls to protect motor
00039     
00040     // Dice roll code
00041     while (true) 
00042     {
00043         if(button1==0 or EMGDice>0.8) // Roll dice if switch2 is pressed or EMG input is delivered.
00044         { 
00045             Roll();
00046             wait(t); // wait before next roll to protect motors
00047             ledb = 1; 
00048             ledg = 0; // green led on = ready to roll
00049         }     
00050           
00051     }
00052 }