Tests of LCD display, light sensor, magnetometer (raw) and analog input.
Dependencies: MAG3110 SLCD mbed
magsensorv2.cpp@0:8664d36d4ff3, 2014-08-14 (annotated)
- Committer:
- scohennm
- Date:
- Thu Aug 14 17:32:01 2014 +0000
- Revision:
- 0:8664d36d4ff3
This uses the right-hand switch on the KL46Z board to show different data modes. More of a test of how publishing works. The magnetometer and light sensor plus one analog input.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:8664d36d4ff3 | 1 | |
scohennm | 0:8664d36d4ff3 | 2 | #include "mbed.h" |
scohennm | 0:8664d36d4ff3 | 3 | #include "MAG3110.h" |
scohennm | 0:8664d36d4ff3 | 4 | #include "SLCD.h" |
scohennm | 0:8664d36d4ff3 | 5 | /* |
scohennm | 0:8664d36d4ff3 | 6 | Test of magnetometer and other sensors on the KL46Z board. Not clear to me at this |
scohennm | 0:8664d36d4ff3 | 7 | time what the conversion factor of the magnetic field values map to. |
scohennm | 0:8664d36d4ff3 | 8 | set up state machine to cycle through what is displayed on the LCD screen. Remember |
scohennm | 0:8664d36d4ff3 | 9 | there are 4 characthers plus a "free" decimal point. - 140721 sc |
scohennm | 0:8664d36d4ff3 | 10 | */ |
scohennm | 0:8664d36d4ff3 | 11 | #define MESSTIME 0.5 |
scohennm | 0:8664d36d4ff3 | 12 | #define BLINKTIME 500 |
scohennm | 0:8664d36d4ff3 | 13 | #define DATATIME 200 |
scohennm | 0:8664d36d4ff3 | 14 | #define LEDOFF 1 |
scohennm | 0:8664d36d4ff3 | 15 | #define LEDON 0 |
scohennm | 0:8664d36d4ff3 | 16 | #define MESSAGETIME 0.3 |
scohennm | 0:8664d36d4ff3 | 17 | #define LIGHT "LGHT" |
scohennm | 0:8664d36d4ff3 | 18 | #define MAGX_LBL "MAGX" |
scohennm | 0:8664d36d4ff3 | 19 | #define MAGY_LBL "MAGY" |
scohennm | 0:8664d36d4ff3 | 20 | #define BOTIX_LBL "BOTX" |
scohennm | 0:8664d36d4ff3 | 21 | |
scohennm | 0:8664d36d4ff3 | 22 | // States |
scohennm | 0:8664d36d4ff3 | 23 | #define NUMSTATES 4 |
scohennm | 0:8664d36d4ff3 | 24 | #define L_STATE 0 |
scohennm | 0:8664d36d4ff3 | 25 | #define MAGX_STATE 1 |
scohennm | 0:8664d36d4ff3 | 26 | #define MAGY_STATE 2 |
scohennm | 0:8664d36d4ff3 | 27 | #define BOTIX_STATE 3 |
scohennm | 0:8664d36d4ff3 | 28 | |
scohennm | 0:8664d36d4ff3 | 29 | #define PROGNAME "MAG v2.0" |
scohennm | 0:8664d36d4ff3 | 30 | #define LPRNAME "MV2.0" |
scohennm | 0:8664d36d4ff3 | 31 | |
scohennm | 0:8664d36d4ff3 | 32 | // look at this site for PIN defintions. http://mbed.org/platforms/FRDM-KL46Z/ |
scohennm | 0:8664d36d4ff3 | 33 | // #define PRINTDBUG |
scohennm | 0:8664d36d4ff3 | 34 | |
scohennm | 0:8664d36d4ff3 | 35 | MAG3110 mag(PTE25, PTE24); |
scohennm | 0:8664d36d4ff3 | 36 | |
scohennm | 0:8664d36d4ff3 | 37 | SLCD slcd; //define LCD display |
scohennm | 0:8664d36d4ff3 | 38 | AnalogIn LightSensor(PTE22); |
scohennm | 0:8664d36d4ff3 | 39 | AnalogIn MaxBotix(PTB0); |
scohennm | 0:8664d36d4ff3 | 40 | Serial pc(USBTX, USBRX); |
scohennm | 0:8664d36d4ff3 | 41 | Timer LEDTimer; |
scohennm | 0:8664d36d4ff3 | 42 | Timer DATATimer; |
scohennm | 0:8664d36d4ff3 | 43 | |
scohennm | 0:8664d36d4ff3 | 44 | void LCDMess(char *lMess){ |
scohennm | 0:8664d36d4ff3 | 45 | slcd.Home(); |
scohennm | 0:8664d36d4ff3 | 46 | slcd.clear(); |
scohennm | 0:8664d36d4ff3 | 47 | slcd.printf(lMess); |
scohennm | 0:8664d36d4ff3 | 48 | } |
scohennm | 0:8664d36d4ff3 | 49 | |
scohennm | 0:8664d36d4ff3 | 50 | void LCDTMess(char *lMess, float Ddelay){ |
scohennm | 0:8664d36d4ff3 | 51 | slcd.Home(); |
scohennm | 0:8664d36d4ff3 | 52 | slcd.clear(); |
scohennm | 0:8664d36d4ff3 | 53 | slcd.printf(lMess); |
scohennm | 0:8664d36d4ff3 | 54 | wait(Ddelay); |
scohennm | 0:8664d36d4ff3 | 55 | } |
scohennm | 0:8664d36d4ff3 | 56 | int main() { |
scohennm | 0:8664d36d4ff3 | 57 | DigitalOut Rled(LED_RED); |
scohennm | 0:8664d36d4ff3 | 58 | DigitalOut Gled(LED_GREEN); |
scohennm | 0:8664d36d4ff3 | 59 | int displayState = 2; // show magy first on LCD |
scohennm | 0:8664d36d4ff3 | 60 | int LButtonState; |
scohennm | 0:8664d36d4ff3 | 61 | DigitalIn LftButton(PTC3); |
scohennm | 0:8664d36d4ff3 | 62 | int ledState = true; |
scohennm | 0:8664d36d4ff3 | 63 | float lightValue; //Read from ADC connected to ambient light sensor (photo transistor) |
scohennm | 0:8664d36d4ff3 | 64 | int magX = 0, magY = 0, magZ = 0; // Magnetic field data |
scohennm | 0:8664d36d4ff3 | 65 | float MaxBValue; |
scohennm | 0:8664d36d4ff3 | 66 | char lcdData[10]; |
scohennm | 0:8664d36d4ff3 | 67 | |
scohennm | 0:8664d36d4ff3 | 68 | // Do once... equivalent to the setup fucntion in the Arduino paradigm |
scohennm | 0:8664d36d4ff3 | 69 | mag.begin(); |
scohennm | 0:8664d36d4ff3 | 70 | // Setup event timers for LED blink and taking magnetometer data |
scohennm | 0:8664d36d4ff3 | 71 | LEDTimer.start(); |
scohennm | 0:8664d36d4ff3 | 72 | LEDTimer.reset(); |
scohennm | 0:8664d36d4ff3 | 73 | DATATimer.start(); |
scohennm | 0:8664d36d4ff3 | 74 | DATATimer.reset(); |
scohennm | 0:8664d36d4ff3 | 75 | // Show program name LCD and Serial |
scohennm | 0:8664d36d4ff3 | 76 | pc.printf(PROGNAME); |
scohennm | 0:8664d36d4ff3 | 77 | LCDTMess(LPRNAME,MESSTIME); |
scohennm | 0:8664d36d4ff3 | 78 | // Do forever... equivalent to the loop fucntion in the Arduino paradigm |
scohennm | 0:8664d36d4ff3 | 79 | while (true) { |
scohennm | 0:8664d36d4ff3 | 80 | // read the inverted logic of the left button on the board. |
scohennm | 0:8664d36d4ff3 | 81 | LButtonState = !LftButton; |
scohennm | 0:8664d36d4ff3 | 82 | if (LButtonState) { //Change data that is displayed cycle through states |
scohennm | 0:8664d36d4ff3 | 83 | displayState++; |
scohennm | 0:8664d36d4ff3 | 84 | displayState = displayState % NUMSTATES; // Roll over if greater than the number of states defined |
scohennm | 0:8664d36d4ff3 | 85 | switch (displayState) { |
scohennm | 0:8664d36d4ff3 | 86 | case L_STATE: { |
scohennm | 0:8664d36d4ff3 | 87 | LCDTMess(LIGHT,MESSTIME); |
scohennm | 0:8664d36d4ff3 | 88 | break; |
scohennm | 0:8664d36d4ff3 | 89 | } |
scohennm | 0:8664d36d4ff3 | 90 | case MAGX_STATE: { |
scohennm | 0:8664d36d4ff3 | 91 | LCDTMess(MAGX_LBL,MESSTIME); |
scohennm | 0:8664d36d4ff3 | 92 | break; |
scohennm | 0:8664d36d4ff3 | 93 | } |
scohennm | 0:8664d36d4ff3 | 94 | case MAGY_STATE:{ |
scohennm | 0:8664d36d4ff3 | 95 | LCDTMess(MAGY_LBL,MESSTIME); |
scohennm | 0:8664d36d4ff3 | 96 | break; |
scohennm | 0:8664d36d4ff3 | 97 | } |
scohennm | 0:8664d36d4ff3 | 98 | case BOTIX_STATE:{ |
scohennm | 0:8664d36d4ff3 | 99 | LCDTMess(BOTIX_LBL,MESSTIME); |
scohennm | 0:8664d36d4ff3 | 100 | break; |
scohennm | 0:8664d36d4ff3 | 101 | } |
scohennm | 0:8664d36d4ff3 | 102 | |
scohennm | 0:8664d36d4ff3 | 103 | } |
scohennm | 0:8664d36d4ff3 | 104 | } |
scohennm | 0:8664d36d4ff3 | 105 | // Manage mag and light data |
scohennm | 0:8664d36d4ff3 | 106 | if (DATATimer.read_ms() > DATATIME){ //take data at DATATIME intervals |
scohennm | 0:8664d36d4ff3 | 107 | DATATimer.reset(); |
scohennm | 0:8664d36d4ff3 | 108 | lightValue = 1.0-LightSensor.read(); |
scohennm | 0:8664d36d4ff3 | 109 | mag.getValues(&magX, &magY, &magZ); |
scohennm | 0:8664d36d4ff3 | 110 | MaxBValue = MaxBotix.read(); |
scohennm | 0:8664d36d4ff3 | 111 | |
scohennm | 0:8664d36d4ff3 | 112 | #ifdef PRINTDBUG |
scohennm | 0:8664d36d4ff3 | 113 | pc.printf("%f\r\n", lightValue); |
scohennm | 0:8664d36d4ff3 | 114 | pc.printf("%d\r\n", magX); |
scohennm | 0:8664d36d4ff3 | 115 | pc.printf("%d\r\n", magY); |
scohennm | 0:8664d36d4ff3 | 116 | pc.printf("%d\r\n", MaxBotix); |
scohennm | 0:8664d36d4ff3 | 117 | #endif |
scohennm | 0:8664d36d4ff3 | 118 | |
scohennm | 0:8664d36d4ff3 | 119 | switch (displayState) { |
scohennm | 0:8664d36d4ff3 | 120 | case L_STATE: { |
scohennm | 0:8664d36d4ff3 | 121 | sprintf (lcdData,"%4.3f",lightValue); |
scohennm | 0:8664d36d4ff3 | 122 | break; |
scohennm | 0:8664d36d4ff3 | 123 | } |
scohennm | 0:8664d36d4ff3 | 124 | case MAGX_STATE: { |
scohennm | 0:8664d36d4ff3 | 125 | sprintf (lcdData,"%4d",magX/100); |
scohennm | 0:8664d36d4ff3 | 126 | break; |
scohennm | 0:8664d36d4ff3 | 127 | } |
scohennm | 0:8664d36d4ff3 | 128 | case MAGY_STATE: { |
scohennm | 0:8664d36d4ff3 | 129 | sprintf (lcdData,"%4d",magY/10); |
scohennm | 0:8664d36d4ff3 | 130 | break; |
scohennm | 0:8664d36d4ff3 | 131 | } |
scohennm | 0:8664d36d4ff3 | 132 | case BOTIX_STATE:{ |
scohennm | 0:8664d36d4ff3 | 133 | sprintf (lcdData,"%4.3f",MaxBValue); |
scohennm | 0:8664d36d4ff3 | 134 | break; |
scohennm | 0:8664d36d4ff3 | 135 | } |
scohennm | 0:8664d36d4ff3 | 136 | } |
scohennm | 0:8664d36d4ff3 | 137 | LCDMess(lcdData); |
scohennm | 0:8664d36d4ff3 | 138 | } |
scohennm | 0:8664d36d4ff3 | 139 | // Manage LED's |
scohennm | 0:8664d36d4ff3 | 140 | if(LEDTimer.read_ms() > BLINKTIME) { // Blink LED's for dramatic effect. |
scohennm | 0:8664d36d4ff3 | 141 | LEDTimer.reset(); |
scohennm | 0:8664d36d4ff3 | 142 | ledState = !ledState; |
scohennm | 0:8664d36d4ff3 | 143 | Rled = ledState; |
scohennm | 0:8664d36d4ff3 | 144 | Gled = !ledState; |
scohennm | 0:8664d36d4ff3 | 145 | } |
scohennm | 0:8664d36d4ff3 | 146 | |
scohennm | 0:8664d36d4ff3 | 147 | } |
scohennm | 0:8664d36d4ff3 | 148 | } |