Dice roll

Dependencies:   mbed

Committer:
ARGroenenboom
Date:
Tue Oct 31 09:29:13 2017 +0000
Revision:
0:68cc55f8a240
Child:
1:8be4a41833fd
Dice roll

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ARGroenenboom 0:68cc55f8a240 1 #include "mbed.h"
ARGroenenboom 0:68cc55f8a240 2
ARGroenenboom 0:68cc55f8a240 3 DigitalOut ledb(LED_BLUE);
ARGroenenboom 0:68cc55f8a240 4 DigitalOut ledr(LED_RED);
ARGroenenboom 0:68cc55f8a240 5 DigitalOut ledg(LED_GREEN);
ARGroenenboom 0:68cc55f8a240 6
ARGroenenboom 0:68cc55f8a240 7 DigitalOut motor1DC(D7);
ARGroenenboom 0:68cc55f8a240 8 PwmOut motor1PWM(D6);
ARGroenenboom 0:68cc55f8a240 9
ARGroenenboom 0:68cc55f8a240 10 DigitalIn button1(SW2);
ARGroenenboom 0:68cc55f8a240 11
ARGroenenboom 0:68cc55f8a240 12 volatile int n = 0;
ARGroenenboom 0:68cc55f8a240 13 float t = 1;
ARGroenenboom 0:68cc55f8a240 14
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 0:68cc55f8a240 22 wait(1.5);
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 0:68cc55f8a240 28 ledb = 0; // blue led on
ARGroenenboom 0:68cc55f8a240 29 }
ARGroenenboom 0:68cc55f8a240 30
ARGroenenboom 0:68cc55f8a240 31 int main()
ARGroenenboom 0:68cc55f8a240 32 {
ARGroenenboom 0:68cc55f8a240 33 motor1DC = 1;
ARGroenenboom 0:68cc55f8a240 34 ledb = 1;
ARGroenenboom 0:68cc55f8a240 35 ledr = 1;
ARGroenenboom 0:68cc55f8a240 36 ledg = 0;
ARGroenenboom 0:68cc55f8a240 37
ARGroenenboom 0:68cc55f8a240 38 while (true) {
ARGroenenboom 0:68cc55f8a240 39 if(button1==0)
ARGroenenboom 0:68cc55f8a240 40 {
ARGroenenboom 0:68cc55f8a240 41 Roll();
ARGroenenboom 0:68cc55f8a240 42 wait(t); // wait before next roll to protect motors
ARGroenenboom 0:68cc55f8a240 43 ledb = 1;
ARGroenenboom 0:68cc55f8a240 44 ledg = 0; // green led on = ready to roll
ARGroenenboom 0:68cc55f8a240 45 }
ARGroenenboom 0:68cc55f8a240 46 }
ARGroenenboom 0:68cc55f8a240 47 }