-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data-Collection.ino
126 lines (103 loc) · 4.34 KB
/
Data-Collection.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
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
124
125
126
const int buttonPin = 2;
const int LowFuelSwitch = 3;
const int ledPin = 13;
int buttonState = 0;
int lastButtonState = HIGH;
int switchState = 0;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000;
bool reading = false;
int UpperRheostatLowest, UpperRheostatHighest, MainRheostatLowest, MainRheostatHighest;
// Constants for fixed resistors in the voltage divider
const float UpperRheostatFixedResistor = 33.0; // Ohms
const float MainRheostatFixedResistor = 22.0; // Ohms
const float ReferenceVoltage = 4.96; // Nano Every +5V output is 4.96 volts
// Set up averaging for reading the ADC pins
const int numSamples = 50; // Number of samples to average. Adjust as desired, noting more samples takes longer.
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(LowFuelSwitch, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
switchState = digitalRead(LowFuelSwitch);
int rawA0;
int rawA1;
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
// Remove line below if not using ANSI terminal for serial monitor
Serial.print("\033[2J\033[H"); // Clear screen and move cursor to home position in PuTTY
Serial.println("Button pressed - Collecting samples for 10 seconds");
digitalWrite(ledPin, HIGH);
startMillis = millis();
reading = true;
UpperRheostatLowest = 1023;
UpperRheostatHighest = 0;
MainRheostatLowest = 1023;
MainRheostatHighest = 0;
}
delay(50);
}
lastButtonState = buttonState;
if (reading) {
currentMillis = millis();
if (currentMillis - startMillis <= period) {
long sumA0 = 0;
long sumA1 = 0;
int rawA0Lowest = 1023;
int rawA0Highest = 0;
int rawA1Lowest = 1023;
int rawA1Highest = 0;
for (int i = 0; i < numSamples; i++) {
int rawA0 = analogRead(A0);
int rawA1 = analogRead(A1);
sumA0 += rawA0;
sumA1 += rawA1;
if (rawA0 < rawA0Lowest) rawA0Lowest = rawA0;
if (rawA0 > rawA0Highest) rawA0Highest = rawA0;
if (rawA1 < rawA1Lowest) rawA1Lowest = rawA1;
if (rawA1 > rawA1Highest) rawA1Highest = rawA1;
delay(10); // Small delay between readings
}
// Calculate the averages
float averageA0 = (float)sumA0 / numSamples;
float averageA1 = (float)sumA1 / numSamples;
// Update the lowest and highest if the current average is a new low or high
if (averageA0 < MainRheostatLowest) MainRheostatLowest = averageA0;
if (averageA0 > MainRheostatHighest) MainRheostatHighest = averageA0;
if (averageA1 < UpperRheostatLowest) UpperRheostatLowest = averageA1;
if (averageA1 > UpperRheostatHighest) UpperRheostatHighest = averageA1;
} else {
Serial.println();
Serial.println("-------------------------------------------------");
Serial.println("New Data");
Serial.println();
Serial.print("Upper Rheostat Lowest: ");
Serial.println(UpperRheostatLowest * (ReferenceVoltage / 1023.0));
Serial.print("Upper Rheostat Highest: ");
Serial.println(UpperRheostatHighest * (ReferenceVoltage / 1023.0));
// Calculate and print the derived resistance of the Upper rheostat
float upperRheostatResistance = UpperRheostatLowest * (ReferenceVoltage / 1023.0) / (ReferenceVoltage - UpperRheostatLowest * (ReferenceVoltage / 1023.0)) * UpperRheostatFixedResistor;
Serial.print("Derived Upper Rheostat Resistance: ");
Serial.println(upperRheostatResistance);
Serial.print("Main Rheostat Lowest: ");
Serial.println(MainRheostatLowest * (ReferenceVoltage / 1023.0));
Serial.print("Main Rheostat Highest: ");
Serial.println(MainRheostatHighest * (ReferenceVoltage / 1023.0));
float mainRheostatResistance = MainRheostatLowest * (ReferenceVoltage / 1023.0) / (ReferenceVoltage - MainRheostatLowest * (ReferenceVoltage / 1023.0)) * MainRheostatFixedResistor;
Serial.print("Derived Main Rheostat Resistance: ");
Serial.println(mainRheostatResistance);
Serial.print("Low Fuel Switch State: ");
if (switchState == LOW) {
Serial.println("OPEN");
} else {
Serial.println("CLOSED");
}
reading = false;
digitalWrite(ledPin, LOW);
}
}
}