-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalogReadSD.ino
83 lines (76 loc) · 2.66 KB
/
AnalogReadSD.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
#include <SD.h> //has functions for writing data to the SD card
//Attritibutes for controlling data collection of the program
const int analogStartPin=A0;//lowest analog pin for collection
const int numberOfPins=1;//number of pins to collect starting at start pin, (includes start pin)
const int timeBetweenReads=100-15;//miliseconds between reads
const int collectionMultiplier=1;//multiplier if voltage division resistors are used to make it a 5v signal. 1=no division, 2=single division (half voltage)
const int dataCollectionPin=7;//give this pin a 5V signal to collect data
const int ledPin=13;//status LED (13 is internal LED)
//(c) Eric Johnson 2015
//536ejohnson@gmail.com
//MIT Licence
//slightly based off https://www.arduino.cc/en/Tutorial/Datalogger
void setup(){
pinMode(ledPin,OUTPUT);
pinMode(dataCollectionPin,INPUT);
Serial.begin(9600);//set up serial so computer can see live data
while(!SD.begin(4)){//attempt to start SD Card
Serial.println("SD failed");
for(int i=0; i<100;i++){//blink LED if failed
digitalWrite(ledPin,HIGH);
delay(50);
digitalWrite(ledPin,LOW);
delay(50);
}
}
digitalWrite(ledPin,LOW);//
Serial.println("SD started");
}
void loop(){
//write header to file
writeStringLineSD("Time,Analog Datas");
while(true){//always loop
Serial.println("loop dely");
delay(timeBetweenReads);
if(digitalRead(dataCollectionPin)==HIGH){//collect data
Serial.println("collect data");
digitalWrite(ledPin,HIGH);
String data="";
//add time to string
data+=String(millis());
data+=",";
for(int i=analogStartPin; i<analogStartPin+numberOfPins;i++){//for each pin we want to read
long aValue=analogRead(i);//10 bit value
aValue=aValue*5000/1023;//scale to 5000 millivolts
aValue=aValue*collectionMultiplier;//account for voltage division
data+=String(aValue);
data+=",";
}
writeStringLineSD(data);//write data to disk.
}
else //do not read data
{
Serial.println("not collect data");
digitalWrite(ledPin,LOW);
delay(250);
}
}
}
void writeStringLineSD(String s){
File dataFile = SD.open("datalog.txt", FILE_WRITE);//should append
if (dataFile) {// if the file is available, write to it:
dataFile.println(s);
dataFile.close();
// print to the serial port too:
Serial.println("datra to disk "+s);
}
else {// if the file isn't open, pop up an error:
Serial.println("error opening datalog.txt");
while(true){//blink LED forever until reset
digitalWrite(ledPin,HIGH);
delay(50);
digitalWrite(ledPin,LOW);
delay(50);
}
}
}