Skip to content

Commit 98af2ec

Browse files
committed
Create esploraDatalogger.ino
1 parent c4f99ff commit 98af2ec

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed
+222
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*
2+
Arduino Esplora educational data logger and data viewer.
3+
Logs data into a microSD and "plot" it into the LCD.
4+
5+
Written by Angelo Scialabba
6+
7+
11/04/2013
8+
*/
9+
#include <Esplora.h>
10+
#include <Adafruit_GFX.h> // Core graphics library
11+
#include <Adafruit_ST7735.h> // Hardware-specific library
12+
#include <TFT.h> // Arduino LCD library
13+
#include <SPI.h>
14+
#include <SD.h>
15+
16+
#define SD_CS 8
17+
#define MENU 0
18+
#define RECORD 1
19+
#define DISPLAY 2
20+
#define GRAPH 3
21+
22+
int st;
23+
int STORE_DELAY = 1000;
24+
void setup(){
25+
26+
// initialize the screen
27+
EsploraTFT.begin();
28+
29+
// clear the screen
30+
EsploraTFT.background(0,0,0);
31+
st = MENU; //Select menu for default option
32+
33+
// ---------Splash Screen---------
34+
EsploraTFT.stroke(0,255,0);
35+
EsploraTFT.setTextSize(4);
36+
EsploraTFT.text("Esplora",0,25);
37+
EsploraTFT.stroke(255,255,255);
38+
EsploraTFT.setTextSize(1);
39+
EsploraTFT.text("Educational datalogger",0,60);
40+
delay(3000);
41+
//-----------------------------
42+
}
43+
44+
void loop(){
45+
//Read the option selected execute the right function
46+
switch(st){
47+
case MENU:
48+
menu(); // Main menu
49+
break;
50+
case RECORD:
51+
record(); // Record data menu
52+
break;
53+
case DISPLAY:
54+
display(); // Display the sensor data
55+
break;
56+
case GRAPH:
57+
graph(); // Display the graph of stored data
58+
break;
59+
60+
}
61+
62+
63+
}
64+
65+
void menu(){
66+
67+
EsploraTFT.background(0,0,0);
68+
EsploraTFT.stroke(255,255,255);
69+
EsploraTFT.setTextSize(1);
70+
71+
EsploraTFT.text("Select an option:\n1.Record data\n2.Diplay data\n3.Graph\n",0,25);
72+
delay(500);
73+
while(1) // infinite loop until a button is pressed
74+
{
75+
if (Esplora.readButton(SWITCH_1) == LOW) {
76+
st = RECORD; // set new option
77+
return; // the "while" will break and the loop function will execute the new option
78+
} else if (Esplora.readButton(SWITCH_2) == LOW) {
79+
st = DISPLAY; // same here
80+
return;
81+
} else if (Esplora.readButton(SWITCH_3) == LOW) {
82+
st = GRAPH; // same here
83+
return;
84+
}
85+
}
86+
}
87+
88+
void record() {
89+
90+
int value;
91+
File dataFile;
92+
SD.begin(SD_CS); // init the SD library
93+
SD.remove("data.txt"); // delete old data file
94+
dataFile = SD.open("data.txt", FILE_WRITE);
95+
String data;
96+
EsploraTFT.background(0,0,0); //clear the screen
97+
if (!dataFile) {
98+
EsploraTFT.stroke(255,255,255); //+
99+
EsploraTFT.setTextSize(2); //| Show error Message
100+
EsploraTFT.text("SD Error\n",0,0); //|
101+
EsploraTFT.setTextSize(1); //|
102+
EsploraTFT.text("Plug in your SD\nand restart Esplora.",0,30); //+
103+
while(1){
104+
}
105+
}
106+
EsploraTFT.stroke(255,255,255);
107+
EsploraTFT.setTextSize(2);
108+
EsploraTFT.text("Recording\n",0,0);
109+
EsploraTFT.setTextSize(1);
110+
EsploraTFT.text("Hold Switch 1 to stop.",0,30); // Start recording
111+
delay(500);
112+
while(1){ // infinite loop, will break if button 1 is pressed
113+
if (Esplora.readButton(SWITCH_1) == LOW) {
114+
dataFile.close(); // Close file
115+
st = MENU; //and return to main menu
116+
return;
117+
}
118+
data = ""; //+
119+
value = Esplora.readLightSensor(); //| Read Data from sensor - Change Esplora.readLightSensor()
120+
data += String(value); //| and store it - to change the input sensor
121+
data+= "\r\n"; //| in the sd card
122+
dataFile.print(data); //|
123+
delay(STORE_DELAY); //+
124+
}
125+
126+
}
127+
128+
void display()
129+
{
130+
int value;
131+
char tmp[5];
132+
String tmp_string;
133+
EsploraTFT.setTextSize(1);
134+
EsploraTFT.background(0,0,0); //Clear the screen
135+
EsploraTFT.text("Hold Switch 1 to stop.",0,30);
136+
while(1) {
137+
tmp_string=String(Esplora.readLightSensor()); //Read the sensor value
138+
tmp_string.toCharArray(tmp, 5); //convert the value into a string
139+
EsploraTFT.stroke(255,255,255);
140+
EsploraTFT.setTextSize(2);
141+
EsploraTFT.text(tmp,0,0); //write the string on the lcd
142+
delay(500);
143+
EsploraTFT.stroke(0,0,0);
144+
EsploraTFT.text(tmp,0,0); //erase the old value from the screen
145+
if (Esplora.readButton(SWITCH_1) == LOW) {
146+
st = MENU; //return to main menu when button 1 is pressed
147+
return;
148+
}
149+
}
150+
151+
}
152+
153+
154+
void graph(){
155+
int value,index,xPos;
156+
unsigned long i,points,div,position_index;
157+
File dataFile;
158+
char tmp[6];
159+
SD.begin(SD_CS);
160+
dataFile = SD.open("data.txt", FILE_READ); //open the data file
161+
String data;
162+
points = 0;
163+
xPos=0;
164+
EsploraTFT.background(0,0,0); //Clear the screen
165+
if (!dataFile) {
166+
EsploraTFT.stroke(255,255,255); //show an error message
167+
EsploraTFT.setTextSize(2); //if the file does not exist
168+
EsploraTFT.text("SD Error\n",0,0); //or the sd card is not present
169+
EsploraTFT.setTextSize(1);
170+
EsploraTFT.text("Plug in your SD\nand restart Esplora.",0,30);
171+
while(1){
172+
}
173+
}
174+
for (i=0;i<dataFile.size();i++){
175+
if (dataFile.read()=='\n') points++; // count how many values the file contains
176+
}
177+
position_index=0;
178+
div = points/EsploraTFT.width();
179+
if (div == 0){ //points available on screen are more that stored ones
180+
div = EsploraTFT.width()/points; //fit the data in the screen and read from file
181+
dataFile.seek(0);
182+
for (i = 0 ; i<points;i++){
183+
for (index = 0; index<6; index++){
184+
tmp[index] = dataFile.read();
185+
if (tmp[index] =='\n'){
186+
tmp[index-1] = '\0';
187+
break;
188+
}
189+
}
190+
value = atoi(tmp); //convert the text into a number
191+
value = map(value,0,1023,0,EsploraTFT.height()); //map the value
192+
EsploraTFT.line(i*div, EsploraTFT.height() - value, i*div, EsploraTFT.height()); //draw the value on lcd
193+
}
194+
} else { //all stored data can't be drawed on screen
195+
dataFile.seek(0);
196+
for (i = 0 ; i<points;i++){
197+
for (index = 0; index<6; index++){
198+
tmp[index] = dataFile.read();
199+
if (tmp[index] =='\n'){
200+
tmp[index-1] = '\0';
201+
position_index++;
202+
break;
203+
}
204+
}
205+
if ((position_index-1) % div == 0){ // select only few points
206+
value = atoi(tmp); // convert
207+
value = map(value,0,1023,0,EsploraTFT.height()); // and print values on LCD
208+
EsploraTFT.line(xPos, EsploraTFT.height() - value, xPos, EsploraTFT.height());
209+
xPos++;
210+
}
211+
}
212+
213+
}
214+
while(1){
215+
if (Esplora.readButton(SWITCH_1) == LOW) {
216+
dataFile.close(); //when button 1 is pressed
217+
st = MENU; //go to main menu
218+
return;
219+
}
220+
}
221+
222+
}

0 commit comments

Comments
 (0)