Hey guys, I'm not entirely new to Arduino but I do struggle with writing code and understanding everything, so please bear with me. I'm making an automated chicken coop door. There are plenty of posts about them and, trust me, I've scoured many of them but none address my specific set-up. My hardware is:
Arduino Uno
Cytron MD-10C R3
12v Window motor from a Chevy truck
2 limit switches
JVR programmable 12v timer
I'm using the ezButton and Cytron motor libraries.
I have everything hooked up and working but i'm stumbling on the code. My intent is to use the 12v timer to simply trigger the arduino on and off, twice daily. Each time it powers on i want the arduino to address which of the two switches is pressed and act accordingly (drive the motor up or down) until the other switch is pressed (stop the motor). I have a basic sketch where i can press one button to start the motor and press the other to stop it. My issue is that when either button is held at start up, it doesn't recognize it as being pressed. It has to be released and then pressed again. Is there any way to counter this?
#include "CytronMotorDriver.h"
#include <ezButton.h>
ezButton buttonBottom(6); // create ezButton object that attach to pin 6;
ezButton buttonTop(7); // create ezButton object that attach to pin 7;
// Configure the motor driver.
CytronMD motor(PWM_DIR, 3, 4); // PWM = Pin 3, DIR = Pin 4.
// The setup routine runs once when you press reset.
void setup() {
}
// The loop routine runs over and over again forever.
void loop() {
buttonBottom.loop(); // MUST call the loop() function first
buttonTop.loop(); // MUST call the loop() function first
int btn1State = buttonBottom.getState();
int btn2State = buttonTop.getState();
if (buttonBottom.isPressed())
motor.setSpeed(128); // Run reverse at full speed to lift door
delay(100);
if (buttonTop.isPressed())
motor.setSpeed(0); // Stop door
delay(100);
}
#include "CytronMotorDriver.h"
#include <ezButton.h>
ezButton buttonBottom(6); // create ezButton object that attach to pin 6;
ezButton buttonTop(7); // create ezButton object that attach to pin 7;
// Configure the motor driver.
CytronMD motor(PWM_DIR, 3, 4); // PWM = Pin 3, DIR = Pin 4.
// The setup routine runs once when you press reset.
void setup() {
int btn1State = buttonBottom.getState();
int btn2State = buttonTop.getState();
}
// The loop routine runs over and over again forever.
void loop() {
buttonBottom.loop(); // MUST call the loop() function first
buttonTop.loop(); // MUST call the loop() function first
//int btn1State = buttonBottom.getState();
//int btn2State = buttonTop.getState();
if (buttonBottom.isPressed())
motor.setSpeed(128); // Run reverse at full speed to lift door
delay(100);
if (buttonTop.isPressed())
motor.setSpeed(0); // Stop door
delay(100);
}
I'm getting the same result
To determine which of the two switches is pressed when the Arduino first turns on is best done in setup. Since the button states are static and not changing, you can use a simple digitalRead() of the switch or the equivalent ez button library function of getStateRaw(). The dynamic ez button functions won't work properly because the .loop() has not been called over and over.
The when you know which way you want to run the motor, set it up to do so, and start the motor running at the end of setup().
Then you can read both buttons dynamically in loop() and when either one is .pressed() you turn off the motor.
Thank you again, I was finally able to get those darn switches working like i wanted them to. I was never able to get it using the ezButton library, but the digitalRead() worked. I'm so happy to finally get this figured out. Will post the working code:
#include "CytronMotorDriver.h"
const int buttonPin = 1; // Pin connected to the button
const int buttonPin2 = 2; // Pin connected to the button
CytronMD motor(PWM_DIR, 3, 4); // PWM = Pin 3, DIR = Pin 4.
void setup() {
// Initialize the button pin as an input with an internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
// Read the state of the button
int buttonState = digitalRead(buttonPin);
int buttonState2 = digitalRead(buttonPin2);
// Check if the button is pressed
if (buttonState == LOW) {
motor.setSpeed(255);
delay(1000);
} else {
if (buttonState2 == LOW) {
motor.setSpeed(-255); // Run reverse at full speed to lift door
delay(1000);}
} }
void loop() {
// Read the state of the button
int buttonState = digitalRead(buttonPin);
int buttonState2 = digitalRead(buttonPin2);
// Check if the button is pressed
if (buttonState2 == LOW) {
motor.setSpeed(0); // Stop Door
delay(1000);}
if (buttonState == LOW){
motor.setSpeed(0); // Stop Door
delay(1000);}
}
I have downgraded my high praise for ezButton ("it doesn't suck") to the more accurate
ezButton doesn't suck but you can't mix using delays with it or it will. Suck.
My new recommendation for buttons is not, as far as I can tell, conveniently available in the IDE Library manager, but is ez (!) enough to grab straight off the github page here
It has a default debounce time of 100 milliseconds, but offers a constructor where you can set it to a less lazy value.
It also handle millis() incorrectly. I have not analyzed the implications but if anything goes wrong it will have been performing well for 49.71 days…
It does not care if your sketch has blocking code.