-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_Arduino_5.ino
71 lines (53 loc) · 1.71 KB
/
3_Arduino_5.ino
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
int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorPin2 = A2;
int sensorPin3 = A3;
int sensorValue0 = 0;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
void setup() {
Serial.begin(9600); //sets serial port for communication
}
void loop()
{
float sensorAverage = 0.0;
float sensorVariance = 0.0;
// read the value from the sensor:
sensorValue0 = analogRead(sensorPin0);
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
//put them in an array
int sensor_array [4] = {sensorValue0,sensorValue1,sensorValue2,sensorValue3};
//calculate the Average of sensor values
sensorAverage = (sensor_array[0]+sensor_array[1]+sensor_array[2]+sensor_array[3])/4;
//calculate the variance of sensor values
float diff_sensor0 = sensor_array[0] - sensorAverage;
float diff_sensor1 = sensor_array[1] - sensorAverage;
float diff_sensor2 = sensor_array[2] - sensorAverage;
float diff_sensor3 = sensor_array[3] - sensorAverage;
sensorVariance = (sq(diff_sensor0)
+ sq(diff_sensor1)
+ sq(diff_sensor2)
+ sq(diff_sensor3))/4;
//Print the results
Serial.print("Sensor 0 = ");
Serial.print(sensorValue0); //prints the values coming from the sensor on the screen
Serial.print("\t");
Serial.print("Sensor 1 = ");
Serial.print(sensorValue1);
Serial.print("\t");
Serial.print("Sensor 2 = ");
Serial.print(sensorValue2);
Serial.print("\t");
Serial.print("Sensor 3 = ");
Serial.print(sensorValue3);
Serial.print("\t");
Serial.print("Average = ");
Serial.print(sensorAverage);
Serial.print("\t");
Serial.print("Variance = ");
Serial.println(sensorVariance);
delay(5000);
}