uLCD robot moveable via pushbuttons
Dependencies: 4DGL-uLCD-SE PinDetect mbed
main.cpp@0:327ccd5eafc8, 2016-11-06 (annotated)
- Committer:
- jboettcher
- Date:
- Sun Nov 06 21:57:20 2016 +0000
- Revision:
- 0:327ccd5eafc8
Complete
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jboettcher | 0:327ccd5eafc8 | 1 | |
jboettcher | 0:327ccd5eafc8 | 2 | #include "mbed.h" |
jboettcher | 0:327ccd5eafc8 | 3 | #include "Speaker.h" |
jboettcher | 0:327ccd5eafc8 | 4 | #include "PinDetect.h" |
jboettcher | 0:327ccd5eafc8 | 5 | #include "uLCD_4DGL.h" |
jboettcher | 0:327ccd5eafc8 | 6 | // setup builtin LEDs |
jboettcher | 0:327ccd5eafc8 | 7 | DigitalOut myLed1(LED1); |
jboettcher | 0:327ccd5eafc8 | 8 | DigitalOut myLed2(LED2); |
jboettcher | 0:327ccd5eafc8 | 9 | DigitalOut myLed3(LED3); |
jboettcher | 0:327ccd5eafc8 | 10 | DigitalOut myLed4(LED4); |
jboettcher | 0:327ccd5eafc8 | 11 | // setup pushbuttons for debounced interrupt use |
jboettcher | 0:327ccd5eafc8 | 12 | PinDetect pb1(p23); |
jboettcher | 0:327ccd5eafc8 | 13 | PinDetect pb2(p24); |
jboettcher | 0:327ccd5eafc8 | 14 | PinDetect pb3(p25); |
jboettcher | 0:327ccd5eafc8 | 15 | PinDetect pb4(p26); |
jboettcher | 0:327ccd5eafc8 | 16 | |
jboettcher | 0:327ccd5eafc8 | 17 | // setup LCD |
jboettcher | 0:327ccd5eafc8 | 18 | uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object |
jboettcher | 0:327ccd5eafc8 | 19 | |
jboettcher | 0:327ccd5eafc8 | 20 | //moving include down here allows use in uLCD in Robot class draw and erase member functions |
jboettcher | 0:327ccd5eafc8 | 21 | //easier for now than passing a new uLCD_4DGL object argument to the class |
jboettcher | 0:327ccd5eafc8 | 22 | #include "Robot.h" |
jboettcher | 0:327ccd5eafc8 | 23 | |
jboettcher | 0:327ccd5eafc8 | 24 | // setup instance of new Speaker class, mySpeaker using pin 21 |
jboettcher | 0:327ccd5eafc8 | 25 | // the pin must be a PWM output pin |
jboettcher | 0:327ccd5eafc8 | 26 | Speaker mySpeaker(p21); |
jboettcher | 0:327ccd5eafc8 | 27 | |
jboettcher | 0:327ccd5eafc8 | 28 | // pushbutton status - need volatile for safe interrupt R/W |
jboettcher | 0:327ccd5eafc8 | 29 | volatile int pbStatus = 0; |
jboettcher | 0:327ccd5eafc8 | 30 | |
jboettcher | 0:327ccd5eafc8 | 31 | // Callback routine is interrupt activated by a debounced pb1 hit |
jboettcher | 0:327ccd5eafc8 | 32 | void pb1_hit_callback (void) |
jboettcher | 0:327ccd5eafc8 | 33 | { |
jboettcher | 0:327ccd5eafc8 | 34 | // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED |
jboettcher | 0:327ccd5eafc8 | 35 | pbStatus = pbStatus | 0x01; |
jboettcher | 0:327ccd5eafc8 | 36 | |
jboettcher | 0:327ccd5eafc8 | 37 | } |
jboettcher | 0:327ccd5eafc8 | 38 | // Callback routine is interrupt activated by a debounced pb2 hit |
jboettcher | 0:327ccd5eafc8 | 39 | void pb2_hit_callback (void) |
jboettcher | 0:327ccd5eafc8 | 40 | { |
jboettcher | 0:327ccd5eafc8 | 41 | // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED |
jboettcher | 0:327ccd5eafc8 | 42 | pbStatus = pbStatus | 0x02; |
jboettcher | 0:327ccd5eafc8 | 43 | } |
jboettcher | 0:327ccd5eafc8 | 44 | // Callback routine is interrupt activated by a debounced pb3 hit |
jboettcher | 0:327ccd5eafc8 | 45 | void pb3_hit_callback (void) |
jboettcher | 0:327ccd5eafc8 | 46 | { |
jboettcher | 0:327ccd5eafc8 | 47 | // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED |
jboettcher | 0:327ccd5eafc8 | 48 | pbStatus = pbStatus | 0x04; |
jboettcher | 0:327ccd5eafc8 | 49 | } |
jboettcher | 0:327ccd5eafc8 | 50 | // Callback routine is interrupt activated by a debounced pb4 hit |
jboettcher | 0:327ccd5eafc8 | 51 | void pb4_hit_callback (void) |
jboettcher | 0:327ccd5eafc8 | 52 | { |
jboettcher | 0:327ccd5eafc8 | 53 | // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED |
jboettcher | 0:327ccd5eafc8 | 54 | pbStatus = pbStatus | 0x08; |
jboettcher | 0:327ccd5eafc8 | 55 | } |
jboettcher | 0:327ccd5eafc8 | 56 | |
jboettcher | 0:327ccd5eafc8 | 57 | // main program |
jboettcher | 0:327ccd5eafc8 | 58 | int main() |
jboettcher | 0:327ccd5eafc8 | 59 | { |
jboettcher | 0:327ccd5eafc8 | 60 | uLCD.printf("\nmyTitties!\nJoseph Boettcher"); |
jboettcher | 0:327ccd5eafc8 | 61 | //setup four SPST push buttons |
jboettcher | 0:327ccd5eafc8 | 62 | pb1.mode(PullUp); //add internal pullup resistor |
jboettcher | 0:327ccd5eafc8 | 63 | pb2.mode(PullUp); |
jboettcher | 0:327ccd5eafc8 | 64 | pb3.mode(PullUp); |
jboettcher | 0:327ccd5eafc8 | 65 | pb4.mode(PullUp); |
jboettcher | 0:327ccd5eafc8 | 66 | // need a small delay for initial pullup to take effect due to capacitance |
jboettcher | 0:327ccd5eafc8 | 67 | wait(.01); |
jboettcher | 0:327ccd5eafc8 | 68 | // Setup Interrupt callback functions for each pb hit |
jboettcher | 0:327ccd5eafc8 | 69 | pb1.attach_deasserted(&pb1_hit_callback); |
jboettcher | 0:327ccd5eafc8 | 70 | pb2.attach_deasserted(&pb2_hit_callback); |
jboettcher | 0:327ccd5eafc8 | 71 | pb3.attach_deasserted(&pb3_hit_callback); |
jboettcher | 0:327ccd5eafc8 | 72 | pb4.attach_deasserted(&pb4_hit_callback); |
jboettcher | 0:327ccd5eafc8 | 73 | // Start sampling the pb inputs using interrupts |
jboettcher | 0:327ccd5eafc8 | 74 | pb1.setSampleFrequency(); |
jboettcher | 0:327ccd5eafc8 | 75 | pb2.setSampleFrequency(); |
jboettcher | 0:327ccd5eafc8 | 76 | pb3.setSampleFrequency(); |
jboettcher | 0:327ccd5eafc8 | 77 | pb4.setSampleFrequency(); |
jboettcher | 0:327ccd5eafc8 | 78 | // pushbuttons are all now setup with pullups and running with a software decounce filter |
jboettcher | 0:327ccd5eafc8 | 79 | |
jboettcher | 0:327ccd5eafc8 | 80 | // use your modified robot class to move and display robot's new location on LCD |
jboettcher | 0:327ccd5eafc8 | 81 | Robot myRobot; |
jboettcher | 0:327ccd5eafc8 | 82 | |
jboettcher | 0:327ccd5eafc8 | 83 | myRobot.draw(); //initial robot display in center of LCD |
jboettcher | 0:327ccd5eafc8 | 84 | //main loop to control and display robot |
jboettcher | 0:327ccd5eafc8 | 85 | while(1) { |
jboettcher | 0:327ccd5eafc8 | 86 | //check pushbutton status for new input |
jboettcher | 0:327ccd5eafc8 | 87 | switch (pbStatus) { |
jboettcher | 0:327ccd5eafc8 | 88 | //move forward |
jboettcher | 0:327ccd5eafc8 | 89 | case 0x01 : |
jboettcher | 0:327ccd5eafc8 | 90 | myLed1 = 1; |
jboettcher | 0:327ccd5eafc8 | 91 | pbStatus = 0; |
jboettcher | 0:327ccd5eafc8 | 92 | mySpeaker.PlayNote(200.0,0.1,0.8); |
jboettcher | 0:327ccd5eafc8 | 93 | myRobot.erase(); |
jboettcher | 0:327ccd5eafc8 | 94 | myRobot.moveForward(1); |
jboettcher | 0:327ccd5eafc8 | 95 | myRobot.draw(); |
jboettcher | 0:327ccd5eafc8 | 96 | break; |
jboettcher | 0:327ccd5eafc8 | 97 | //move backwards |
jboettcher | 0:327ccd5eafc8 | 98 | case 0x02 : |
jboettcher | 0:327ccd5eafc8 | 99 | myLed2 = 1; |
jboettcher | 0:327ccd5eafc8 | 100 | pbStatus = 0; |
jboettcher | 0:327ccd5eafc8 | 101 | mySpeaker.PlayNote(400.0,0.1,0.8); |
jboettcher | 0:327ccd5eafc8 | 102 | myRobot.erase(); |
jboettcher | 0:327ccd5eafc8 | 103 | myRobot.moveBackward(1); |
jboettcher | 0:327ccd5eafc8 | 104 | myRobot.draw(); |
jboettcher | 0:327ccd5eafc8 | 105 | break; |
jboettcher | 0:327ccd5eafc8 | 106 | //move left |
jboettcher | 0:327ccd5eafc8 | 107 | case 0x04 : |
jboettcher | 0:327ccd5eafc8 | 108 | myLed3 = 1; |
jboettcher | 0:327ccd5eafc8 | 109 | pbStatus = 0; |
jboettcher | 0:327ccd5eafc8 | 110 | mySpeaker.PlayNote(600.0,0.1,0.8); |
jboettcher | 0:327ccd5eafc8 | 111 | myRobot.erase(); |
jboettcher | 0:327ccd5eafc8 | 112 | myRobot.moveLeft(1); |
jboettcher | 0:327ccd5eafc8 | 113 | myRobot.draw(); |
jboettcher | 0:327ccd5eafc8 | 114 | break; |
jboettcher | 0:327ccd5eafc8 | 115 | //move right |
jboettcher | 0:327ccd5eafc8 | 116 | case 0x08 : |
jboettcher | 0:327ccd5eafc8 | 117 | myLed4 = 1; |
jboettcher | 0:327ccd5eafc8 | 118 | pbStatus = 0; |
jboettcher | 0:327ccd5eafc8 | 119 | mySpeaker.PlayNote(800.0,0.1,0.8); |
jboettcher | 0:327ccd5eafc8 | 120 | myRobot.erase(); |
jboettcher | 0:327ccd5eafc8 | 121 | myRobot.moveRight(1); |
jboettcher | 0:327ccd5eafc8 | 122 | myRobot.draw(); |
jboettcher | 0:327ccd5eafc8 | 123 | break; |
jboettcher | 0:327ccd5eafc8 | 124 | //no pb or multiple pbs hit |
jboettcher | 0:327ccd5eafc8 | 125 | default : |
jboettcher | 0:327ccd5eafc8 | 126 | myLed1 = 0; |
jboettcher | 0:327ccd5eafc8 | 127 | myLed2 = 0; |
jboettcher | 0:327ccd5eafc8 | 128 | myLed3 = 0; |
jboettcher | 0:327ccd5eafc8 | 129 | myLed4 = 0; |
jboettcher | 0:327ccd5eafc8 | 130 | pbStatus = 0; |
jboettcher | 0:327ccd5eafc8 | 131 | } //end switch |
jboettcher | 0:327ccd5eafc8 | 132 | //myLed1 = !myLed1; //blink to show loop is still running |
jboettcher | 0:327ccd5eafc8 | 133 | wait(.05); |
jboettcher | 0:327ccd5eafc8 | 134 | } //end while |
jboettcher | 0:327ccd5eafc8 | 135 | } //end main |
jboettcher | 0:327ccd5eafc8 | 136 |