Hello,
My project is an Arduino-based noise meter, this device attempts to capture and process and then visually display amplitude changes of an analog input signal (A0 pin). Saving it to an .txt file using it later to make an excel graph.
What i expected in my .txt file:
yyyy/mm/dd,hours:minutes:seconds,Recorded value
But instead i get this:
yyyy/mm/dd
,hours:minutes:seconds
,Recorded value
I am using an Arduino UNO, an Ethernet Shield W5100, a TinyRTC I2C module and an ky-038 sound sensor
Here is my code:
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
RTC_DS1307 rtc;
const int chipSelect = 4;
const int DO = 2; // Digital PIN 2 do arduino
const int DA = A0; // Analog PIN 0 do Arduino
// Digital PIN do arduino
const int led_01 = 2;
const int led_02 = 3;
const int led_03 = 5;
const int led_04 = 6;
const int led_05 = 7;
const int led_06 = 8;
const int led_07 = 9;
/**
* valores do sensor (Threshold)
*
*/
int sVal_01 = 500; // Initial Threshold
int sVal_02 = 508;
int sVal_03 = 512;
int sVal_04 = 516;
int sVal_05 = 520;
int sVal_06 = 524;
int sVal_07 = 528;
int lastPin = 9; // last digital PIN number
int totalLED = 7; // total LEDs
/**
* defenições adicionais
*/
int debugSwitch = true;
int ledTest = true;
int sensorvalue = 0;
/**
* Setup
*/
void setup() {
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Serial.println("RTC is running!");
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
Serial.begin(9600);
for (int i = 2; i <= lastPin; i++) {
pinMode(i, OUTPUT);
}
if (ledTest == true) {
initTest(); // Iniciar o teste dos leds
}
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
delay(5000);
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 1; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 1) {
dataString += " ";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
DateTime now = rtc.now();
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(" ");
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.println(now.day(), DEC);
dataFile.print(',');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.println(now.second(), DEC);
dataFile.print(',');
dataFile.println(dataString);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
sensorvalue = analogRead(DA); // receber valores do sensor do pino A0
if (debugSwitch == true) {
debug();
} else {
Serial.println("O modo debug está defeneido como falso!");
}
// Lights Animation
warenLights();
}
void debug() {
Serial.print("Valor do sensor: ");
if (sensorvalue >= sVal_01) {
Serial.print(sensorvalue);
Serial.println(" - LED ligado!");
} else {
Serial.println(sensorvalue);
}
}
void initTest() { //iniciação do teste dos leds
digitalWrite(led_01, HIGH);
digitalWrite(led_02, HIGH);
digitalWrite(led_03, HIGH);
digitalWrite(led_04, HIGH);
delay(1000);
digitalWrite(led_05, HIGH);
digitalWrite(led_06, HIGH);
delay(1000);
digitalWrite(led_07, HIGH);
delay(2000);
digitalWrite(led_01, LOW);
digitalWrite(led_02, LOW);
digitalWrite(led_03, LOW);
digitalWrite(led_04, LOW);
delay(500);
digitalWrite(led_05, LOW);
digitalWrite(led_06, LOW);
delay(500);
digitalWrite(led_07, LOW);
}
void warenLights() {
// Basicamente os leds irão ligar comforme o valor defenido
if(sensorvalue >= sVal_01) {
digitalWrite(led_01, HIGH);
} else {
digitalWrite(led_01, LOW);
}
if(sensorvalue >= sVal_02) {
digitalWrite(led_02, HIGH);
} else {
digitalWrite(led_02, LOW);
}
if(sensorvalue >= sVal_03) {
digitalWrite(led_03, HIGH);
} else {
digitalWrite(led_03, LOW);
}
if(sensorvalue >= sVal_04) {
digitalWrite(led_04, HIGH);
} else {
digitalWrite(led_04, LOW);
}
if(sensorvalue >= sVal_05) {
digitalWrite(led_05, HIGH);
} else {
digitalWrite(led_05, LOW);
}
if(sensorvalue >= sVal_06) {
digitalWrite(led_06, HIGH);
} else {
digitalWrite(led_06, LOW);
}
if(sensorvalue >= sVal_07) {
digitalWrite(led_07, HIGH);
} else {
digitalWrite(led_07, LOW);
}
delay(1000);
}
Every constructive comment is appreciated
Part of the code that saves data:
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 1; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 1) {
dataString += " ";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
DateTime now = rtc.now();
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(" ");
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.println(now.day(), DEC);
dataFile.print(',');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.println(now.second(), DEC);
dataFile.print(',');
dataFile.println(dataString);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}