AmeliaLita
Member
Hi, I am trying to use Teensy 3.5 to write data that I acquire from two sensors (encoder and an EMG). I found that there are previous forum mentioning about this too and I've also tried it. I tried changing the code from the forum. It does made new file in my SD card but the data sensor isn't saved, so basically it's just an empty csv data.
I use the SdFat library.
This is the code
Is there anything wrong with it? How can I make it work? I think there's something wrong with the dologging void but I'm not entirely sure how to work my way around. Please help me, thank you.
I use the SdFat library.
This is the code
Code:
#include <SdFat.h>
#define outputA 5
#define outputB 6
#define RawEMG A0
#define MaxCount 10000
int counter = 0;
int derajat = 0;
int EMG;
int aState;
int aLastState;
SdFatSdio sd;
File file;
void setup()
{
pinMode (outputA, INPUT_PULLUP);
pinMode (outputB, INPUT_PULLUP);
Serial.begin(9600);
//Reads the initial state of the outputA
aLastState = digitalRead(outputA);
if (!sd.begin()) {
sd.initErrorHalt();
}
}
void doLogging(uint32_t timeStamp, int *data1,int *data2, int *data3, int nb)
{
// following is for logging
static uint32_t ifl = 0;
static uint32_t count = 0;
char filename[32];
if(!count)
{
//open file
sprintf(filename,"Data.csv",ifl); ifl++;
if (!file.open(filename, O_RDWR | O_CREAT)) {
sd.errorHalt("open failed");
count=1;
}
}
//
if(count>0)
{
// write to file
file.print(timeStamp);
for(int ii=0; ii<nb; ii++)
{ file.print(',');
file.print(data1[ii]);
file.print(',');
file.print(data2[ii]);
file.print(',');
file.print(data3[ii]);
}
file.println();
count++;
if(count>MaxCount) count=0;
}
//
if(!count)
{
// close file
file.close();
}
}
void loop()
{
uint32_t t0 = millis();
EMG = analogRead(RawEMG); //reads EMG sensor
//float EMG = EMG * (5.0 / 1023.0);
aState = digitalRead(outputA); //reads the 'current' state of the outputA
//if outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (aState != aLastState){
//if outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState){
counter ++;
} else {
counter --;
}
if (counter > 2048 or counter < -2048) //omron sebelumnya 1200 yang 600ppr, resolusi x 2
{
counter = 0;
}
derajat = counter * 360/2048;
}
aLastState = aState;
Serial.print(counter);
Serial.print(",");
Serial.print(derajat);
Serial.print(",");
Serial.println(EMG);
doLogging(t0,&counter, &derajat, &EMG,1); //insert data (?)
}
Is there anything wrong with it? How can I make it work? I think there's something wrong with the dologging void but I'm not entirely sure how to work my way around. Please help me, thank you.