Hi All
I have a strange problem with my code, I hope you will be able to help me.
First some background about what my code is used for. It´s a very simple pice of code. It reads two analogue signals and send it via serial to a computer. Before it is send, each value will get a C or an M in front of the value, this is used in processing to identify the values which is received.
At one of the analogue inputs I measure the time between two inputs and calculate a beat pr minute. This is done by an interrupt, I just feed the analogue signal to the interrupt, which will trigger the measurement.
I have attached a image which shows how the signal I´m receiving looks like.
My problem is that I don´t receive the BPM and diffMillis output if I run the attached code, but if I disable the output from the two analogue interfaces I receive the BPM and diffMilis
This will work for the BPM and diffMillis:
//Sending BPM and IBI if it´ ready
if (BPMReady) {
Serial.print("B");
Serial.println(BPM);
Serial.print("Q");
Serial.println(diffMillis);
BPMReady = false;
}
/*
//Sends values from the analogue inputs
Serial.print("C");
Serial.println(Slide_1_Value);
Serial.print("M");
Serial.println(Slide_2_Value);
*/
Are there any good explanation why I´m not able to receive the analogue output together with the BPM and diffMillis?
Here is my code:
#define PIN_Slide_1 A0
#define PIN_Slide_2 A1
int i;
int Slide_1_Value;
int Slide_2_Value;
boolean firstBeat, secondBeat, BPMReady;
float firstMillis, secondMillis, diffMillis,BPM;
void setup() {
Serial.begin(38400);
attachInterrupt(0, CalcBPM, RISING);
}
void loop() {
i = 0;
Slide_1_Value = 0;
while (i < 4) {
Slide_1_Value = Slide_1_Value + analogRead(PIN_Slide_1);
i++;
}
Slide_1_Value = Slide_1_Value / 5;
i = 0;
Slide_2_Value = 0;
while (i < 4) {
Slide_2_Value = Slide_2_Value + analogRead(PIN_Slide_2);
i++;
}
Slide_2_Value = Slide_2_Value / 5;
//Sending BPM and IBI if it´ ready
if (BPMReady) {
Serial.print("B");
Serial.println(BPM);
Serial.print("Q");
Serial.println(diffMillis);
BPMReady = false;
}
//Sends values from the analogue inputs
Serial.print("C");
Serial.println(Slide_1_Value);
Serial.print("M");
Serial.println(Slide_2_Value);
}
void CalcBPM() {
firstBeat = true;
if(secondBeat){
secondMillis = millis();
diffMillis = secondMillis - firstMillis;
BPM = 59999 / diffMillis;
if (BPM > 240)
BPM = 0;
BPMReady = true;
secondBeat = false;
firstBeat = false;
}
if(firstBeat){
firstMillis = millis();
firstBeat = false;
secondBeat = true;
}
}