Executive decision maker for the mbed NXP1768 Application board. Ask a question, and press the center joystick button. Uses a pushbutton, PWM speaker, mbed LEDs, RGB LED, LCD, and the C rand function.
Dependencies: C12832_lcd LCD_fonts PinDetect mbed
main.cpp@0:af96f6744422, 2015-02-03 (annotated)
- Committer:
- 4180_1
- Date:
- Tue Feb 03 19:42:36 2015 +0000
- Revision:
- 0:af96f6744422
ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:af96f6744422 | 1 | //Executive Decision Maker for Mbed Application Board |
4180_1 | 0:af96f6744422 | 2 | #include "mbed.h" |
4180_1 | 0:af96f6744422 | 3 | #include "Speaker.h" |
4180_1 | 0:af96f6744422 | 4 | #include "PinDetect.h" |
4180_1 | 0:af96f6744422 | 5 | #include "C12832_lcd.h" |
4180_1 | 0:af96f6744422 | 6 | #include "Arial_9.h" //font for LCD |
4180_1 | 0:af96f6744422 | 7 | #include <math.h> //needed for rand() |
4180_1 | 0:af96f6744422 | 8 | |
4180_1 | 0:af96f6744422 | 9 | DigitalOut myLed1(LED1); //Builtin LEDs |
4180_1 | 0:af96f6744422 | 10 | DigitalOut myLed2(LED2); |
4180_1 | 0:af96f6744422 | 11 | DigitalOut myLed3(LED3); |
4180_1 | 0:af96f6744422 | 12 | DigitalOut myLed4(LED4); |
4180_1 | 0:af96f6744422 | 13 | |
4180_1 | 0:af96f6744422 | 14 | PinDetect pb1(p14); //Debounce pushbutton and use interrupts |
4180_1 | 0:af96f6744422 | 15 | |
4180_1 | 0:af96f6744422 | 16 | Speaker mySpeaker(p26); //PWM speaker to play notes |
4180_1 | 0:af96f6744422 | 17 | |
4180_1 | 0:af96f6744422 | 18 | Timer t; //use a hardware timer |
4180_1 | 0:af96f6744422 | 19 | |
4180_1 | 0:af96f6744422 | 20 | PwmOut r (p23); //RGB LED pins |
4180_1 | 0:af96f6744422 | 21 | PwmOut g (p24); |
4180_1 | 0:af96f6744422 | 22 | PwmOut b (p25); |
4180_1 | 0:af96f6744422 | 23 | |
4180_1 | 0:af96f6744422 | 24 | C12832_LCD lcd; //On board LCD display |
4180_1 | 0:af96f6744422 | 25 | |
4180_1 | 0:af96f6744422 | 26 | volatile int pbStatus = 0; //pb hit flag |
4180_1 | 0:af96f6744422 | 27 | |
4180_1 | 0:af96f6744422 | 28 | void pb1_hit_callback (void) //pb interrupt routine - a callback after debounce |
4180_1 | 0:af96f6744422 | 29 | { |
4180_1 | 0:af96f6744422 | 30 | pbStatus = 1; |
4180_1 | 0:af96f6744422 | 31 | } |
4180_1 | 0:af96f6744422 | 32 | void funcD( int i) //display a binary number on mbed's 4 LEDs |
4180_1 | 0:af96f6744422 | 33 | { |
4180_1 | 0:af96f6744422 | 34 | myLed1 = i &0x01; |
4180_1 | 0:af96f6744422 | 35 | myLed2 = (i>>1) & 0x01; |
4180_1 | 0:af96f6744422 | 36 | myLed3 = (i>>2) & 0x01; |
4180_1 | 0:af96f6744422 | 37 | myLed4 = (i>>3) & 0x01; |
4180_1 | 0:af96f6744422 | 38 | } |
4180_1 | 0:af96f6744422 | 39 | int main() |
4180_1 | 0:af96f6744422 | 40 | { |
4180_1 | 0:af96f6744422 | 41 | unsigned int number=0; |
4180_1 | 0:af96f6744422 | 42 | //setup debounced pushbutton using interrupts |
4180_1 | 0:af96f6744422 | 43 | pb1.mode(PullDown); |
4180_1 | 0:af96f6744422 | 44 | wait(.01); |
4180_1 | 0:af96f6744422 | 45 | pb1.attach_asserted(&pb1_hit_callback); |
4180_1 | 0:af96f6744422 | 46 | pb1.setSampleFrequency(); |
4180_1 | 0:af96f6744422 | 47 | //setup LCD prompt |
4180_1 | 0:af96f6744422 | 48 | lcd.cls(); |
4180_1 | 0:af96f6744422 | 49 | lcd.set_font((unsigned char*) Arial_9); |
4180_1 | 0:af96f6744422 | 50 | lcd.printf(" Ask a Question, \n\r then push Joystick"); |
4180_1 | 0:af96f6744422 | 51 | //RGB LED init |
4180_1 | 0:af96f6744422 | 52 | b = 1.0; //blue 1.0=off |
4180_1 | 0:af96f6744422 | 53 | //use a timer to generate seed for rand (different number to start each time) |
4180_1 | 0:af96f6744422 | 54 | t.start(); //start timer |
4180_1 | 0:af96f6744422 | 55 | while(pbStatus == 0) {}; //timer counts until pb hit |
4180_1 | 0:af96f6744422 | 56 | srand(t.read_ms()); //read ms from timer |
4180_1 | 0:af96f6744422 | 57 | while(1) { |
4180_1 | 0:af96f6744422 | 58 | if(pbStatus == 1) { //pb hit? |
4180_1 | 0:af96f6744422 | 59 | pbStatus = 0; //reset pb hit flag |
4180_1 | 0:af96f6744422 | 60 | lcd.cls(); |
4180_1 | 0:af96f6744422 | 61 | lcd.locate(38,10); |
4180_1 | 0:af96f6744422 | 62 | //loop though several random numbers on LEDs and make beeps on Speaker |
4180_1 | 0:af96f6744422 | 63 | for(int i=0; i<24; ++i) { |
4180_1 | 0:af96f6744422 | 64 | number = (rand() % 16); |
4180_1 | 0:af96f6744422 | 65 | mySpeaker.PlayNote(200.0 * (number + 1), 0.1, 1.0); |
4180_1 | 0:af96f6744422 | 66 | funcD(number); |
4180_1 | 0:af96f6744422 | 67 | } |
4180_1 | 0:af96f6744422 | 68 | // Update RGB led: Red, Yellow, or Green based on number |
4180_1 | 0:af96f6744422 | 69 | // Update LCD: No, Maybe, or Yes |
4180_1 | 0:af96f6744422 | 70 | b = 1.0; |
4180_1 | 0:af96f6744422 | 71 | if (number < 5) { |
4180_1 | 0:af96f6744422 | 72 | r = 0.8; |
4180_1 | 0:af96f6744422 | 73 | g = 1.0; |
4180_1 | 0:af96f6744422 | 74 | lcd.printf(" NO"); |
4180_1 | 0:af96f6744422 | 75 | } else { |
4180_1 | 0:af96f6744422 | 76 | r = 0.8; |
4180_1 | 0:af96f6744422 | 77 | g = 0.8; |
4180_1 | 0:af96f6744422 | 78 | if (number <=10) lcd.printf(" MAYBE"); |
4180_1 | 0:af96f6744422 | 79 | if (number > 10) { |
4180_1 | 0:af96f6744422 | 80 | r = 1.0; |
4180_1 | 0:af96f6744422 | 81 | lcd.printf(" YES"); |
4180_1 | 0:af96f6744422 | 82 | } |
4180_1 | 0:af96f6744422 | 83 | } |
4180_1 | 0:af96f6744422 | 84 | } |
4180_1 | 0:af96f6744422 | 85 | wait(.05); |
4180_1 | 0:af96f6744422 | 86 | } |
4180_1 | 0:af96f6744422 | 87 | } |