Ive got some code to figure out audio beats per min from a button press interrupt. I can get the bpm fine into void loop() but my problem is that within loop() I have two while loops depending on bpm values,
while( bpm <= 130 ){
...
}
while(bpm > 130){
...
}
if I change change from above 130 back down below 130 bpm using the interrupt, the code will stop running and seems to stay within the interrupt... wtf? any clues?
heres my code:
#include <TrueRandom.h>
#include <DmxSimple.h>
int fadeab = 0;
long reda = 0;
long redb = 0;
long greena = 0;
long greenb = 0;
long bluea = 0;
long blueb = 0;
int bright = 50;
float fader = 0;
float fadeg = 0;
float fadeb = 0;
int i = 0;
const int buttonPin = 2;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
volatile int bpm = 0;
volatile float t1 = 0;
volatile float t2 = 0;
void setup() {
// DmxSimple.usePin(2);
//pinMode(3, INPUT);
attachInterrupt(0, bpmset, HIGH);
pinMode(buttonPin, INPUT);
DmxSimple.maxChannel(8);
DmxSimple.write(2, 0); //strobe
DmxSimple.write(3, 10); //aux func
DmxSimple.write(4, 100); //aux func
DmxSimple.write(1, 200); //main
Serial.begin(9600);
}
void bpmset(){
if ((millis() - lastDebounceTime) > debounceDelay) {
if(t1 == 0 || millis() - t1 > 5000){
t1 = millis();
Serial.print("t1 = ");
Serial.println(t1);
}
else{
t2 = millis();
Serial.print("bpm = ");
bpm = 1/(t2 - t1)*60000;
if(bpm < 0 || bpm > 300) bpm = 0;
Serial.println(bpm);
}
}
lastDebounceTime = millis();
}
void loop(){
while( bpm <= 130 ){
Serial.println(bpm);
DmxSimple.write(1, 200);
int x = 1;
for (int i = 0; i > -1; i = i + x){
//sensorValue = analogRead(A1);
//outputValue = map(sensorValue, 0, 1023, 0, 255); // map it to the range of the analog out:
DmxSimple.write(1, 255); //main
if (i == 100) {
x = -1;
// randomSeed(analogRead(0));
reda = TrueRandom.random(1,254);
// randomSeed(analogRead(0));
greena = TrueRandom.random(1,254);
// randomSeed(analogRead(0));
bluea = TrueRandom.random(1,254);
}
fader = i*redb/100 + (float)(100 -i)/100*reda; //
fadeg = i*greenb/100 + (float)(100 -i)/100*greena; //
fadeb = i*blueb/100 + (float)(100 -i)/100*bluea; //
DmxSimple.write(5,(int) fadeb); //blue
DmxSimple.write(6,(int) fadeg); //green
DmxSimple.write(7, (int) fader); //red
// Serial.print((float)(100 -i)/100*reda); // pow ( i, -1);
// Serial.print(" ");
// Serial.println( i);
delay(20);
if (i == 0){
// randomSeed(analogRead(0));
redb = TrueRandom.random(1,254);
// Serial.println(redb);
// randomSeed(analogRead(0));
greenb = TrueRandom.random(1,254);
// Serial.println(greenb);
// randomSeed(analogRead(0));
blueb = TrueRandom.random(1,254);
// Serial.println(blueb);
}
}
}
while(bpm > 130){
Serial.println(bpm);
redb = TrueRandom.random(1,0);
greenb = TrueRandom.random(1,254);
blueb = TrueRandom.random(1,25);
DmxSimple.write(1, 200);
delay(t2-t1);
DmxSimple.write(1, 15);
delay(t2-t1);
}
}