P422_Labs
/
elevator
lab 7 code
Diff: main.cpp
- Revision:
- 1:2a6907395c5d
- Parent:
- 0:4350259cf0d0
--- a/main.cpp Thu Mar 08 16:58:55 2018 +0000 +++ b/main.cpp Sat Mar 10 15:51:19 2018 +0000 @@ -1,5 +1,7 @@ #include "mbed.h" #include "MPL3115A2.h" +#include "string" +#include "ctype.h" Serial pc(SERIAL_TX, SERIAL_RX); DigitalOut myled(LED1); @@ -12,69 +14,10 @@ struct dataPoints { double pres; - double tmp; + double alt; }; - -//Solution entails - -int main() { - - dataPoints data[100]; - - uint8_t id; - pc.printf("\n\r*** MPL3115A2 Pressure/Temperature Sensor Test *** \n\r"); - while ((id=pressure_sensor.getID())!=0xC4) { - pc.printf("Status read unsuccessful: Value = 0x%02x\n\r",id); - pc.printf("Check wiring to the pressure sensor\n\r",id); - pc.printf("Retesting for correct ID in 1 second...\n\r"); - wait(1); - } - //uint8_t tmp[2]; - //tmp[0] = 0x26; -// pressure_sensor.readRegs(0x26, &tmp[1], 1); -// tmp[1] = tmp[1] & 0xC6; -// pressure_sensor.writeRegs(tmp,2); - pc.printf("Status read successfully: Value = 0x%02x\n\r",id); - pc.printf("***1hz readings from the pressure sensor***\n\r"); - - int counter= 0; - while(1) { - //this is where I would check for the GPIO pin if implemented - if(counter!=100){ - dataPoints p1 = {0,0}; - //pc.printf("%f hPA; %f m\n\r",pressure_sensor.getPressure(),pressure_sensor.getAltitude()); - p1.pres = pressure_sensor.getPressure(); - p1.tmp = pressure_sensor.getTemperature(); - data[counter] = p1; - //pc.printf("STORED VALUES: \n\r Pressure: %f hPA, Altitude: %f m\n\r",data[0].pres,data[0].alt); - pc.printf("DATA STORED\n\r"); - - //if the counter is going to go over the 100 limit reset it to zero and then - if(counter+1<=100){ - counter++; - } - else{ - counter=0; - } - myled = !myled; - wait(1); - } - else{ - pc.printf("STORED VALUES: \n\r Pressure: %f hPA, Altitude: %f m\n\r",data[0].pres,data[0].tmp); - for(int i=1; i<=counter; i++){ - pc.printf("Pressure: %f hPA, Altitude: %f m\n\r",data[i].pres,data[i].tmp); - } - for(int i=0; i<=100; i++){ - data[i].pres=NULL; - data[i].tmp=NULL; - } - pc.printf("Cleared Data \n\r"); - } - } -} - int mpl3115_reg_print(int start, int length) { if(0x00 <= start && start <=0x2D){ @@ -388,4 +331,78 @@ return -1; } } -} \ No newline at end of file +} + +//Solution entails +char s[1]; +int main() { + + dataPoints data[100]; + + uint8_t id; + pc.printf("\n\r*** MPL3115A2 Pressure/Altitude Sensor Test *** \n\r"); + while ((id=pressure_sensor.getID())!=0xC4) { + pc.printf("Status read unsuccessful: Value = 0x%02x\n\r",id); + pc.printf("Check wiring to the pressure sensor\n\r",id); + pc.printf("Retesting for correct ID in 1 second...\n\r"); + wait(1); + } + //uint8_t tmp[2]; + //tmp[0] = 0x26; +// pressure_sensor.readRegs(0x26, &tmp[1], 1); +// tmp[1] = tmp[1] & 0xC6; +// pressure_sensor.writeRegs(tmp,2); + pc.printf("Status read successfully: Value = 0x%02x\n\r",id); + pc.printf("***1hz readings from the pressure sensor***\n\r"); + + int counter= 0; + while(1) { + mpl3115_reg_print(0,0); + //this is where I would check for the GPIO pin if implemented + if(counter!=100){ + dataPoints p1 = {0,0}; + //pc.printf("%f hPA; %f m\n\r",pressure_sensor.getPressure(),pressure_sensor.getAltitude()); + p1.pres = pressure_sensor.getPressure(); + p1.alt = pressure_sensor.getAltitude(); + data[counter] = p1; + //pc.printf("STORED VALUES: \n\r Pressure: %f hPA, Altitude: %f m\n\r",data[0].pres,data[0].alt); + pc.printf("DATA STORED\n\r"); + if (pc.readable()){ + s[1]=pc.getc(); + if(s[1] == 'p'){ + pc.printf("STORED VALUES: \n\r Pressure: %f hPA, Altitude: %f m\n\r",data[0].pres,data[0].alt); + for(int i=1; i<=counter; i++){ + pc.printf("Pressure: %f hPA, Altitude: %f m\n\r",data[i].pres,data[i].alt); + s[1] = 'x'; + + } + } + } + + + //if the counter is going to go over the 100 limit reset it to zero and then + if(counter+1<=100){ + counter++; + } + else{ + counter=0; + } + myled = !myled; + wait(1); + } + else{ + pc.printf("STORED VALUES: \n\r Pressure: %f hPA, Altitude: %f m\n\r",data[0].pres,data[0].alt); + for(int i=1; i<=counter; i++){ + pc.printf("Pressure: %f hPA, Altitude: %f m\n\r",data[i].pres,data[i].alt); + } + for(int i=0; i<=100; i++){ + data[i].pres=NULL; + data[i].alt=NULL; + } + pc.printf("Cleared Data \n\r"); + } + + } + + +}