Magnetometer program example of use for NMHU students and others.

Dependencies:   MAG3110 SLCD TSI mbed

Fork of magsensor_46_v2 by Stanley Cohen

Committer:
scohennm
Date:
Sun Mar 29 18:24:10 2015 +0000
Revision:
1:cba20461ceec
Child:
2:06d3417f068e
Use of mag sensor on KL46Z also light sensor. Demo for NMHU Ambient computing class.

Who changed what in which revision?

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