-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
102 lines (78 loc) · 2.05 KB
/
main.cpp
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
#include "WProgram.h"
#include "SoftwareSerial.h"
// prototypes
void blink(int n, int d);
// variables
int ledPin = 13; // LED connected to digital pin 13
// analog pins
int photoPin = 0;
int accxPin = 3;
int accyPin = 2;
int acczPin = 1;
long timer = 0;
int sampleRate = 1000;
int initPause = 1; // the open logger needs to initialize for 2 seconds
int initTimer = 3000;
void setup(){
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.println("----------------------------");
Serial.println("Telling Stories with Sensors");
Serial.println("ITP Spring 2010 | OpenLogger");
Serial.println("----------------------------");
pinMode(ledPin, OUTPUT); // sets the digital pin as output
// don't think it's necessary but set the analog pins
pinMode(photoPin, INPUT);
pinMode(accxPin, INPUT);
pinMode(accyPin, INPUT);
pinMode(acczPin, INPUT);
}
void loop(){
// have to wait for about two seconds before it's ready to log
if (initPause) {
if(millis() - timer >= initTimer) {
// print the headers
Serial.println("time,accx,accy,accz,light");
initPause = 0;
}
} else {
if(millis() - timer >= sampleRate) {
// store the current time
timer = millis();
long timestamp = timer;
// read the sensors, then construct the string for output
int photo = analogRead(photoPin);
int accx = analogRead(accxPin);
int accy = analogRead(accyPin);
int accz = analogRead(acczPin);
// to fast track this, just use the print methods.
Serial.print(timestamp);
Serial.print(',');
Serial.print(accx);
Serial.print(',');
Serial.print(accy);
Serial.print(',');
Serial.print(accz);
Serial.print(',');
Serial.println(photo);
// then blink the led as comfirmation of the reading
//blink(1, 100); // blink led 2 times, 500 ms interval
}
}
}
void blink(int n, int d){
for (int i=0;i<n;i++)
{
digitalWrite(ledPin, HIGH);
delay(d/2);
digitalWrite(ledPin, LOW);
delay(d/2);
}
}
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}