for black mbed

Dependencies:   mbed C12832

Committer:
alexafer
Date:
Tue Jun 01 16:19:33 2021 +0000
Revision:
0:5e44a7bb06df
for the black mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexafer 0:5e44a7bb06df 1 #include "mbed.h"
alexafer 0:5e44a7bb06df 2 #include "C12832.h"
alexafer 0:5e44a7bb06df 3
alexafer 0:5e44a7bb06df 4 // defines msg
alexafer 0:5e44a7bb06df 5 #define SOS 0xff
alexafer 0:5e44a7bb06df 6 #define INIT 0xfe
alexafer 0:5e44a7bb06df 7 #define END 0xfd
alexafer 0:5e44a7bb06df 8 #define OK 0xfc
alexafer 0:5e44a7bb06df 9
alexafer 0:5e44a7bb06df 10 C12832 lcd(p5, p7, p6, p8, p11);
alexafer 0:5e44a7bb06df 11
alexafer 0:5e44a7bb06df 12 //pc mbed USB Slave function
alexafer 0:5e44a7bb06df 13 // connect mbed to pc USB
alexafer 0:5e44a7bb06df 14 RawSerial pc(USBTX, USBRX);
alexafer 0:5e44a7bb06df 15 //mbed LED
alexafer 0:5e44a7bb06df 16 DigitalOut led1(LED1);
alexafer 0:5e44a7bb06df 17
alexafer 0:5e44a7bb06df 18 AnalogIn sensorMQ2(p17);
alexafer 0:5e44a7bb06df 19 CAN can(p30, p29);
alexafer 0:5e44a7bb06df 20
alexafer 0:5e44a7bb06df 21 //constants
alexafer 0:5e44a7bb06df 22 const int numReadings = 500;
alexafer 0:5e44a7bb06df 23
alexafer 0:5e44a7bb06df 24 //MQ2
alexafer 0:5e44a7bb06df 25 const float airRatioMQ2 = 10.0;
alexafer 0:5e44a7bb06df 26 const float slopeMQ2 = -0.4687;
alexafer 0:5e44a7bb06df 27 const float interceptMQ2 = 1.3969;
alexafer 0:5e44a7bb06df 28
alexafer 0:5e44a7bb06df 29 //globals for the sensor readings
alexafer 0:5e44a7bb06df 30 float mq2sensorPPM = 0;
alexafer 0:5e44a7bb06df 31
alexafer 0:5e44a7bb06df 32 //globals for the R0 values
alexafer 0:5e44a7bb06df 33 float r0MQ2 = 0.83142;
alexafer 0:5e44a7bb06df 34
alexafer 0:5e44a7bb06df 35
alexafer 0:5e44a7bb06df 36 //gloabals for alarm values
alexafer 0:5e44a7bb06df 37 float alarmMQ2 = 20.0;
alexafer 0:5e44a7bb06df 38
alexafer 0:5e44a7bb06df 39 float calculateR0(AnalogIn s, float ratio) {
alexafer 0:5e44a7bb06df 40 float sensor_volt;
alexafer 0:5e44a7bb06df 41 float rs;
alexafer 0:5e44a7bb06df 42 float sensorValue = 0.0;
alexafer 0:5e44a7bb06df 43 float r0;
alexafer 0:5e44a7bb06df 44
alexafer 0:5e44a7bb06df 45 //take 500 sensor readings and add them together
alexafer 0:5e44a7bb06df 46 for(int i = 0; i < numReadings; i++) {
alexafer 0:5e44a7bb06df 47 sensorValue = sensorValue + s.read();
alexafer 0:5e44a7bb06df 48 }
alexafer 0:5e44a7bb06df 49
alexafer 0:5e44a7bb06df 50 sensorValue = sensorValue/numReadings;//average sensor value
alexafer 0:5e44a7bb06df 51 sensor_volt = sensorValue * 3.3;
alexafer 0:5e44a7bb06df 52 rs = ((3.3-sensor_volt)/sensor_volt);
alexafer 0:5e44a7bb06df 53 r0 = rs/ratio;
alexafer 0:5e44a7bb06df 54 pc.printf("RO VALUE: %f \n\n", r0);
alexafer 0:5e44a7bb06df 55 return r0;
alexafer 0:5e44a7bb06df 56 }
alexafer 0:5e44a7bb06df 57
alexafer 0:5e44a7bb06df 58 float determinePPM(AnalogIn sensor, float R0, float m, float b) {
alexafer 0:5e44a7bb06df 59 //Slope and y-intercept of ppm graph line, and R0 from previous calculations
alexafer 0:5e44a7bb06df 60 float voltage = sensor.read() * 3.3;
alexafer 0:5e44a7bb06df 61 float RS_gas = ((3.3-voltage)/voltage);
alexafer 0:5e44a7bb06df 62 float ppmRatio = RS_gas/R0;
alexafer 0:5e44a7bb06df 63 float ppm_log = (log10(ppmRatio)-b)/m;
alexafer 0:5e44a7bb06df 64 float ppm = pow(10, ppm_log);
alexafer 0:5e44a7bb06df 65 if(ppm<0){
alexafer 0:5e44a7bb06df 66 ppm = 0.0;
alexafer 0:5e44a7bb06df 67 }
alexafer 0:5e44a7bb06df 68 if(ppm>10000){
alexafer 0:5e44a7bb06df 69 ppm = 10000;
alexafer 0:5e44a7bb06df 70 }
alexafer 0:5e44a7bb06df 71 return ppm;
alexafer 0:5e44a7bb06df 72 }
alexafer 0:5e44a7bb06df 73
alexafer 0:5e44a7bb06df 74 void sendInfo()
alexafer 0:5e44a7bb06df 75 {
alexafer 0:5e44a7bb06df 76 char temp = 0;
alexafer 0:5e44a7bb06df 77 led1 = !led1;
alexafer 0:5e44a7bb06df 78 while(pc.readable()) {
alexafer 0:5e44a7bb06df 79 temp = pc.getc();
alexafer 0:5e44a7bb06df 80 if (temp == 'w')
alexafer 0:5e44a7bb06df 81 {
alexafer 0:5e44a7bb06df 82 pc.printf("{\"MQ2\":%f}\n", mq2sensorPPM);
alexafer 0:5e44a7bb06df 83 }
alexafer 0:5e44a7bb06df 84 }
alexafer 0:5e44a7bb06df 85 }
alexafer 0:5e44a7bb06df 86
alexafer 0:5e44a7bb06df 87 // main() runs in its own thread in the OS
alexafer 0:5e44a7bb06df 88 int main() {
alexafer 0:5e44a7bb06df 89 //Uncomment if we want to reset R0 from default to our environment
alexafer 0:5e44a7bb06df 90 r0MQ2 = calculateR0(sensorMQ2, airRatioMQ2);
alexafer 0:5e44a7bb06df 91
alexafer 0:5e44a7bb06df 92 pc.attach(&sendInfo, Serial::RxIrq);
alexafer 0:5e44a7bb06df 93 pc.baud(9600);
alexafer 0:5e44a7bb06df 94
alexafer 0:5e44a7bb06df 95 char msg;
alexafer 0:5e44a7bb06df 96
alexafer 0:5e44a7bb06df 97 while (1) {
alexafer 0:5e44a7bb06df 98
alexafer 0:5e44a7bb06df 99 mq2sensorPPM = determinePPM(sensorMQ2, r0MQ2, slopeMQ2, interceptMQ2);
alexafer 0:5e44a7bb06df 100 lcd.cls();
alexafer 0:5e44a7bb06df 101 lcd.locate(0,3);
alexafer 0:5e44a7bb06df 102 lcd.printf("MQ2 value: %f", mq2sensorPPM);
alexafer 0:5e44a7bb06df 103 if(mq2sensorPPM>alarmMQ2) {
alexafer 0:5e44a7bb06df 104 msg = SOS ;
alexafer 0:5e44a7bb06df 105 if (can.write(CANMessage(1337, &msg, 1))) {
alexafer 0:5e44a7bb06df 106 pc.printf("Message sent: %d\n", (int)msg);
alexafer 0:5e44a7bb06df 107 lcd.locate(0,15);
alexafer 0:5e44a7bb06df 108 lcd.printf("threshold!");
alexafer 0:5e44a7bb06df 109 }
alexafer 0:5e44a7bb06df 110 } else {
alexafer 0:5e44a7bb06df 111 msg = OK;
alexafer 0:5e44a7bb06df 112 if (can.write(CANMessage(1337, &msg, 1))) {
alexafer 0:5e44a7bb06df 113 pc.printf("Message sent: %d\n", (int)msg);
alexafer 0:5e44a7bb06df 114 }
alexafer 0:5e44a7bb06df 115 }
alexafer 0:5e44a7bb06df 116 wait(0.1);
alexafer 0:5e44a7bb06df 117 }
alexafer 0:5e44a7bb06df 118 }