Hi you all,
I am working on a project where I have a two way switch, two sensors and a SD card reader. When I flip the switch one way the data of one of sensor 1 is saved to a .csv file on the SD card. When I flip the switch the other way the data of sensor 2 is saved on a different .csv file on the SD card. So far this works but I would like to add one thing:
When I start a measurement series of sensor 1 and then I stop for a while and later take a second series of measurements of sensor 1 then I would like to start on a second column in the .csv file.
I tried to store the data in rows but that didn't work since then the amount of measurements is limited. That is why I store to data in columns.
This is my code so far.
Thank you for your help.
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
#include <SPI.h>
#include <SD.h>
#include <FastLED.h>
const int chipSelect = 4;
// Two way switch
int Switch = A0;
int NormalSensor = A1;
int TorsionSensor = A2;
// define the LED amount and the pin to which it is connected
#define LED_PIN 6
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
void setup() {
// 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
}
// Set up the pins
pinMode(Switch, INPUT);
pinMode(NormalSensor, INPUT);
pinMode(TorsionSensor, INPUT);
// LED set up
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
// Check if SD card is present
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) {
leds[0] = CRGB(255, 0, 0);
FastLED.show();
delay (100);
leds[0] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
}
}
Serial.println("card initialized.");
}
void loop() {
// Check state of the switch and then set LED color and write sensor data to the SD card file.
while (analogRead(Switch) < 100) {
File dataFile = SD.open("Normal.csv", FILE_WRITE);
leds[0] = CRGB(125, 125, 125);
FastLED.show();
Serial.println(analogRead(NormalSensor));
if (dataFile) {
dataFile.println(analogRead(NormalSensor));
dataFile.close();
}
}
// Check state of the switch and then set LED color and write sensor data to the SD card file.
while (analogRead(Switch) < 900 && analogRead(Switch) > 100) {
File dataFile = SD.open("Torsion.csv", FILE_WRITE);
leds[0] = CRGB(0, 0, 255);
FastLED.show();
Serial.println(analogRead(TorsionSensor));
if (dataFile) {
dataFile.println(analogRead(TorsionSensor));
dataFile.close();
}
}
// If the switch is in the middle position don't do anything just set the color of the status LED different.
while (analogRead(Switch) > 900) {
leds[0] = CRGB(0, 255, 0);
FastLED.show();
}
}
Before making changes can you please explain why you are using analogRead() when the input from the switch is a digital signal, ie either on or off ? The A* inputs can be used as normal GPIO (digital) pins with digitalRead().
Is there anything keeping the switch pin in a known state at all times, such as a pullup or pulldown resistor ?
Or using the even easier method using INPUT_PULLUP in pinMode() to turn on the built in pullup resistor for the pin
That aside, I don't understand what data layout in the files that you are aiming for
The obvious layout would be one row for each series with a new row starting when switching inputs.
You mention stopping "for a while". Does that mean that you don't want any measurements taken for a while even though you have not switched inputs ? If so, what will signal that you have stopped ?
@UKHeliBob I did that at first but then I have a limited amount of data that I can store per row. Per row I would need around 5000 measurements and this didn't work.
@david_2018 I haven't thought of this. In my opinion it doesn't matter too much since I will analyse the data afterwards in python. I will take the data per column and the amount of data points is not too important then I would think
You just need to be able to handle varying numbers of measurements in each measurement period. For instance, if the 2nd measurement period has fewer measurements than the first, then you still need to append an extra comma to the remaining measurements from the 1st measurement period, otherwise a 3rd measurement period will not align properly. If any measurement period has more measurements than all the previous measurement periods, then you need to generate new lines with empty columns for the previous measurement periods to keep the data aligned. This also means your analysis software needs to be able to handle the empty data points.
Don't try to read the entire line in at once, that is likely where you are getting a problem.
A separate file for each measurement period might be easier, just implement a method of sequentially numbering the files, although the one line per measurement period should be achievable. The python code can do the work of combining all the files into a single csv if you really need that.
I am now trying to solve it like this. So I make a counter that makes a new file every new measurement.
if (analogRead(Switch) < 100) {
String normalfile = "Normal" + String(counterNormal) + ".csv";
counterNormal++;
Serial.println(normalfile);
}
// Check state of the switch and then set LED color and write sensor data to the SD card file.
while (analogRead(Switch) < 100) {
File dataFile = SD.open(normalfile, FILE_WRITE);
leds[0] = CRGB(125, 125, 125);
FastLED.show();
Serial.println(analogRead(NormalSensor));
if (dataFile) {
dataFile.println(analogRead(NormalSensor));
dataFile.close();
}
}
Well I want one array with around 5000 values every time I make a measurement. I expect to be doing ±20 measurements per session so in my eyes it would be okay to have 20 separate files.