what is your expected data rate?
The following code may be a start
Code:
//
#define SDFAT_BETA 0
#if SDFAT_BETA == 0
#include <SdFat.h>
#else
#include <SdFat-beta.h> // latest SdFat needed for SDIO and (renamed SdFat.h into SdFat-beta.h)
#endif
#define outputA 5
#define outputB 6
#define RawEMG A0
#define MaxCount 10000
uint32_t t0 = 0;
int counter = 0;
int derajat = 0;
int EMG = 0;
int aState;
int aLastState;
#if SDFAT_BETA == 0
SdFatSdio sd;
File file;
#else
SdFs sd;
FsFile file;
#endif
#define SD_CONFIG SdioConfig(FIFO_SDIO)
#define F_SAMP 500
#define PIT_PERIOD (F_BUS/F_SAMP)
uint32_t ADC_ready=0;
uint32_t ADC_overflow=0;
void ADC_input(void)
{
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;
//
ADC_ready=1; //flag to loop that we have new data
}
void ADC_Trigger_isr(void)
{
PIT_TFLG2=1;
static uint16_t busy;
digitalWriteFast(13,!digitalReadFast(13));
if(busy && ADC_ready) { ADC_overflow++; return; }
busy=1;
ADC_input();
busy=0;
}
void ADC_Trigger_init(void)
{ // check with AudioStream if we are responsable for updates
const int prio=10;
// assign local function as ISR
_VectorsRam[IRQ_PIT_CH2 + 16] = ADC_Trigger_isr;
NVIC_SET_PRIORITY(IRQ_PIT_CH2, prio*16);
NVIC_ENABLE_IRQ(IRQ_PIT_CH2);
// turn on PIT clock
SIM_SCGC6 |= SIM_SCGC6_PIT;
// turn on PIT
PIT_MCR = 0x00;
// Timer 0
PIT_LDVAL2 = PIT_PERIOD;
PIT_TCTRL2 = 2; // enable Timer 2 interrupts
PIT_TCTRL2 |= 1; // start Timer 2
}
void setup()
{
pinMode (outputA, INPUT_PULLUP);
pinMode (outputB, INPUT_PULLUP);
Serial.begin(9600);
while(!Serial);
//Reads the initial state of the outputA
aLastState = digitalRead(outputA);
if (!sd.begin(SD_CONFIG)) {
sd.initErrorHalt(&Serial);
}
pinMode(13,OUTPUT);
ADC_Trigger_init();
}
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%03u.csv",(unsigned int)ifl); ifl++;
if (!file.open(filename, O_RDWR | O_CREAT)) sd.errorHalt("open failed");
Serial.println(filename);
// write header to file
file.println("timeStamp,counter,derajat,EMG");
// flag open file
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)
{ Serial.print("Overflow: "); Serial.println(ADC_overflow);
ADC_overflow=0;
// close file
file.close();
}
}
void loop()
{
if(ADC_ready)
{ doLogging(t0,&counter, &derajat, &EMG,1); //insert data (?)
ADC_ready=0;
}
}