-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoTachometer.ino
99 lines (84 loc) · 2.84 KB
/
ArduinoTachometer.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
int previousSensorValue = -1;
bool dataCollectionState = false;
bool endDataState = false;
unsigned long dataStartTime = 0;
const unsigned long dataCollectionTime = 60000; // 60000 milliseconds == 60 seconds
const int ANALOG_HIGH = 1023;
const int ANALOG_LIGHT_SENSOR_OUTPUT_PIN = 0;
const int ANALOG_BUTTON_OUT_PIN = 1;
const int TOLERANCE = 1;
/************************************* Main Body **************************************************************/
void setup() {
initializePins();
//Begin sending output at 9600 baud
Serial.begin(9600);
}
void loop() {
// Code here will check for for the start/stop collecting-data event and
// set the power to the LED transistor appropriately (for now, the default is always on)
if ((analogRead(ANALOG_BUTTON_OUT_PIN) == ANALOG_HIGH) && !dataCollectionState) {
startDataCollectionTimer();
sendCSVHeader();
dataCollectionState = true;
}
// If we have started collecting data and haven't finished
if ((dataCollectionState) && (!endDataState)) {
//Read output from Light Sensor and write to serial port
previousSensorValue = printSensorValueWithinTolerance(analogRead(ANALOG_LIGHT_SENSOR_OUTPUT_PIN));
// check timer
if (isTestComplete()) {
setEndState();
}
}
delay(10);
}
/************************************* End Main Body **************************************************************/
// Sets the Arduino pins for input
void initializePins() {
pinMode(ANALOG_BUTTON_OUT_PIN, INPUT);
pinMode(ANALOG_LIGHT_SENSOR_OUTPUT_PIN, INPUT);
}
//Prints header for CSV file
void sendCSVHeader() {
Serial.println("Voltage,Time Of Measurement");
}
// Records start time for data collection
void startDataCollectionTimer() {
dataStartTime = millis();
}
//Checks is current time is beyond the start time by the amount dataCollectionTime
bool isTestComplete() {
return (millis() > dataStartTime + dataCollectionTime);
}
// Marks the end of the file and sets the end-of-data-collection state flag to true
bool setEndState() {
endDataState = true;
Serial.println("EOF");
return endDataState;
}
// Prints the current sensor value along with a timestamp in milliseconds
void printSensorValue(int sensorValue) {
Serial.print(sensorValue);
Serial.print(",");
Serial.println(millis());
}
// switches data collection state between off and on
void setLightSensor(bool powerState) {
if (powerState) {
if (!endDataState) {
dataCollectionState = true;
}
} else {
dataCollectionState = false;
}
}
// Prints the sensor value if it is outside the window of tolerance
int printSensorValueWithinTolerance(int sensorValue) {
// If the value differs from the previous value by the amount TOLERANCE, print the new value
if ((abs(sensorValue - previousSensorValue)) > TOLERANCE) {
printSensorValue(sensorValue);
} else {
delay(1);
}
return sensorValue;
}