Dice roll

Dependencies:   mbed

Committer:
ARGroenenboom
Date:
Wed Nov 01 09:17:20 2017 +0000
Revision:
1:8be4a41833fd
Parent:
0:68cc55f8a240
Child:
2:0432dad1e3ac
Latest version

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 void Roll()
ARGroenenboom 0:68cc55f8a240 16 {
ARGroenenboom 0:68cc55f8a240 17 ledg = 1; // green led off
ARGroenenboom 0:68cc55f8a240 18 ledr = 0; // red led on = rolling
ARGroenenboom 0:68cc55f8a240 19
ARGroenenboom 0:68cc55f8a240 20 motor1PWM = 1; // motor on -> roll dice
ARGroenenboom 0:68cc55f8a240 21 wait(1.5);
ARGroenenboom 0:68cc55f8a240 22 motor1PWM = 0; // motor off
ARGroenenboom 0:68cc55f8a240 23
ARGroenenboom 0:68cc55f8a240 24 motor1DC = abs(motor1DC-1); // rotate other way next time
ARGroenenboom 0:68cc55f8a240 25
ARGroenenboom 0:68cc55f8a240 26 ledr = 1; // red led off
ARGroenenboom 0:68cc55f8a240 27 ledb = 0; // blue led on
ARGroenenboom 0:68cc55f8a240 28 }
ARGroenenboom 0:68cc55f8a240 29
ARGroenenboom 0:68cc55f8a240 30 int main()
ARGroenenboom 0:68cc55f8a240 31 {
ARGroenenboom 0:68cc55f8a240 32 motor1DC = 1;
ARGroenenboom 0:68cc55f8a240 33 ledb = 1;
ARGroenenboom 0:68cc55f8a240 34 ledr = 1;
ARGroenenboom 0:68cc55f8a240 35 ledg = 0;
ARGroenenboom 0:68cc55f8a240 36
ARGroenenboom 1:8be4a41833fd 37 while (true)
ARGroenenboom 1:8be4a41833fd 38 {
ARGroenenboom 0:68cc55f8a240 39 if(button1==0)
ARGroenenboom 0:68cc55f8a240 40 {
ARGroenenboom 1:8be4a41833fd 41 Roll();
ARGroenenboom 1:8be4a41833fd 42 wait(t); // wait before next roll to protect motors
ARGroenenboom 1:8be4a41833fd 43 ledb = 1;
ARGroenenboom 1:8be4a41833fd 44 ledg = 0; // green led on = ready to roll
ARGroenenboom 1:8be4a41833fd 45 }
ARGroenenboom 0:68cc55f8a240 46 }
ARGroenenboom 0:68cc55f8a240 47 }