PRO2_Team 1_collected code with ticker_not working yet

Dependencies:   SHTx mbed

Fork of PRO2_samlet_kode by Software hold - Team 1 - PRO2 2017

Committer:
OlgaHoeyer
Date:
Thu May 11 11:40:27 2017 +0000
Revision:
0:21e9cc38dd31
Child:
1:135bd458e39e
EDE PRO2 Team 1: "Awareness and Energioptimering. Udluftnings infoinator". Summary of the code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OlgaHoeyer 0:21e9cc38dd31 1 //Defining and Including stuff up here
OlgaHoeyer 0:21e9cc38dd31 2 #include <mbed.h>
OlgaHoeyer 0:21e9cc38dd31 3 #include <stdlib.h>
OlgaHoeyer 0:21e9cc38dd31 4 #include <stdio.h>
OlgaHoeyer 0:21e9cc38dd31 5 #include <string.h>
OlgaHoeyer 0:21e9cc38dd31 6 #include "SHTx/sht15.hpp" //"CMSIS Target not recognised"
OlgaHoeyer 0:21e9cc38dd31 7 #define temperature_low = 18; //For use in the Data Analysis
OlgaHoeyer 0:21e9cc38dd31 8 #define temperature_high = 25; //For use in the Data Analysis
OlgaHoeyer 0:21e9cc38dd31 9 #include "rgb_led.h" //For use in the RGB_LED_output part
OlgaHoeyer 0:21e9cc38dd31 10 #include "7_segment_control.h" //For use in the RGB_LED_output part
OlgaHoeyer 0:21e9cc38dd31 11
OlgaHoeyer 0:21e9cc38dd31 12
OlgaHoeyer 0:21e9cc38dd31 13 //The correct order of files:
OlgaHoeyer 0:21e9cc38dd31 14
OlgaHoeyer 0:21e9cc38dd31 15 //Declaration of GLOBAL VARIABLES:
OlgaHoeyer 0:21e9cc38dd31 16 float temperature, humidity; //this will be data read from sensor
OlgaHoeyer 0:21e9cc38dd31 17 void GetTemperatureAndHumidity();
OlgaHoeyer 0:21e9cc38dd31 18
OlgaHoeyer 0:21e9cc38dd31 19 Serial pc(USBTX, USBRX);
OlgaHoeyer 0:21e9cc38dd31 20 DigitalOut busy(LED1); //Don't think we need it.
OlgaHoeyer 0:21e9cc38dd31 21 InterruptIn event(USER_BUTTON);
OlgaHoeyer 0:21e9cc38dd31 22 DigitalOut myled(LED1);
OlgaHoeyer 0:21e9cc38dd31 23 DigitalIn up(p5);
OlgaHoeyer 0:21e9cc38dd31 24 DigitalIn down(p6);
OlgaHoeyer 0:21e9cc38dd31 25
OlgaHoeyer 0:21e9cc38dd31 26 int status, humidity, temperature;
OlgaHoeyer 0:21e9cc38dd31 27 int Compare_values();
OlgaHoeyer 0:21e9cc38dd31 28
OlgaHoeyer 0:21e9cc38dd31 29 //ports on the Nucleo: PB_8, PB_9
OlgaHoeyer 0:21e9cc38dd31 30 SHTx::SHT15 sensor(PB_8, PB_9);
OlgaHoeyer 0:21e9cc38dd31 31
OlgaHoeyer 0:21e9cc38dd31 32
OlgaHoeyer 0:21e9cc38dd31 33 //At the very top, we're going to have main()
OlgaHoeyer 0:21e9cc38dd31 34
OlgaHoeyer 0:21e9cc38dd31 35 int main() {
OlgaHoeyer 0:21e9cc38dd31 36
OlgaHoeyer 0:21e9cc38dd31 37 Logging();
OlgaHoeyer 0:21e9cc38dd31 38
OlgaHoeyer 0:21e9cc38dd31 39 Hiberation();
OlgaHoeyer 0:21e9cc38dd31 40
OlgaHoeyer 0:21e9cc38dd31 41 Settings();
OlgaHoeyer 0:21e9cc38dd31 42
OlgaHoeyer 0:21e9cc38dd31 43 //Data Collection
OlgaHoeyer 0:21e9cc38dd31 44 GetTemperatureAndHumidity();
OlgaHoeyer 0:21e9cc38dd31 45
OlgaHoeyer 0:21e9cc38dd31 46 Comparevalues(); //Data Analysis
OlgaHoeyer 0:21e9cc38dd31 47
OlgaHoeyer 0:21e9cc38dd31 48 void rgb_outp(int status)(); //RGB_LED_output
OlgaHoeyer 0:21e9cc38dd31 49
OlgaHoeyer 0:21e9cc38dd31 50 return 0;
OlgaHoeyer 0:21e9cc38dd31 51 }
OlgaHoeyer 0:21e9cc38dd31 52
OlgaHoeyer 0:21e9cc38dd31 53
OlgaHoeyer 0:21e9cc38dd31 54 //1) Logging
OlgaHoeyer 0:21e9cc38dd31 55 void Logging() { //This needs to be updated before we have
OlgaHoeyer 0:21e9cc38dd31 56 //the final program done.
OlgaHoeyer 0:21e9cc38dd31 57 FILE *fp;
OlgaHoeyer 0:21e9cc38dd31 58
OlgaHoeyer 0:21e9cc38dd31 59
OlgaHoeyer 0:21e9cc38dd31 60 if ((fp = fopen("Logger_data", "a+")) == NULL) { //filename logging_data
OlgaHoeyer 0:21e9cc38dd31 61 fprintf (stdout, "Can't open \"Logger_data\" file.\n");
OlgaHoeyer 0:21e9cc38dd31 62 exit(EXIT_FAILURE);//making sure that we can open the file
OlgaHoeyer 0:21e9cc38dd31 63 }
OlgaHoeyer 0:21e9cc38dd31 64
OlgaHoeyer 0:21e9cc38dd31 65 fprintf(fp,"\n\nlogger_data kl.00.00 D.dd-mm-yyyy\n");//writing in the file
OlgaHoeyer 0:21e9cc38dd31 66 fprintf(fp,"Temperatur:x\n");//variable from temp will be put here
OlgaHoeyer 0:21e9cc38dd31 67 fprintf(fp,"Co2:x\n");//variable from Co2
OlgaHoeyer 0:21e9cc38dd31 68 fprintf(fp,"H2o:x"); //variable from H2o
OlgaHoeyer 0:21e9cc38dd31 69
OlgaHoeyer 0:21e9cc38dd31 70
OlgaHoeyer 0:21e9cc38dd31 71 fprintf(fp,"Logger_data update complete!");
OlgaHoeyer 0:21e9cc38dd31 72 if (fclose(fp) != 0)
OlgaHoeyer 0:21e9cc38dd31 73 fprintf(stderr, "Error closing file\n"); //Closing file
OlgaHoeyer 0:21e9cc38dd31 74
OlgaHoeyer 0:21e9cc38dd31 75 //i and t are assigned to 0 here
OlgaHoeyer 0:21e9cc38dd31 76 i=0
OlgaHoeyer 0:21e9cc38dd31 77 t=0
OlgaHoeyer 0:21e9cc38dd31 78
OlgaHoeyer 0:21e9cc38dd31 79 return 0; //goes into Hibernation
OlgaHoeyer 0:21e9cc38dd31 80 }
OlgaHoeyer 0:21e9cc38dd31 81
OlgaHoeyer 0:21e9cc38dd31 82
OlgaHoeyer 0:21e9cc38dd31 83
OlgaHoeyer 0:21e9cc38dd31 84 //2) Hibernation
OlgaHoeyer 0:21e9cc38dd31 85
OlgaHoeyer 0:21e9cc38dd31 86
OlgaHoeyer 0:21e9cc38dd31 87 void Hibernation() {
OlgaHoeyer 0:21e9cc38dd31 88 int t;
OlgaHoeyer 0:21e9cc38dd31 89 int i;
OlgaHoeyer 0:21e9cc38dd31 90 //Or whatever that brings it into Hibernation at once
OlgaHoeyer 0:21e9cc38dd31 91
OlgaHoeyer 0:21e9cc38dd31 92 while(1)
OlgaHoeyer 0:21e9cc38dd31 93 {
OlgaHoeyer 0:21e9cc38dd31 94 if (i>0)
OlgaHoeyer 0:21e9cc38dd31 95 {
OlgaHoeyer 0:21e9cc38dd31 96 Settings();
OlgaHoeyer 0:21e9cc38dd31 97 }
OlgaHoeyer 0:21e9cc38dd31 98 else if (t=600) {
OlgaHoeyer 0:21e9cc38dd31 99 GetTemperatureAndHumidity(); //Data Collection
OlgaHoeyer 0:21e9cc38dd31 100 Comparevalues(); //Data Analysis
OlgaHoeyer 0:21e9cc38dd31 101 rgb.output(Olga_variable);
OlgaHoeyer 0:21e9cc38dd31 102 }
OlgaHoeyer 0:21e9cc38dd31 103 else
OlgaHoeyer 0:21e9cc38dd31 104 {
OlgaHoeyer 0:21e9cc38dd31 105 t++;
OlgaHoeyer 0:21e9cc38dd31 106 wait 1000 ms;
OlgaHoeyer 0:21e9cc38dd31 107 }
OlgaHoeyer 0:21e9cc38dd31 108
OlgaHoeyer 0:21e9cc38dd31 109
OlgaHoeyer 0:21e9cc38dd31 110 return 0;
OlgaHoeyer 0:21e9cc38dd31 111
OlgaHoeyer 0:21e9cc38dd31 112
OlgaHoeyer 0:21e9cc38dd31 113
OlgaHoeyer 0:21e9cc38dd31 114 //3) Settings
OlgaHoeyer 0:21e9cc38dd31 115
OlgaHoeyer 0:21e9cc38dd31 116 void Settings(int t,i,set_temp,extern int timespan) {
OlgaHoeyer 0:21e9cc38dd31 117 while(i<1){ //Opening set_temp for input
OlgaHoeyer 0:21e9cc38dd31 118
OlgaHoeyer 0:21e9cc38dd31 119
OlgaHoeyer 0:21e9cc38dd31 120 if (up==1) {//Recieving input from the user
OlgaHoeyer 0:21e9cc38dd31 121 set_temp = (set_temp +1); } //if up, temp rises by 1
OlgaHoeyer 0:21e9cc38dd31 122 if (down==1) { //Recieving input from the user
OlgaHoeyer 0:21e9cc38dd31 123 set_temp = (set_temp -1);} //If down, temp drops by 1
OlgaHoeyer 0:21e9cc38dd31 124 else if (timespan == 500);{
OlgaHoeyer 0:21e9cc38dd31 125 break;}
OlgaHoeyer 0:21e9cc38dd31 126 }
OlgaHoeyer 0:21e9cc38dd31 127
OlgaHoeyer 0:21e9cc38dd31 128 //If the system is passive for 5 seconds, i will be assigned to 2
OlgaHoeyer 0:21e9cc38dd31 129 //and t will get the value 700
OlgaHoeyer 0:21e9cc38dd31 130 //This forces it to go past hibernation
OlgaHoeyer 0:21e9cc38dd31 131
OlgaHoeyer 0:21e9cc38dd31 132 i=2
OlgaHoeyer 0:21e9cc38dd31 133 t=700
OlgaHoeyer 0:21e9cc38dd31 134 return 0;
OlgaHoeyer 0:21e9cc38dd31 135 }
OlgaHoeyer 0:21e9cc38dd31 136
OlgaHoeyer 0:21e9cc38dd31 137
OlgaHoeyer 0:21e9cc38dd31 138 //4) Data Collection
OlgaHoeyer 0:21e9cc38dd31 139
OlgaHoeyer 0:21e9cc38dd31 140 void GetTemperatureAndHumidity()
OlgaHoeyer 0:21e9cc38dd31 141 {
OlgaHoeyer 0:21e9cc38dd31 142 // Speed things up a bit.
OlgaHoeyer 0:21e9cc38dd31 143 sensor.setOTPReload(false);
OlgaHoeyer 0:21e9cc38dd31 144 sensor.setResolution(true);
OlgaHoeyer 0:21e9cc38dd31 145
OlgaHoeyer 0:21e9cc38dd31 146 while(1)
OlgaHoeyer 0:21e9cc38dd31 147 {
OlgaHoeyer 0:21e9cc38dd31 148 busy = true;
OlgaHoeyer 0:21e9cc38dd31 149 sensor.update();
OlgaHoeyer 0:21e9cc38dd31 150 busy = false;
OlgaHoeyer 0:21e9cc38dd31 151
OlgaHoeyer 0:21e9cc38dd31 152 // Temperature in celcius
OlgaHoeyer 0:21e9cc38dd31 153 sensor.setScale(false);
OlgaHoeyer 0:21e9cc38dd31 154 temperature=sensor.getTemperature(); //don't know if it works
OlgaHoeyer 0:21e9cc38dd31 155
OlgaHoeyer 0:21e9cc38dd31 156 // Relative Humidity
OlgaHoeyer 0:21e9cc38dd31 157 humidity=sensor.getHumidity(); //don't know if it works
OlgaHoeyer 0:21e9cc38dd31 158
OlgaHoeyer 0:21e9cc38dd31 159 wait(5);
OlgaHoeyer 0:21e9cc38dd31 160 return;
OlgaHoeyer 0:21e9cc38dd31 161 }
OlgaHoeyer 0:21e9cc38dd31 162 }
OlgaHoeyer 0:21e9cc38dd31 163
OlgaHoeyer 0:21e9cc38dd31 164 //5) Data Analysis
OlgaHoeyer 0:21e9cc38dd31 165
OlgaHoeyer 0:21e9cc38dd31 166 void Comparevalues() {
OlgaHoeyer 0:21e9cc38dd31 167
OlgaHoeyer 0:21e9cc38dd31 168 //__________________________________
OlgaHoeyer 0:21e9cc38dd31 169 // Definition of limits
OlgaHoeyer 0:21e9cc38dd31 170 //__________________________________
OlgaHoeyer 0:21e9cc38dd31 171
OlgaHoeyer 0:21e9cc38dd31 172 //Humidity limits.
OlgaHoeyer 0:21e9cc38dd31 173 const int HUMIDITY_LOW = 20;
OlgaHoeyer 0:21e9cc38dd31 174 const int HUMIDITY_HIGH= 60;
OlgaHoeyer 0:21e9cc38dd31 175
OlgaHoeyer 0:21e9cc38dd31 176 //Extra limits where LED will start blinking in red:
OlgaHoeyer 0:21e9cc38dd31 177 //13 < Temp < 30; 10%< hum < 80%
OlgaHoeyer 0:21e9cc38dd31 178 const int HUMIDITY_EXTRA_LOW = 10;
OlgaHoeyer 0:21e9cc38dd31 179 const int HUMIDITY_EXTRA_HIGH= 80;
OlgaHoeyer 0:21e9cc38dd31 180 const int TEMPERATURE_EXTRA_LOW = 13;
OlgaHoeyer 0:21e9cc38dd31 181 const int TEMPERATURE_EXTRA_HIGH= 30;
OlgaHoeyer 0:21e9cc38dd31 182
OlgaHoeyer 0:21e9cc38dd31 183 //Definition of output values
OlgaHoeyer 0:21e9cc38dd31 184 const int GOOD_LEVEL = 1; //green
OlgaHoeyer 0:21e9cc38dd31 185 const int ACCEPT_LEVEL = 2; //orange
OlgaHoeyer 0:21e9cc38dd31 186 const int CRITICAL_LEVEL = 3; //red
OlgaHoeyer 0:21e9cc38dd31 187 const int EXTRA_CRITICAL_LEVEL = 4; //red strobe
OlgaHoeyer 0:21e9cc38dd31 188 const int USER_NEEDED = 5; //blue strobe
OlgaHoeyer 0:21e9cc38dd31 189
OlgaHoeyer 0:21e9cc38dd31 190
OlgaHoeyer 0:21e9cc38dd31 191 //__________________________________
OlgaHoeyer 0:21e9cc38dd31 192 // main body: comparison
OlgaHoeyer 0:21e9cc38dd31 193 //__________________________________
OlgaHoeyer 0:21e9cc38dd31 194
OlgaHoeyer 0:21e9cc38dd31 195 humidity=(int)hum; //convertation of float to int.
OlgaHoeyer 0:21e9cc38dd31 196 temperature=(int)temp;
OlgaHoeyer 0:21e9cc38dd31 197
OlgaHoeyer 0:21e9cc38dd31 198 // check if there is a connection to the sensor
OlgaHoeyer 0:21e9cc38dd31 199 // (if null=no data=there is no connection
OlgaHoeyer 0:21e9cc38dd31 200
OlgaHoeyer 0:21e9cc38dd31 201 if ((temperature == 0) || (humidity == 0))
OlgaHoeyer 0:21e9cc38dd31 202 status= USER_NEEDED;
OlgaHoeyer 0:21e9cc38dd31 203
OlgaHoeyer 0:21e9cc38dd31 204 //comparing in nested loop.
OlgaHoeyer 0:21e9cc38dd31 205 else if ((temperature < TEMPERATURE_EXTRA_LOW) ||
OlgaHoeyer 0:21e9cc38dd31 206 (temperature > TEMPERATURE_EXTRA_HIGH))
OlgaHoeyer 0:21e9cc38dd31 207
OlgaHoeyer 0:21e9cc38dd31 208 status= EXTRA_CRITICAL_LEVEL;
OlgaHoeyer 0:21e9cc38dd31 209
OlgaHoeyer 0:21e9cc38dd31 210 else if ((humidity < HUMIDITY_EXTRA_LOW) ||
OlgaHoeyer 0:21e9cc38dd31 211 (humidity > HUMIDITY_EXTRA_HIGH))
OlgaHoeyer 0:21e9cc38dd31 212
OlgaHoeyer 0:21e9cc38dd31 213 status= EXTRA_CRITICAL_LEVEL;
OlgaHoeyer 0:21e9cc38dd31 214
OlgaHoeyer 0:21e9cc38dd31 215 else if ((temperature < temperature_low) ||
OlgaHoeyer 0:21e9cc38dd31 216 (temperature > temperature_high))
OlgaHoeyer 0:21e9cc38dd31 217
OlgaHoeyer 0:21e9cc38dd31 218 status= CRITICAL_LEVEL;
OlgaHoeyer 0:21e9cc38dd31 219
OlgaHoeyer 0:21e9cc38dd31 220 else if ((temperature < temperature_high) &&
OlgaHoeyer 0:21e9cc38dd31 221 (temperature > temperature_low) &&
OlgaHoeyer 0:21e9cc38dd31 222 (humidity < HUMIDITY_HIGH) &&
OlgaHoeyer 0:21e9cc38dd31 223 (humidity > HUMIDITY_LOW))
OlgaHoeyer 0:21e9cc38dd31 224
OlgaHoeyer 0:21e9cc38dd31 225 status= GOOD_LEVEL;
OlgaHoeyer 0:21e9cc38dd31 226
OlgaHoeyer 0:21e9cc38dd31 227 else status= ACCEPT_LEVEL;
OlgaHoeyer 0:21e9cc38dd31 228
OlgaHoeyer 0:21e9cc38dd31 229 return(status);
OlgaHoeyer 0:21e9cc38dd31 230 }
OlgaHoeyer 0:21e9cc38dd31 231
OlgaHoeyer 0:21e9cc38dd31 232
OlgaHoeyer 0:21e9cc38dd31 233 //6) RGB_LED_output
OlgaHoeyer 0:21e9cc38dd31 234 void rgb_outp(int status) {
OlgaHoeyer 0:21e9cc38dd31 235
OlgaHoeyer 0:21e9cc38dd31 236 int status; // Status carries data from data analysis().
OlgaHoeyer 0:21e9cc38dd31 237 RGB_LED lamp(PC_8,PC_6,PB_9);// Creates an object out of the class RGB_LED.
OlgaHoeyer 0:21e9cc38dd31 238 // Connect pins on the Nucleo, to the pins the
OlgaHoeyer 0:21e9cc38dd31 239 // class.
OlgaHoeyer 0:21e9cc38dd31 240 // Following is a object of the class segment_7. It is the actual analogy
OlgaHoeyer 0:21e9cc38dd31 241 // to our 7 segmented displays. There needs to go 11 pins into the parenteses.
OlgaHoeyer 0:21e9cc38dd31 242 //segment_7 seg_num(11 pins);
OlgaHoeyer 0:21e9cc38dd31 243
OlgaHoeyer 0:21e9cc38dd31 244 enum colour // Enumeration is used only for making the program more
OlgaHoeyer 0:21e9cc38dd31 245 { // easily readable.
OlgaHoeyer 0:21e9cc38dd31 246 green, // Is alike an int, starts at green = 1,
OlgaHoeyer 0:21e9cc38dd31 247 orange, // orange = 2 etc.
OlgaHoeyer 0:21e9cc38dd31 248 red,
OlgaHoeyer 0:21e9cc38dd31 249 red_blink,
OlgaHoeyer 0:21e9cc38dd31 250 blue
OlgaHoeyer 0:21e9cc38dd31 251 };
OlgaHoeyer 0:21e9cc38dd31 252
OlgaHoeyer 0:21e9cc38dd31 253 colour RGB_out = static_cast<colour>(status);
OlgaHoeyer 0:21e9cc38dd31 254 // Taking the value from the sensors and change them the enum type.
OlgaHoeyer 0:21e9cc38dd31 255 // Enum is somewhat akin to an int already, but RGB_out != status..
OlgaHoeyer 0:21e9cc38dd31 256
OlgaHoeyer 0:21e9cc38dd31 257 switch(RGB_out)
OlgaHoeyer 0:21e9cc38dd31 258 {
OlgaHoeyer 0:21e9cc38dd31 259 case green:
OlgaHoeyer 0:21e9cc38dd31 260 lamp.set(0.0f, 1.0f, 0.0f);
OlgaHoeyer 0:21e9cc38dd31 261 lamp.flash(1.0f, 1.0f); // On constantly.
OlgaHoeyer 0:21e9cc38dd31 262 break;
OlgaHoeyer 0:21e9cc38dd31 263 case orange:
OlgaHoeyer 0:21e9cc38dd31 264 lamp.set(1.0f, 0.6f, 0.0f);
OlgaHoeyer 0:21e9cc38dd31 265 lamp.flash(1.0f, 1.0f); // On constantly.
OlgaHoeyer 0:21e9cc38dd31 266 break;
OlgaHoeyer 0:21e9cc38dd31 267 case red:
OlgaHoeyer 0:21e9cc38dd31 268 lamp.set(1.0f, 0.0f, 0.0f);
OlgaHoeyer 0:21e9cc38dd31 269 lamp.flash(1.0f, 1.0f); // On constantly.
OlgaHoeyer 0:21e9cc38dd31 270 break;
OlgaHoeyer 0:21e9cc38dd31 271 case red_blink:
OlgaHoeyer 0:21e9cc38dd31 272 lamp.set(1.0f, 0.0f, 0.0f);
OlgaHoeyer 0:21e9cc38dd31 273 lamp.flash(2.0f, 0.5f); // On for 50% of every 2 seconds.
OlgaHoeyer 0:21e9cc38dd31 274 break;
OlgaHoeyer 0:21e9cc38dd31 275 case blue:
OlgaHoeyer 0:21e9cc38dd31 276 lamp.set(0.0f, 0.0f, 1.0f);
OlgaHoeyer 0:21e9cc38dd31 277 lamp.flash(2.0f, 0.5f); // On for 50% of every 2 seconds.
OlgaHoeyer 0:21e9cc38dd31 278 break;
OlgaHoeyer 0:21e9cc38dd31 279 default: // Error has occured, blue. Could just have defaulted instead
OlgaHoeyer 0:21e9cc38dd31 280 lamp.set(0.0f, 0.0f, 1.0f); // of case blue, however I think
OlgaHoeyer 0:21e9cc38dd31 281 lamp.flash(2.0f, 0.5f); // this is more understandable.
OlgaHoeyer 0:21e9cc38dd31 282 }
OlgaHoeyer 0:21e9cc38dd31 283
OlgaHoeyer 0:21e9cc38dd31 284 return;
OlgaHoeyer 0:21e9cc38dd31 285 }
OlgaHoeyer 0:21e9cc38dd31 286
OlgaHoeyer 0:21e9cc38dd31 287
OlgaHoeyer 0:21e9cc38dd31 288
OlgaHoeyer 0:21e9cc38dd31 289