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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Executive Decision Maker for Mbed Application Board
00002 #include "mbed.h"
00003 #include "Speaker.h"
00004 #include "PinDetect.h" 
00005 #include "C12832_lcd.h"
00006 #include "Arial_9.h" //font for LCD
00007 #include <math.h> //needed for rand()
00008 
00009 DigitalOut myLed1(LED1); //Builtin LEDs
00010 DigitalOut myLed2(LED2);
00011 DigitalOut myLed3(LED3);
00012 DigitalOut myLed4(LED4);
00013 
00014 PinDetect pb1(p14); //Debounce pushbutton and use interrupts
00015 
00016 Speaker mySpeaker(p26); //PWM speaker to play notes
00017 
00018 Timer t; //use a hardware timer
00019 
00020 PwmOut r (p23); //RGB LED pins
00021 PwmOut g (p24);
00022 PwmOut b (p25);
00023 
00024 C12832_LCD lcd; //On board LCD display
00025 
00026 volatile int pbStatus = 0; //pb hit flag
00027 
00028 void pb1_hit_callback (void) //pb interrupt routine - a callback after debounce
00029 {
00030     pbStatus = 1;
00031 }
00032 void funcD( int i) //display a binary number on mbed's 4 LEDs
00033 {
00034     myLed1 = i &0x01;
00035     myLed2 = (i>>1) & 0x01;
00036     myLed3 = (i>>2) & 0x01;
00037     myLed4 = (i>>3) & 0x01;
00038 }
00039 int main()
00040 {
00041     unsigned int number=0;
00042     //setup debounced pushbutton using interrupts
00043     pb1.mode(PullDown);
00044     wait(.01);
00045     pb1.attach_asserted(&pb1_hit_callback);
00046     pb1.setSampleFrequency();
00047     //setup LCD prompt
00048     lcd.cls();
00049     lcd.set_font((unsigned char*) Arial_9);
00050     lcd.printf("   Ask a Question, \n\r   then push Joystick");
00051     //RGB LED init
00052     b = 1.0; //blue 1.0=off 
00053     //use a timer to generate seed for rand (different number to start each time)
00054     t.start(); //start timer
00055     while(pbStatus == 0) {}; //timer counts until pb hit
00056     srand(t.read_ms()); //read ms from timer
00057     while(1) {
00058         if(pbStatus == 1) { //pb hit?
00059             pbStatus = 0; //reset pb hit flag
00060             lcd.cls();
00061             lcd.locate(38,10);
00062             //loop though several random numbers on LEDs and make beeps on Speaker
00063             for(int i=0; i<24; ++i) {
00064                 number = (rand() % 16);
00065                 mySpeaker.PlayNote(200.0 * (number + 1), 0.1, 1.0);
00066                 funcD(number);
00067             }
00068             // Update RGB led:  Red, Yellow, or Green based on number
00069             // Update LCD: No, Maybe, or Yes
00070             b = 1.0;
00071             if (number < 5) {
00072                 r = 0.8;
00073                 g = 1.0;
00074                 lcd.printf("  NO");
00075             } else {
00076                 r = 0.8;
00077                 g = 0.8;
00078                 if (number <=10) lcd.printf(" MAYBE");
00079                 if (number > 10) {
00080                     r = 1.0;
00081                     lcd.printf(" YES");
00082                 }
00083             }
00084         }
00085         wait(.05);
00086     }
00087 }