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 #include "mbed.h" 00002 #include "PinDetect.h" 00003 #include "uLCD_4DGL.h" 00004 #include "Shiftbrite.h" 00005 00006 DigitalOut myled1(LED1); 00007 DigitalOut myled2(LED2); 00008 DigitalOut myled3(LED3); 00009 DigitalOut myled4(LED4); 00010 00011 // use class to setup pushbuttons pins 00012 PinDetect pb1(p23); 00013 PinDetect pb2(p24); 00014 00015 // use class to setup the Color LCD 00016 uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object 00017 00018 // Global variables used in callbacks and main program 00019 // C variables in interrupt routines should use volatile keyword 00020 int volatile magnetHit=0; 00021 00022 //timer setup 00023 Timer t; 00024 00025 // Callback routine is interrupt activated by a debounced magnet hit 00026 void pb1_hit_callback (void) 00027 { 00028 magnetHit=1; 00029 } 00030 00031 // Callback routine is interrupt activated by a debounced magnet unhit 00032 void pb2_hit_callback (void) 00033 { 00034 magnetHit=1; 00035 } 00036 00037 // use class to setup Shiftbrite pins 00038 Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci 00039 00040 // init 00041 float circumference=0, speed=0, tempFloat=0, changePercent=0, averageBlock=0; //average is float 00042 int a1=10000, a2=10000, a3=10000, a4=10000, a5=10000, cumulativeDistBlock=0, recentBlock=0; //avg values assume ~7.5km 00043 int highestBlock=0, lowestBlock=99999999, dayCount=0; 00044 unsigned int myColor = 0x000000; 00045 unsigned int shiftR=0, shiftG=0, shiftB=0; 00046 bool execSpaces=0; 00047 00048 int main() { 00049 //start timer 00050 t.start(); 00051 myled1 = 1; 00052 00053 shiftR = 50; 00054 myShiftbrite.write( shiftR, shiftG, shiftB); 00055 00056 uLCD.color(0xff3333); 00057 uLCD.locate(0,0); 00058 uLCD.printf(" ...Starting..."); 00059 00060 // Use internal pullups/downs for the pushbutton 00061 pb1.mode(PullDown); //PullUp gnd and input. PullDown vcc and input. 00062 pb2.mode(PullUp); 00063 // Delay for initial pullup to take effect 00064 wait(.1); 00065 // Setup Interrupt callback functions for a pb hit 00066 //pb1.attach_deasserted(&pb1_unhit_callback); 00067 pb1.attach_asserted(&pb1_hit_callback); 00068 pb2.attach_deasserted(&pb2_hit_callback); 00069 // Start sampling pb inputs using interrupts. leave as () default 20ms aka 20000. 00070 pb1.setSampleFrequency(10000); //using 10ms aka 10000 00071 pb2.setSampleFrequency(10000); 00072 //setSamplesTillAssert, default is 50 continuous samples 00073 pb1.setSamplesTillAssert(6); 00074 pb2.setSamplesTillAssert(6); 00075 00076 // Set RTC time to Wed, 28 Oct 2009 11:35:37 (January 1, 1970) 00077 set_time(1); 00078 //time_t mktime (tm * timeptr) //needed? 00079 00080 //setup circumference 00081 circumference = 3.14159*0.24; //pi * diam (m) //tweakMe was 0.254 00082 00083 wait(0.4); 00084 uLCD.locate(0,0); 00085 uLCD.color(0x4444ff); 00086 uLCD.printf(" Ultimate Hamster"); 00087 00088 //labels print in this color 00089 uLCD.color(0x8888ff); 00090 00091 // set up permanent characters on display 00092 uLCD.locate(0,2); 00093 uLCD.printf("Speed: (m/s)"); 00094 00095 uLCD.locate(0,4); 00096 uLCD.printf("Change: (%%)"); 00097 00098 //optimize other labels too one day... 00099 uLCD.locate(0,6); 00100 uLCD.printf("Recent: (m)"); 00101 uLCD.locate(0,8); 00102 uLCD.printf("Average: (m)"); 00103 uLCD.locate(0,10); 00104 uLCD.printf("Highest: (m)"); 00105 uLCD.locate(0,12); 00106 uLCD.printf("Lowest: (m)"); 00107 00108 uLCD.locate(0,15); 00109 uLCD.printf("0 /24hr day 0 "); 00110 00111 //old debug display... 00112 //uLCD.locate(0,14); 00113 //uLCD.printf("Time:"); 00114 //uLCD.locate(0,15); 00115 //uLCD.printf("Hour:"); 00116 00117 // end 00118 00119 //numbers print in this color 00120 uLCD.color(0xcccc33); 00121 //if you want to change this, also change value near changePercent print! 00122 00123 shiftR=0; 00124 shiftG=0; 00125 shiftB=50; 00126 myShiftbrite.write( shiftR, shiftG, shiftB); 00127 00128 00129 while(1) { 00130 00131 myled1 = !myled1; // show loop is active 00132 //shiftR = (rand()*40) % 40; 00133 //shiftG = (rand()*40) % 40; 00134 //shiftB = (rand()*40) % 40; 00135 shiftR = 0; 00136 shiftG = 0; 00137 shiftB = 0; 00138 myShiftbrite.write( shiftR, shiftG, shiftB); 00139 00140 switch (magnetHit) { 00141 case 0: 00142 myled2 = 1; 00143 myled3 = 0; 00144 break; 00145 case 1: 00146 00147 myled2 = 0; 00148 myled3 = 1; 00149 00150 //change title color 00151 //myColor = (rand()*0xffffff) % 0xffffff; 00152 00153 //change shiftbrite color 00154 myShiftbrite.write( 20, 20, 20); 00155 00156 //speed calc 00157 //using timer t. 00158 tempFloat = t.read(); 00159 t.reset(); 00160 00161 //send speed to display 00162 speed = circumference / tempFloat; 00163 uLCD.locate(7,2); 00164 uLCD.printf("%5.2F", speed); //speed: number 00165 00166 //4mph or 1.8m/s is a timer value of 0.44s per revolution 00167 if (tempFloat > 0.3) { //instead use 0.3 00168 cumulativeDistBlock = cumulativeDistBlock + 1; 00169 } else { 00170 //shiftbrite 00171 } 00172 00173 magnetHit=0; //force to zero 00174 00175 myShiftbrite.write( shiftR, shiftG, shiftB); 00176 00177 break; 00178 } 00179 00180 //for debug only 00181 //uLCD.locate(0,10); 00182 //uLCD.printf("t.read: %5.2F", t.read()); 00183 00184 //check if wheel is still for more than 5 seconds 00185 //t.stop(); //line not needed if t.read() can occur without pause 00186 if (t.read() > 3 && t.read() < 4) { //1 second window (24hr takes ~0.7s) 00187 uLCD.locate(8,2); 00188 uLCD.printf("zero"); //speed: zero 00189 } 00190 //t.start(); //line not needed if t.read() can occur without pause 00191 00192 if (t.read() > 300) { //every 5 mins, reset speed timer 00193 //prevent overflow, max value is 1800, 30 mins 00194 t.reset(); 00195 } 00196 00197 //get the current time 00198 time_t seconds = time(NULL); 00199 00200 uLCD.locate(0,15); 00201 uLCD.printf("%u", seconds/60/60 ); //change seconds to seconds/60/60 00202 00203 //time display for debug 00204 //uLCD.locate(6,14); 00205 //uLCD.printf("%u ", seconds ); 00206 //uLCD.locate(6,15); 00207 //uLCD.printf("%u ", seconds/60/60 ); 00208 00209 //this runs every 24hr block 00210 if (seconds >= 86400) { //86400 is 24hrs 00211 myShiftbrite.write( 50, 0, 0); 00212 00213 //compute 24hr block values 00214 recentBlock = cumulativeDistBlock; 00215 cumulativeDistBlock = 0; //reset 00216 dayCount++; //add one to day 00217 00218 //change tracking 00219 changePercent = 100*(recentBlock-averageBlock) / (averageBlock); //signed float 00220 00221 //shift numbers in array to delete oldest value 00222 a1 = a2; //oldest 00223 a2 = a3; 00224 a3 = a4; 00225 a4 = a5; 00226 a5 = recentBlock; //newest 00227 averageBlock = (a1 + a2 + a3 + a4 + a5)/5; //int becomes float 00228 00229 //check if recentBlock is new high or new low 00230 if (recentBlock > highestBlock) { //new high 00231 highestBlock = recentBlock; 00232 //Highest (m) store max value 00233 tempFloat = highestBlock*circumference; 00234 uLCD.locate(8,10); 00235 uLCD.printf("%7.0F", tempFloat); 00236 uLCD.locate(5,14); 00237 uLCD.printf("NEW HIGH"); 00238 execSpaces = 1; //if no update on next round, set spaces 00239 } else if (recentBlock < lowestBlock) { //new low 00240 lowestBlock = recentBlock; 00241 //Lowest (m) store min value 00242 tempFloat = lowestBlock*circumference; 00243 uLCD.locate(8,12); 00244 uLCD.printf("%7.0F", tempFloat); 00245 uLCD.locate(5,14); 00246 uLCD.printf("NEW LOW "); 00247 execSpaces = 1; 00248 } else if (execSpaces == 1) { 00249 uLCD.locate(5,14); 00250 uLCD.printf(" "); 00251 execSpaces = 0; 00252 } //state machine so that spaces will not overwrite spaces every time 00253 00254 //use tempFloat to convert int rotations to distance (m) 00255 //use circumference float 00256 //update the following on display 00257 00258 //Change (%) past 24hr block vs average 00259 if (changePercent > 0) { 00260 //green 00261 uLCD.color(0x33ff33); 00262 } else if (changePercent < 0) { 00263 //red 00264 uLCD.color(0xff3333); 00265 } else { 00266 //purple, white?? 00267 uLCD.color(0xff33ff); 00268 } 00269 //tempFloat = changePercent; 00270 uLCD.locate(8,4); 00271 uLCD.printf("%7.0F", changePercent); //NAN and INF possible! 00272 uLCD.color(0xcccc33); //reset color value!!!!! copy value from way above. 00273 00274 //Recent (m) past 24hr block 00275 tempFloat = recentBlock*circumference; 00276 uLCD.locate(8,6); 00277 uLCD.printf("%7.0F", tempFloat); 00278 //Average (m) average of the past 5 24hr 00279 tempFloat = averageBlock*circumference; 00280 uLCD.locate(8,8); 00281 uLCD.printf("%7.0F", tempFloat); 00282 //print for low and high happens only when value updated 00283 00284 //bottom line updates 00285 uLCD.locate(0,15); 00286 uLCD.printf("0 "); // remove leftover characters from XX/24hr 00287 uLCD.locate(15,15); //modify this 00288 uLCD.printf("%u", dayCount); 00289 00290 set_time(1); //reset 00291 00292 myShiftbrite.write( shiftR, shiftG, shiftB); 00293 } 00294 00295 } //while end 00296 }
Generated on Sat Sep 3 2022 16:28:26 by
1.7.2