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
}