Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect mbed
main.cpp
00001 00002 #include "mbed.h" 00003 #include "Speaker.h" 00004 #include "PinDetect.h" 00005 #include "uLCD_4DGL.h" 00006 // setup builtin LEDs 00007 DigitalOut myLed1(LED1); 00008 DigitalOut myLed2(LED2); 00009 DigitalOut myLed3(LED3); 00010 DigitalOut myLed4(LED4); 00011 // setup pushbuttons for debounced interrupt use 00012 PinDetect pb1(p23); 00013 PinDetect pb2(p24); 00014 PinDetect pb3(p25); 00015 PinDetect pb4(p26); 00016 00017 // setup LCD 00018 uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object 00019 00020 //moving include down here allows use in uLCD in Robot class draw and erase member functions 00021 //easier for now than passing a new uLCD_4DGL object argument to the class 00022 #include "Robot.h" 00023 00024 // setup instance of new Speaker class, mySpeaker using pin 21 00025 // the pin must be a PWM output pin 00026 Speaker mySpeaker(p21); 00027 00028 // pushbutton status - need volatile for safe interrupt R/W 00029 volatile int pbStatus = 0; 00030 00031 // Callback routine is interrupt activated by a debounced pb1 hit 00032 void pb1_hit_callback (void) 00033 { 00034 // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED 00035 pbStatus = pbStatus | 0x01; 00036 00037 } 00038 // Callback routine is interrupt activated by a debounced pb2 hit 00039 void pb2_hit_callback (void) 00040 { 00041 // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED 00042 pbStatus = pbStatus | 0x02; 00043 } 00044 // Callback routine is interrupt activated by a debounced pb3 hit 00045 void pb3_hit_callback (void) 00046 { 00047 // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED 00048 pbStatus = pbStatus | 0x04; 00049 } 00050 // Callback routine is interrupt activated by a debounced pb4 hit 00051 void pb4_hit_callback (void) 00052 { 00053 // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED 00054 pbStatus = pbStatus | 0x08; 00055 } 00056 00057 // main program 00058 int main() 00059 { 00060 uLCD.printf("\nmyRobot!\nyourname"); 00061 //setup four SPST push buttons 00062 pb1.mode(PullUp); //add internal pullup resistor 00063 pb2.mode(PullUp); 00064 pb3.mode(PullUp); 00065 pb4.mode(PullUp); 00066 // need a small delay for initial pullup to take effect due to capacitance 00067 wait(.01); 00068 // Setup Interrupt callback functions for each pb hit 00069 pb1.attach_deasserted(&pb1_hit_callback); 00070 pb2.attach_deasserted(&pb2_hit_callback); 00071 pb3.attach_deasserted(&pb3_hit_callback); 00072 pb4.attach_deasserted(&pb4_hit_callback); 00073 // Start sampling the pb inputs using interrupts 00074 pb1.setSampleFrequency(); 00075 pb2.setSampleFrequency(); 00076 pb3.setSampleFrequency(); 00077 pb4.setSampleFrequency(); 00078 // pushbuttons are all now setup with pullups and running with a software decounce filter 00079 00080 // use your modified robot class to move and display robot's new location on LCD 00081 Robot myRobot; 00082 00083 myRobot.draw(); //initial robot display in center of LCD 00084 //main loop to control and display robot 00085 while(1) { 00086 //check pushbutton status for new input 00087 switch (pbStatus) { 00088 //move forward 00089 case 0x01 : 00090 myLed1 = 1; 00091 pbStatus = 0; 00092 mySpeaker.PlayNote(200.0,0.1,0.8); 00093 myRobot.erase(); 00094 myRobot.moveForward(1); 00095 myRobot.draw(); 00096 break; 00097 //move backwards 00098 case 0x02 : 00099 myLed2 = 1; 00100 pbStatus = 0; 00101 mySpeaker.PlayNote(400.0,0.1,0.8); 00102 myRobot.erase(); 00103 myRobot.moveBackward(1); 00104 myRobot.draw(); 00105 break; 00106 //move left 00107 case 0x04 : 00108 myLed3 = 1; 00109 pbStatus = 0; 00110 mySpeaker.PlayNote(600.0,0.1,0.8); 00111 myRobot.erase(); 00112 myRobot.moveLeft(1); 00113 myRobot.draw(); 00114 break; 00115 //move right 00116 case 0x08 : 00117 myLed4 = 1; 00118 pbStatus = 0; 00119 mySpeaker.PlayNote(800.0,0.1,0.8); 00120 myRobot.erase(); 00121 myRobot.moveRight(1); 00122 myRobot.draw(); 00123 break; 00124 //no pb or multiple pbs hit 00125 default : 00126 // myLed1 = 0; 00127 myLed2 = 0; 00128 myLed3 = 0; 00129 myLed4 = 0; 00130 pbStatus = 0; 00131 } //end switch 00132 myLed1 = !myLed1; //blink to show loop is still running 00133 wait(.05); 00134 } //end while 00135 } //end main 00136
Generated on Wed Jul 27 2022 00:22:05 by
1.7.2