I don't know why but since yesterday, my Arduino sketch returns weird data. I've check eaches inputs and the returns values are ok.
But that line:
sprintf(read, "%d;%d;%d;%d;%d;%d;%d;%d\r", x, analogRead(HD), analogRead(HG), analogRead(CD), analogRead(CG), 0, 0, analogRead(ECG));
returns something like this:
3000;0;110;291;263;230;0;0 (It's like the analog reads are shift from 1 position to the right)
It's suppose to return something like this:
3000;110;291;263;230;0;0;245
Here is my sketch:
int HD = A0;
int HG = A1;
int CD = A2;
int CG = A3;
int ECG = A5;
volatile boolean interruptFlag = 0;
char read[45];
char signal[2048];
volatile int count = 0;
volatile long x = 0;
void setup() {
Serial.begin(9600);
noInterrupts(); // Stop interrupts.
TCCR1A = 0; // Set entire TCCR1A register to 0.
TCCR1B = 0; // Same for TCCR1B.
TCNT1 = 0; // Initialize counter value to 0.
// Set compare match register for 50Hz increments.
OCR1A = 39999; // = 16000000 / (8 * 50) - 1 (must be <65536)
// Turn on CTC mode.
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 64 prescaler.
TCCR1B |= (0 << CS12) | (1 << CS11) | (1 << CS10);
// Enable timer compare interrupt.
TIMSK1 |= (1 << OCIE1A);
interrupts(); // Allow interrupts.
}
ISR(TIMER1_COMPA_vect){
if (!interruptFlag){
interruptFlag = 1;
}
}
void loop() {
if (interruptFlag){
writeAnalogSignals();
count++;
interruptFlag = 0;
}
}
void writeAnalogSignals() {
if (strlen(read) == 0) {
sprintf(read, "%d;%d;%d;%d;%d;%d;%d;%d\r", x, analogRead(HD), analogRead(HG), analogRead(CD), analogRead(CG), 0, 0, analogRead(ECG));
} else {
sprintf(read, "|%d;%d;%d;%d;%d;%d;%d;%d\r", x, analogRead(HD), analogRead(HG), analogRead(CD), analogRead(CG), 0, 0, analogRead(ECG));
}
strcat(signal, read);
x += 2;
if (count >= 50) {
Serial.print(signal);
signal[0] = 0;
count = 0;
}
}
x
is 4 bytes butsprintf
expects it to be 2 bytes because you used%d
in the format string. If you want to keepx
as along
, then replace the first%d
with%ld
and that should fix it; otherwise declarx
asint
. Please accept @jot answer if that fixes your problem.