Hello everyone.
I'm sorry for the dumb question, but I tried many ways to solve this problem without success.
I'm building a shield to acquisition of bioelectric signals, which will be connected to the person during the day, storing the information (ECG, for example) in a SD card, then be plotted using a Python script.
the user must press a button to start recording, and the same button if you want to interrupt it. The system works perfectly, but only by Serial Monitor (USB). When Arduino is powered by a external source and I try to record, nothing happens.
I know it must be something simple, but did not find any topic or tutorial to solve my problem.
Thanks.
Here is the code:
// Datalogging ECG
#include <SD.h>
#include <SPI.h>
//SPI Communication with UNO
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
int CS_pin = 4;
int pino_ecg = 1; // from conditioning circuit
float freq_amostragem = 1000; // sampling frequency (Hz)
long id = 1; //sample
// pushbutton debouncing
int buttonPin = 5;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean rec_On = LOW;
void setup()
{
Serial.begin(9600);
pinMode(CS_pin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(pino_ecg, INPUT);
//Initializing Card
Serial.println("\nInicializando cartao SD...\n");
if (!SD.begin(CS_pin))
{
Serial.println("Erro na inicializacao do cartao.\n");
return;
}
Serial.println("Inicializando realizada com sucesso.\n");
// Acredito que não vá precisar de cabeçalho no arquivo texto:
//File header
File logFile = SD.open("ECG_log.txt", FILE_WRITE);
if (logFile)
{
logFile.println(" "); //Blank Line
String header = "id, tensao";
logFile.println("ID , tensao");
logFile.close();
Serial.println("ID , tensao");
}
else
{
Serial.println("Nao foi possivel abrir o arquivo ECG_log\n");
return;
}
}
// Define debouncing function
boolean debounce(boolean last){
boolean current = digitalRead(buttonPin);
if(last != current){
delay(5);
current = digitalRead(buttonPin);
}
return current;
}
void loop()
{
//Check ECG signal
pino_ecg = analogRead(pino_ecg);
//Creating a string with sample number and ECG voltage
String stringECG = String(id) + " " + String(pino_ecg);
//Button State
currentButton = debounce(lastButton);
if(lastButton == LOW && currentButton == HIGH){
rec_On = !rec_On;
}
lastButton = currentButton;
.
if(rec_On == HIGH){
//Opening the File on SD
File logFile = SD.open("ECG_log.txt", FILE_WRITE);
if (logFile)
{
logFile.println(stringECG);
logFile.close();
Serial.println(stringECG);
}
else
{
Serial.println("Nao foi possivel gravar no arquivo ECG_log\n");
}
// increasing ID number
id += 1;
//Convertendo a frequencia de amostragem em periodo de amostragem [ms]
float periodo_amostragem = (1 / freq_amostragem)*1000;
delay(periodo_amostragem);
}
}