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.
main.cpp
00001 #include "MicroBit.h" 00002 #include "MicroBitUARTService.h" 00003 00004 MicroBit uBit; 00005 MicroBitUARTService *uart; 00006 00007 uint16_t step_counter = 0; 00008 bool in_range = false; 00009 char stepsString[50]; 00010 00011 int connected = 0; 00012 00013 MicroBitImage img("0 0 0 0 1\n0 0 0 0 1\n0 0 0 0 1\n0 0 0 0 1\n0 0 0 0 1\n"); 00014 00015 // 00016 // Scales the given value that is in the -1024 to 1024 range 00017 // int a value between 0 and 4. 00018 // 00019 int pixel_from_g(int value) 00020 { 00021 int x = 0; 00022 00023 if (value > -750) 00024 x++; 00025 if (value > -250) 00026 x++; 00027 if (value > 250) 00028 x++; 00029 if (value > 750) 00030 x++; 00031 00032 return x; 00033 } 00034 00035 void onConnected(MicroBitEvent e) 00036 { 00037 //uBit.display.scroll("C"); 00038 00039 connected = 1; 00040 00041 while (connected == 1) { 00042 int x = pixel_from_g(uBit.accelerometer.getX()); 00043 int y = pixel_from_g(uBit.accelerometer.getY()); 00044 00045 uBit.display.image.clear(); 00046 uBit.display.image.setPixelValue(x, y, 255); 00047 //uBit.display.image.setPixelValue(1, 0, 255); 00048 if(y < 4) 00049 { 00050 in_range = true; 00051 while(in_range) 00052 { 00053 y = pixel_from_g(uBit.accelerometer.getY()); 00054 if(y == 4) 00055 { 00056 in_range = false; 00057 step_counter ++; 00058 } 00059 } 00060 sprintf(stepsString, "%d", step_counter); 00061 uart->send(stepsString); 00062 00063 uBit.serial.send("steps: "); 00064 uBit.serial.send(step_counter); 00065 uBit.serial.send("\r\n"); 00066 00067 } 00068 uBit.sleep(100); 00069 } 00070 00071 } 00072 void onDisconnected(MicroBitEvent e) 00073 { 00074 //uBit.display.scroll("D"); 00075 connected = 0; 00076 step_counter = 0; 00077 00078 00079 00080 while(connected == 0) 00081 { 00082 uBit.display.scroll(img, 50, -1); 00083 uBit.display.scroll(img, 50, +1); 00084 } 00085 } 00086 00087 00088 00089 int main() 00090 { 00091 // Initialise the micro:bit runtime. 00092 uBit.init(); 00093 uBit.serial.baud(115200); 00094 uBit.serial.send("Hello World\n"); 00095 00096 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected); 00097 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected); 00098 00099 uart = new MicroBitUARTService(*uBit.ble, 32, 32); 00100 00101 //uBit.display.scroll("Ready"); 00102 00103 00104 //MicroBitImage i(heart_w,heart_h,heart); 00105 //uBit.display.animate(i,10000,5); 00106 00107 while(connected == 0) 00108 { 00109 uBit.display.scroll(img, 50, -1); 00110 uBit.display.scroll(img, 50, +1); 00111 } 00112 00113 release_fiber(); 00114 00115 // 00116 // Periodically read the accelerometer x and y values, and plot a 00117 // scaled version of this ont the display. 00118 // 00119 //while(1) 00120 // { 00121 // int x = pixel_from_g(uBit.accelerometer.getX()); 00122 // int y = pixel_from_g(uBit.accelerometer.getY()); 00123 // 00124 // uBit.display.image.clear(); 00125 // uBit.display.image.setPixelValue(x, y, 255); 00126 // //uBit.display.image.setPixelValue(1, 0, 255); 00127 // if(y < 4) 00128 // { 00129 // in_range = true; 00130 // while(in_range) 00131 // { 00132 // y = pixel_from_g(uBit.accelerometer.getY()); 00133 // if(y == 4) 00134 // { 00135 // in_range = false; 00136 // step_counter ++; 00137 // } 00138 // } 00139 // //y = pixel_from_g(uBit.accelerometer.getY()); 00140 // //if(y == 4) 00141 // uBit.serial.send("steps: "); 00142 // uBit.serial.send(step_counter); 00143 // uBit.serial.send("\r\n"); 00144 // 00145 // } 00146 // 00147 // uBit.sleep(100); 00148 // } 00149 }
Generated on Wed Jul 13 2022 09:04:42 by
1.7.2