Hi all,
I originally posted for help in the project guidance forums, but I thought I would also post in here to see if you all had any ideas. I'm working on a project where I am controlling the pneumatic actuation of a cylinder using an Arduino. One of the things I would like to be able to do is read in stroke values of the pneumatic cylinder using a linear potentiometer. I have successfully gotten the arduino to control the pneumatic actuation of the cylinder, but when I try to datalog to the SD card, my program lags a lot and no information is written to the SD card. For reference I am using the Adafruit SD car shield with a 16 gb SDHC card.
To debug my code I modified the example SD logger code to read in my linear potentiometer and output the values to an lcd screen. When I run this code no data is written to the SD card. Now if I comment out the code for printing values to the lcd screen data is written to the SD card. Any Idea why this would happen?
Test Code:
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SD.h>
#include "RTClib.h"
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;
int linearpotA =A0;
int MasterCylinderTravel = 0;
File logfile;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// LCD initialization
lcd.begin(20,4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Count: ");
lcd.setCursor(0,1);
lcd.print("MC Travel: in");
//SD card stuff
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(53, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(10,11,12,13)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
}
void loop()
{
// make a string for assembling the data to log:
// read three sensors and append to the string:
//
int sensorValue = analogRead(linearpotA);
float voltage = sensorValue* (5.0 / 1023.3);
float MasterCylinderTravel = 0.4117*voltage;
lcd.setCursor (10,1);
lcd.print(MasterCylinderTravel);
// print to the serial port too:
Serial.println(MasterCylinderTravel);
// The following line will 'save' the file to the SD card after every
// line of data - this will use more power and slow down how much data
// you can read but it's safer!
// If you want to speed up the system, remove the call to flush() and it
// will save the file only every 512 bytes - every time a sector on the
// SD card is filled with data.
if (logfile){
logfile.println(MasterCylinderTravel);
logfile.flush();
}
else{
Serial.println("error opening logfile.csv");
}
// Take 1 measurement every 500 milliseconds
// delay(1000);
}