Ring Bell Twice on Pushbutton Transition

Hi,

Very new to Arduino but here goes. I'm designing a bell control box for a sound stage that has to function as follows:

Press Roll PB:

  • Turn on red light
  • Ring Bell for 1 sec.
  • Flash "Rolling" Indicator
  • Pulse (250ms) Recorder Roll Control

Press Cut PB

  • Turn off red light
  • Ring Bell twice for 250 ms.
  • Turn off Flashing "Rolling" Indicator
  • Pulse (250ms) Recorder Stop Control

I've managed to learn and use many examples I've found online and have gotten everything to work very well except one function. I can't seem to ring the bell for the two short bursts when the "Cut" PB is pressed. Right now I'm using a routine to detect a transition to trigger the bell because I only want it to ring twice no matter how long the "Cut" PB is depressed.

The following code will trigger properly on the "Cut" PB transition and produces one 250 ms. pulse, but no matter what kind of delays and loop counters I've tried I just can't get it to work for a second time.

 if (sbraising_edge == true && cutstatus == LOW && bellenable != LOW){    
    CutBellState = 1;
  
    CutBellMillis = millis();                                        
    }
    if (millis() - CutBellMillis > CutBellPeriod){                   
    CutBellState = 0;

Now this next piece of code will ring the bell twice for the correct time period but won't trigger on the transition.

if(BellRun < 4){
    if (cutstatus != LOW && FlashState != true && bellenable != LOW ){
        unsigned long currentMillis2 = millis();
      if(currentMillis2 - CutBellMillis >= CutBellPeriod){    
         CutBellMillis = currentMillis2;   
         BellRun = BellRun + 1;
                            
      if (CutBellState == LOW)                            
          CutBellState = HIGH;
      else
          CutBellState = LOW;
      }
   }
 }

I've tried to marry the two but have not had any luck yet. Thoughts?

BTW the logic for the program flow (which is working perfectly except for this problem) I wrote, but the routines for the transition detection and timing were gathered online so my understanding of their operation is fundamental at best.

Thanks in advance,

Ken

maybe...?

if (sbraising_edge == true && cutstatus == LOW && bellenable != LOW){    
    CutBellState = 1;
  
    CutBellMillis = millis();                                        
    }
    if (millis() - CutBellMillis > CutBellPeriod){                   
    CutBellState = 0;
    if (millis() - CutBellMillis > (CutBellPeriod x 2)
    CutBellState = 1;
    if (millis() - CutBellMillis > (CutBellPeriod x 3)
    CutBellState = 0;

jcallen:
maybe...?

if (sbraising_edge == true && cutstatus == LOW && bellenable != LOW){    

CutBellState = 1;
 
    CutBellMillis = millis();                                       
    }
    if (millis() - CutBellMillis > CutBellPeriod){                 
    CutBellState = 0;
    if (millis() - CurBellMillis > (CutBellPeriod x 2);
    CutBellState = 1;
    if (millis() - CurBellMillis > (CutBellPeriod x 3);
    CutBellState = 0;

I agree with what you're trying to do here. Here it is with what you probably had in mind

if (sbraising_edge == true && cutstatus == LOW && bellenable != LOW){    
    CutBellState = 1;
  
    CutBellMillis = millis();                                        
}

if (millis() - CutBellMillis > CutBellPeriod)                  
    CutBellState = 0;
else if (millis() - CurBellMillis > (CutBellPeriod x 2))
    CutBellState = 1;
else if (millis() - CurBellMillis > (CutBellPeriod x 3))
    CutBellState = 0;

Hi,
How have you got your Buttons wired, do you have pullup or pulldown resistors fitted, or the arduino's internal pullup resistors activated.

It would be good if you posted your entire code.

Thanks...Tom..... :slight_smile:

if (sbraising_edge == true && cutstatus == LOW && bellenable != LOW){

I am always wary of complicated IF statements.
Here you have three tests which means there are 8 possible outcomes. It is very easy to get mixed up.

I find that using nested tests is easier to follow

if (sbraising_edge == true ) {
   if (cutstatus == LOW) {
      if ( bellenable != LOW) {

      }
   }
}

and I suspect it makes no difference to the code the compiler produces

With careful thought you can put first the test that is least likely to be true - so, most times the whole thing is abandoned quickly.

Please post your complete program.

...R

Yep, the semicolons at the end of the if statements were wrong: my bad, but I saw the curly brace at the end of his if statement and not knowing what was after the last line I was just throwing in an idea.

Well guys that looked great and thank you for the timely response but trying it didn't seem to help. As TomGeorge suggested I'm posting the entire code to see if that put things into better prospective. Please ignore the comments since much of the code segments were captured online. I'm still hacking away at it trying to figure this out but it is an awesome learning experience.

Ken

Roll_Control_7_04_15.ino (9.33 KB)

It would be much easier to make sense of your code (for you and for us) if you divide it up into functions to separate the details from the logic. Have a look at planning and implementing a program.

Then your code in loop() could be something close to the description in your original post. Something like

void loop() {
  readButtons();

  if (rollSelected) {
     roll();
  }
  if (cutSelected) {
    cut();
  }
}

void roll() {
  lampOn(true);
  bellRing(1000);
  rollerOn( true);
  recorderOn(true);
}

void cut() {
  lampOn(false);
  bellRing(250);
  rollerOn( false);
  recorderOn(false);
}

...R

I agree Robin2,

This was my first attempt so I knew it wouldn't be pretty. The scary thing is that I used to be a pretty good assembly level programmer and even worked with C++, but that was 25 years ago. I have been relearning everything.

Thanks for the tips, once I get it working I'll teach myself more about structure and clean it up.

Any recommend literature that you think would help?

Ken

No_MOS:
Any recommend literature that you think would help?

I don't have any suggestions, but I should have included a link to planning and implementing a program

...R

I saw it and I'm reading it. Thank you.

BTW I got my program working thanks to jcallen's idea. Thanks j.

Ken