Dice roll

Dependencies:   mbed

Committer:
ARGroenenboom
Date:
Fri Nov 03 10:34:30 2017 +0000
Revision:
2:0432dad1e3ac
Parent:
1:8be4a41833fd
EMG option added. Threshold raised.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ARGroenenboom 0:68cc55f8a240 1 #include "mbed.h"
ARGroenenboom 0:68cc55f8a240 2
ARGroenenboom 2:0432dad1e3ac 3 // Led control outputs
ARGroenenboom 0:68cc55f8a240 4 DigitalOut ledb(LED_BLUE);
ARGroenenboom 0:68cc55f8a240 5 DigitalOut ledr(LED_RED);
ARGroenenboom 0:68cc55f8a240 6 DigitalOut ledg(LED_GREEN);
ARGroenenboom 0:68cc55f8a240 7
ARGroenenboom 2:0432dad1e3ac 8 // Motor control outputs
ARGroenenboom 0:68cc55f8a240 9 DigitalOut motor1DC(D7);
ARGroenenboom 0:68cc55f8a240 10 PwmOut motor1PWM(D6);
ARGroenenboom 0:68cc55f8a240 11
ARGroenenboom 2:0432dad1e3ac 12 // Input signals. Can either be pressing switch2 or EMG control.
ARGroenenboom 0:68cc55f8a240 13 DigitalIn button1(SW2);
ARGroenenboom 2:0432dad1e3ac 14 AnalogIn EMGDice(A0);
ARGroenenboom 0:68cc55f8a240 15
ARGroenenboom 0:68cc55f8a240 16 void Roll()
ARGroenenboom 0:68cc55f8a240 17 {
ARGroenenboom 0:68cc55f8a240 18 ledg = 1; // green led off
ARGroenenboom 0:68cc55f8a240 19 ledr = 0; // red led on = rolling
ARGroenenboom 0:68cc55f8a240 20
ARGroenenboom 0:68cc55f8a240 21 motor1PWM = 1; // motor on -> roll dice
ARGroenenboom 2:0432dad1e3ac 22 wait(1.05);
ARGroenenboom 0:68cc55f8a240 23 motor1PWM = 0; // motor off
ARGroenenboom 0:68cc55f8a240 24
ARGroenenboom 0:68cc55f8a240 25 motor1DC = abs(motor1DC-1); // rotate other way next time
ARGroenenboom 0:68cc55f8a240 26
ARGroenenboom 0:68cc55f8a240 27 ledr = 1; // red led off
ARGroenenboom 2:0432dad1e3ac 28 ledb = 0; // blue led on -> inidcates wait time to protect motors
ARGroenenboom 0:68cc55f8a240 29 }
ARGroenenboom 0:68cc55f8a240 30
ARGroenenboom 0:68cc55f8a240 31 int main()
ARGroenenboom 0:68cc55f8a240 32 {
ARGroenenboom 2:0432dad1e3ac 33 // Initialize system
ARGroenenboom 0:68cc55f8a240 34 motor1DC = 1;
ARGroenenboom 0:68cc55f8a240 35 ledb = 1;
ARGroenenboom 0:68cc55f8a240 36 ledr = 1;
ARGroenenboom 0:68cc55f8a240 37 ledg = 0;
ARGroenenboom 2:0432dad1e3ac 38 float t = 1.5; // Wait time (s) between rolls to protect motor
ARGroenenboom 0:68cc55f8a240 39
ARGroenenboom 2:0432dad1e3ac 40 // Dice roll code
ARGroenenboom 1:8be4a41833fd 41 while (true)
ARGroenenboom 1:8be4a41833fd 42 {
ARGroenenboom 2:0432dad1e3ac 43 if(button1==0 or EMGDice>0.8) // Roll dice if switch2 is pressed or EMG input is delivered.
ARGroenenboom 0:68cc55f8a240 44 {
ARGroenenboom 1:8be4a41833fd 45 Roll();
ARGroenenboom 1:8be4a41833fd 46 wait(t); // wait before next roll to protect motors
ARGroenenboom 2:0432dad1e3ac 47 ledb = 1;
ARGroenenboom 1:8be4a41833fd 48 ledg = 0; // green led on = ready to roll
ARGroenenboom 2:0432dad1e3ac 49 }
ARGroenenboom 2:0432dad1e3ac 50
ARGroenenboom 0:68cc55f8a240 51 }
ARGroenenboom 0:68cc55f8a240 52 }