Activity Datalogger

Hi All,

new starter with the Arduino, bought a course online and got me to a point where i am starting to understand what i am doing a little, but inexperience has got the better of me.

i have made basic circuits as a start of what i plan on making,:
1 of test start switch (digital input) latching switch
1 of valve operation switch (digital input) (microswitch non latching)
1 of potentiometer (to be replaced with pressure transducer) (analog input)
1 green LED to show power and ready to test
1 white LED to show that the test is recording.

i have got a SEEED sd card shield which i am trying to use to log the data from the valve operation switch and the potentiometer, i can see the values on the serial monitor but the data is not being saved to the SD card, i have manually added the test.txt file to the SD card already but it is not writing to it.

would you be able to help me sort the code out to save the info to the sd card please? also would you be able to help me make the sd card look for the test.txt file and if it does not exist create one? if it is possible, would you also be able to help me if it is possible to create a new file if the test.txt already exists? so would make test1.txt, test2.txt etc.

also i will not be using my screen, but is it possible to make the arduino show a green or red light to show if it is logging the data to sd or not? i have RGB leds and also red and green so can be done as 1 led or 2 if not.

i am going to be replacing the SD card shield with a adafruit datalogging shield that has RTC and the SD combined with some prototyping space.

would really appreciate the help.

thanks in advance!

#include <SPI.h>
#include <SD.h>
File myFile;
//Above included in sketch2_SD
const int On_off_SW = 2;
//on off switch input on pin 2
const int Valve_SW = 3;
//valve switch on pin 3
const int Ready_LED = 5;
//ready LED on pin 5
const int Recording_LED = 6;
//Recording LED on pin 6
const int Pressure_SW =(A0);
//Pressure Transducer on pin A0
int value;
int SWstate;
int On_off_state;

void setup() {

pinMode(Ready_LED, OUTPUT);
//Ready LED on pin 5 is output
pinMode(Recording_LED, OUTPUT);
// Recording LED on pin 6 is output
pinMode(On_off_SW, INPUT_PULLUP);
Serial.begin(9600);
//
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
//
Serial.println("Ready to log data");
}

void loop() {
digitalWrite(Ready_LED, HIGH);
//Green LED light will be on to indicate ready

On_off_state = digitalRead(On_off_SW);
// read status of on off switch
SWstate = digitalRead(Valve_SW);
//read status of valve switch

if (On_off_state == HIGH){
digitalWrite(Recording_LED, HIGH);
//
myFile = SD.open("test.txt", FILE_WRITE);
Serial.print("Writing to test.txt...");
myFile.println("Start of Test");
//
value = analogRead(Pressure_SW);
value =map(value, 0, 1023, 0, 255);
Serial.print(value);
Serial.print(",");
Serial.println(SWstate);
myFile.print(value);
myFile.print(",");
myFile.println(SWstate);
delay(250);
  } else {
  digitalWrite(Recording_LED, LOW);
  myFile.println("End of Test");
  myFile.close();
  Serial.println("File Closed");
  Serial.println ("Waiting...");
  delay(5000);
  return;
}
}

Have you proven that the SD card on your unknown Arduino is actually working ?

Try the ReadWrite example in the Arduino IDE, that willshow you if you have problems with the SD or how its wired.

Hello,

yes i have just tried this and it works, i am just going to check the code against mine

Hi
Remove this "return" at the end of the "else".
It doesn't make sense where it is.

void loop() returns nothing. ------>>>>>> (void)

Thank you,

Found out my issue was with not closing the file, is seems to need to close to store the data, ive just set the txt file to open>save reading>close every 250 millis. Seems to be working well. Just need the RTC to date and time the data then i can do a full days worth of monitoring and break up the data as needed on the PC

Check the SD reference;

And study the details of the flush() command ................................