Loading...
Pages: [1]   Go Down
Author Topic: program wont return to loop from interrupt.  (Read 151 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
Code:
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:
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);
  }
}
« Last Edit: January 28, 2013, 09:45:53 am by phaseform » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 336
Posts: 36476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to modify your post. Delete all the code. Then, edit your code. Remove all the commented out code. Clearly it is not the cause of your problem. Then, use Tools + Auto Format to fix the random indenting. Then, select the # icon, and paste your code. Save your changes. It is really not likely that you have set the maximum number of channels to a smiley face.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 336
Posts: 36476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
do you mean this interrupt, where further interrupts are disabled, and "millis" never updates?
I don't see that the function is dependent on millis() changing.

I do see Serial.print() statement in the interrupt service routine that do not belong there.
Logged

Offline Offline
God Member
*****
Karma: 9
Posts: 839
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your loop() function with the two while loops within it,  seems fundamentally misconceived.

I think you need to re-think the basic operation of what you are actually trying to do. 

It's a bad idea to have some loop within your loop() function,  which might never end.
It is better to have your loop() function iterate continuously,  checking what the state
of your system is.
Logged

Poole, Dorset, UK
Offline Offline
God Member
*****
Karma: 12
Posts: 780
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      bpm = 1/(t2 - t1)*60000;

First ti an t2 need to be unsigned long if you want this
Code:
void bpmset(){
  if ((millis() - lastDebounceTime) > debounceDelay) {
to work when mills rolls over.

By this
Code:
      bpm = 1/(t2 - t1)*60000;
I assume you mean

Code:
      bpm = (1/(t2 - t1))*60000;

This
Code:
//bpm = 60000UL/(t2-t1);

gets you the correct answer with t1 and t2 as unsigned longs and saves you a very expensive divide.

Format your code it will help you find bugs and it helps us help you.

Mark

Logged

South Texas
Offline Offline
God Member
*****
Karma: 8
Posts: 978
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And don't use serial print inside and interrupt routine...
Logged

Pages: [1]   Go Up
Print
 
Jump to: