Go Down

Topic: Data won't write to SD within loop (Read 403 times) previous topic - next topic

jsolomon1st

I finally got my sensor to read when the linear actuator starts and keep going for a short time after, but I can't get the sensor data to store on the SD card? I verified it was taking the reading by writing serially.

I am using the same script from before I added the linear actuator, and it was writing to the SD fine. any ideas??

AWOL

Can't see your code.

Remember  the  code tags

sterretje

Filenames need to adhere to the 8.3 format, your's doesn't.

OP's code (as is)
Code: [Select]


#include <Wire.h>
#include "AS726X.h"
#include <SPI.h>
#include <SD.h>

AS726X sensor;//Creates the sensor object
byte GAIN = 2;
byte MEASUREMENT_MODE = 0;
byte integrationValue = 200; // Change this between 1 - 255 for longer intergration times and more readings.
const int CHANNEL_0 = 0;
int buttonApin = 9;
const int chipSelect = 8;
float sensordataViolet;
float sensordataBlue;
float sensordataGreen;
float sensordataYellow;
float sensordataOrange;
float sensordataRed;
//unsigned long previousTime1;

const int forwards = 7;

const int backwards = 6;//assign relay INx pin to arduino pin

//int  j = 0;


void setup() {

pinMode(buttonApin, INPUT_PULLUP);
 Serial.begin(115200); 
pinMode(10, OUTPUT);
   // The chipSelect pin you use should also be set to output
   Serial.println("Initializing SD card");
pinMode(chipSelect, OUTPUT);
  // 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:
    return;
  }
  Serial.println("card initialized.");
//SD.begin(chipSelect);
 
Wire.begin();
pinMode(forwards, OUTPUT);//set relay as an output

pinMode(backwards, OUTPUT);//set relay as an output


sensor.begin();
 


}



void loop() {
    int  j = 0;
 
   if (digitalRead(buttonApin) == LOW) // low means on/ pushed. it is when pushed it connects the wire
 
  {
 digitalWrite(forwards, LOW);
 digitalWrite(backwards, HIGH);//Activate the relay  to go out. Note they must be different to move the motor


do {
    sensor.enableBulb();
    sensor.takeMeasurements();
 
    sensordataViolet = sensor.getCalibratedViolet();
   
    sensordataBlue = sensor.getCalibratedBlue();
    sensordataGreen = sensor.getCalibratedGreen();
    sensordataYellow = sensor.getCalibratedYellow();
    sensordataOrange = sensor.getCalibratedOrange();
    sensordataRed = sensor.getCalibratedRed();
   
   
 
 File dataFile = SD.open("lactuator.csv", FILE_WRITE);   
    dataFile.println(" 00000, 450 , 500 , 550 , 570 , 600 , 650 "); // blank line
    //String header = "V, B, G, O, Y, R"; // headers
    //dataFile.print(previousTime1);
      dataFile.print(",");
    dataFile.print(sensordataViolet);
     dataFile.print(",");
    dataFile.print(sensordataBlue);
      dataFile.print(",");
    dataFile.print(sensordataGreen);
      dataFile.print(",");
    dataFile.print(sensordataYellow);
      dataFile.print(",");
    dataFile.print(sensordataOrange);
      dataFile.print(",");
    dataFile.print(sensordataRed);
      dataFile.print(",");
    dataFile.flush();
    dataFile.close();
   
    Serial.println(sensordataViolet);
    Serial.println(sensordataBlue);
    Serial.println(sensordataGreen);
    Serial.println(sensordataYellow);
    Serial.println(sensordataOrange);
    Serial.println(sensordataRed);

 
   //sensor.disableBulb();
    j = j+1;

} while ( j < 10  ); // hopefully this runs throug


   sensor.disableBulb();
 delay(2000); // wait 2 seconds


   //dataFile.close();
   //sensor.disableBulb();
// I might need this in the do loop
 
digitalWrite(forwards, HIGH);
 digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
delay(2000);// wait 2

 digitalWrite(forwards, HIGH);

 digitalWrite(backwards, LOW);//Activate the relay to retract actuator, they must be different to move the motor

  delay(2000);// wait 2 seconds

}

}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

jsolomon1st

@sterretje

Thank you!!! That was the problem My file name was 9 characters long.

Go Up