Topic: Is RemoteXY touchy about it's refresh rate ?
Has anyone got any idea why this sketch only allows RemoteXY to communicate if void loop() is remarked out.
With it in the code I can't get RemoteXY to connect at all, I get Error Device not reply.
I can see that it is a timed execution loop, but I want RemoteXY to be able to connect to set filter parameters etc., I can disconnect to see the effect of the changes....
// Arduino Beat Detector By Damian Peckett 2015
// License: Public Domain.
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__HARDSERIAL
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial1
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,3,0,11,0,74,1,8,24,0,
130,1,24,1,53,7,29,4,160,33,
13,60,5,118,26,4,160,33,33,60,
5,1,26,4,160,33,23,60,5,204,
26,129,0,26,2,50,5,14,68,105,
103,105,116,97,108,32,66,101,97,116,
32,68,101,116,101,99,116,111,114,0,
129,0,3,24,15,3,49,66,97,115,
115,70,105,108,116,101,114,0,129,0,
30,24,4,3,31,50,46,48,0,129,
0,61,24,4,3,31,50,46,53,0,
129,0,91,24,4,3,31,51,46,48,
0,129,0,3,34,21,3,49,66,101,
97,116,84,104,114,101,115,104,111,108,
100,0,129,0,30,34,4,3,31,48,
46,48,0,129,0,91,34,6,3,31,
50,48,46,48,0,129,0,60,34,6,
3,31,49,48,46,48,0,129,0,3,
14,15,3,49,65,68,67,99,101,110,
116,114,101,86,111,108,116,115,0,129,
0,30,14,2,3,31,49,46,53,0,
129,0,61,14,4,3,31,50,46,53,
0,129,0,91,14,4,3,31,51,46,
53,0,129,0,3,47,7,3,3,66,
80,77,0,67,6,13,46,12,4,204,
16,4,66,129,35,48,56,4,36,6,
129,0,32,53,5,3,16,49,48,48,
0,129,0,88,53,5,3,16,49,56,
48,0,129,0,60,53,5,3,16,49,
52,48,0,129,0,46,53,5,3,16,
49,50,48,0,129,0,74,53,5,3,
16,49,54,48,0,129,0,3,55,7,
3,3,66,101,97,116,115,0,67,6,
13,54,12,4,204,16,6 };
// this structure defines all the variables of your control interface
struct {
// input variable
int8_t ADC_Centre; // =-100..100 slider position
int8_t Beat_Threshold; // =-100..100 slider position
int8_t Bass_Filter; // =-100..100 slider position
// output variable
char BPM_Text[4]; // string UTF8 end zero
int8_t BPM_Level; // =0..100 level position
char Beat_Count[6]; // string UTF8 end zero
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
// Our Global Sample Rate, 5000hz
#define SAMPLEPERIODUS 200
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
const byte LED = 13;
void setup() {
RemoteXY_Init ();
// // Set ADC to 77khz, max for 10bit
// sbi(ADCSRA,ADPS2);
// cbi(ADCSRA,ADPS1);
// cbi(ADCSRA,ADPS0);
//The pin with the LED
pinMode(LED, OUTPUT);
}
// 20 - 200hz Single Pole Bandpass IIR Filter
float bassFilter(float sample) {
static float xv[3] = {0,0,0}, yv[3] = {0,0,0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = (sample) / 3.f; // change here to values close to 2, to adapt for stronger or weeker sources of line level audio
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]);
return yv[2];
}
// 10hz Single Pole Lowpass IIR Filter
float envelopeFilter(float sample) { //10hz low pass
static float xv[2] = {0,0}, yv[2] = {0,0};
xv[0] = xv[1];
xv[1] = sample / 50.f;
yv[0] = yv[1];
yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);
return yv[1];
}
// 1.7 - 3.0hz Single Pole Bandpass IIR Filter
float beatFilter(float sample) {
static float xv[3] = {0,0,0}, yv[3] = {0,0,0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = sample / 2.7f;
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]);
return yv[2];
}
void loop() {
unsigned long time = micros(); // Used to track rate
float sample, value, envelope, beat, thresh;
unsigned char i;
RemoteXY_Handler ();
for(i = 0;;++i){
// Read ADC and center so +-512
sample = (float)analogRead(0)-503.f;
// Filter only bass component
value = bassFilter(sample);
// Take signal amplitude and filter
if(value < 0)value=-value;
envelope = envelopeFilter(value);
// Every 200 samples (25hz) filter the envelope
if(i == 200) {
// Filter out repeating bass sounds 100 - 180bpm
beat = beatFilter(envelope);
// Threshold it based on potentiometer on AN1
thresh = 0.02f * (float)analogRead(1);
// If we are above threshold, light up LED
if(beat > thresh) digitalWrite(LED, HIGH);
else digitalWrite(LED, LOW);
//Reset sample counter
i = 0;
}
// Consume excess clock cycles, to keep at 5000 hz
for(unsigned long up = time+SAMPLEPERIODUS; time > 20 && time < up; time = micros());
}
}