0

I am having trouble trying to make servomotor and button work together. The button code here below works fine when alone in a separate code but when it is transferred to the code with the servomotor function in it, it doesn't work.

Is there anything I need to be wary of when integrating the button code here to the preexisting code?

#include <Servo.h> // Library required for the servomotor (Make sure to download)
#include <Button.h> // Library required for the microswitch (Make sure to download)

// Parameters for the experiment 
// Note: 1000 is one second
char Experiment = 'Acclimatization';
char Bird_Name = 'birdname here'; 
int Frequency = 30 * 1000; // The motor moves every x seconds 
int Frequency_Deviation = 5 * 1000; // There are x second deviations plus and minus from the frequency set above  
int Duration = 3 * 1000;  // The food is uncovered for x seconds

int servoPin = 9; // Declare the servomotor pin (Can be varied from setup to setup)
Button button(7); // Declare the microswitch pin (Can be varied from setup to setup) 

Servo Servo1; 

void setup() { 
   Servo1.attach(servoPin); 
   //analogWrite(servoPin, 0);
   Serial.begin(9600);
   button.setDebounceTime(100); // set debounce time to 100 milliseconds
}

void loop(){ 
   // Get onset and offset times of when the bird arrives and leaves the perch
   button.loop();
   if(button.isPressed())
    Serial.println("OnsetTime");
   if(button.isReleased())
    Serial.println("OffsetTime");

   // Calculate the acclimatization parameters below
   delay(random(Frequency-Frequency_Deviation,Frequency+Frequency_Deviation)); // Calculates frequency (See paramaters above)
   Servo1.write(0); // Starts from 0 degrees
   delay(Duration); // Duration that the food is uncovered (See parameters above) 
   Servo1.write(75); // Goes to the x degrees 
}
1
  • In your code, you used delay() function two times. This may make Arduino miss some pressing event. Use timestamp instead of delay. See BlinkWithoutDelay example to see how to use timestamp. Please note that debouncing function of library does NOT use delay() function. Therefore, you do NOT need to worry about blocking issue Dec 19 '19 at 8:58
1

Short answer: You have a blocking delay interrupting your button debounce time of 100ms.

Your button library has a debounce time of 100ms. This means you must be holding the button down for more that 100ms before it will register as pressed.

You have a 3 second blocking delay at the end of your loop. When you call delay(XX), your processor cannot do anything during this time (like check for a button), it's just waiting.

Think about how the code is running. In your main loop, you are calling the function button.loop(), and it's checking to see if your button is pressed. If it's pressed, it's starts a timer (your debounce). The goal of this timer is for another 100ms to pass by with your button still being pressed to make sure you actually pressed it. Button.isPressed() fails, and it moves to its next set of instructions, which is move the servo and wait 3 seconds, during which time it cannot check the button. Then it starts all over again, and it calls button.loop() again. This all happens MUCH faster than 100ms. From the start of void loop() to the time it takes for it to wind back up at the 3 second blocking delay is in the nano seconds. This never gives your button library the 100ms of "pressed down" button it needs for button.isPressed() to return true.

I suggest spending some time looking at the behind the scenes of your button library and see how the debounce works. You can do that, or you can use the example debounce sketch off the main arduino website and learn how that works. The next thing you'd want to do is to learn how to make your code "wait" without using the blocking call delay(). This is done by using timers with millis(). A good way to learn this is the example arduino sketch "Blink Without Delay".

Hope that clears some things up.

5
  • Thank you for the feedback. What if I need the day function though? Can I still make the button work while using the delay duration of 3 seconds for example?
    – John
    Dec 19 '19 at 2:28
  • @John "This is done by using timers with millis()." Dec 19 '19 at 7:28
  • @HavocRC, That library does NOT block in debounce time. The library uses timestamp for debouncing. It does NOT use delay() function. Please read the library reference before anwsering Dec 19 '19 at 8:51
  • @John like I said you can get your code to work exactly how you want it to, but you need to use slightly more complicated code. I'm pointing you in the right direction in my answer, if you study and use those two sketches I suggested, you'll have all the knowledge you'll need to accomplish what you wish.
    – HavocRC
    Dec 19 '19 at 18:39
  • @RozonaZoro I didn't say there was a delay in the debounce library I'm talking about the 3 second delay in the OP's sketch (3rd line from the bottom).
    – HavocRC
    Dec 19 '19 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.