-
Notifications
You must be signed in to change notification settings - Fork 0
/
Funcs.h
executable file
·123 lines (123 loc) · 4.3 KB
/
Funcs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright 2018, Gaurav Mishra <[email protected]>
*/
class noteTime {
//Class to keep track of code execution time with rollover protection
private:
static unsigned long timeellapsed, timestart;
public:
noteTime(void) {
//Constructor to reset the timers
timeellapsed = timestart = 0UL;
}
void startIt(void) {
//Start the timer
timestart = millis();
}
void stopIt(void) {
//Stop the timer and update time ellapsed
unsigned long curr = millis();
timeellapsed = timeellapsed + ((curr > timestart) ? (curr - timestart) : ((0xFFFFFFFF - timestart) + curr));
}
void resetIt(void) {
//Reset the timers
startIt();
timeellapsed = 0UL;
}
void addToIt(unsigned long a) {
//Add arbitrary delay from sleep
timeellapsed = timeellapsed + a;
}
unsigned long getEllapsed(void) {
//Get the time ellapsed
return timeellapsed;
}
};
unsigned long noteTime::timeellapsed = 0UL;
unsigned long noteTime::timestart = 0UL;
double x = 0;
double ppmMG(float volts)
{ //Convert the voltage readings to PPM for MG811
volts = volts * 1000; //Volts to milliVolts
#define dcGain 5.67 //Gain of the amplifier
x = volts / dcGain; //Converting amplified output to original output
if (x < 264.0) return 10000.0; //If original emf < minimum emf, give maximum output in PPM
if (x > 325.0) return 400.0; //If original emf > greater than emf, give minimum output in PPM
return pow(10.0, (-16.439 * log10(x)) + 43.84); //Return PPM from emf
}
double ppmTGS(float volts)
{ //Convert the voltage readings to PPM for TGS2600
#define Vc 5.0 //VCC across sensor
#define Rl 10000.0 //Load between sensor and GND (10*10^3)
//#define ro 20771.12438 //Resistance in fresh air
#define ro 19400.0
double rs = ((Vc * Rl) / volts) - Rl; //Resistance of sensor
x = rs / ro; //RS/RO ratio
if (x > 1) return 0.0; //If ratio greater than fresh air, return minimum PPB
if (x < 0.69) return 100.0; //If ratio less than datasheet, return maximum PPB
return pow(10.0, -10.012 * log10(x) + 0.5391); //Return PPB from ratio
}
double getVolts(byte pin)
{ //Reading analog input and converting to voltage for 'Samples' size of samples
//Code from http://www.elcojacobs.com/eleminating-noise-from-sensor-readings-on-arduino-with-digital-filtering/
// read multiple values and sort them to take the mode
unsigned int sortedValues[200];
for (int i = 0; i < 200; i++) {
unsigned int value = analogRead(pin);
byte j;
if (value < sortedValues[0] || i == 0) {
j = 0; //insert at first position
}
else {
for (j = 1; j < i; j++) {
if (sortedValues[j - 1] <= value && sortedValues[j] >= value) {
// j is insert position
break;
}
}
}
for (byte k = i; k > j; k--) {
// move all values higher than current reading up one position
sortedValues[k] = sortedValues[k - 1];
}
sortedValues[j] = value; //insert current reading
delay(50);
}
//return scaled mode of 10 values
double retval = 0;
for (byte i = 95; i < 105; i++) {
retval += sortedValues[i];
}
return ((retval / 10.0) * 5.0 / 1024.0);
}
void flushSerial() {
//Empty the serial
while (Serial.available())
Serial.read();
}
void toggleGSM(byte pin) {
//Turning ON/OFF the GSM/GPRS
digitalWrite(pin, LOW);
delay(3000); //Holding the pin low for 3 seconds
digitalWrite(pin, HIGH);
}
const char specials[] = "{\":,}"; ///* String containing chars to be encoded */
static char hex_digit(char c)
{
return "0123456789ABCDEF"[c & 0x0F];
}
char* urlencode(char* dst, char* src)
{ //Encode strings for HTTP GET
char *d = dst;
char c;
while (c = *src++)
{ if (strchr(specials, c))
{ *d++ = '%';
*d++ = hex_digit(c >> 4);
*d++ = hex_digit(c);
}
else *d++ = c;
}
*d = '\0';
return dst;
}