Im attempting to get 2 servo motors to work on a pushbutton to open a face mask a set distance. Currently with everything wired and coded they open as far as i tell them to but they just continuously move open and closed and dont toggle with the push button switch im using. They never stop. Anyone have a clue what i've done wrong? I currently have the Servo Sensor wire on pins D9 and D10, with the grounds to GND and the power to 5v on an Arduino Nano. I then have a push button (momentary switch) on D2 and GND.
#include "ServoEasing.h"
ServoEasing servoTop;
ServoEasing servoBottom;
const int action_pin = 2;
int location = 31;
// Below numbers should be adjusted in case the facemask does not close/open to desired angle
int bottom_closed = 107;
int top_closed = 167;
int bottom_open = 20;
int top_open = 20;
void setup()
{
pinMode(action_pin, INPUT_PULLUP);
servoTop.attach(9);
servoBottom.attach(10);
setSpeedForAllServos(190);
servoTop.setEasingType(EASE_CUBIC_IN_OUT);
servoBottom.setEasingType(EASE_CUBIC_IN_OUT);
synchronizeAllServosStartAndWaitForAllServosToStop();
}
void loop()
{
int proximity = digitalRead(action_pin);
if (proximity == LOW)
{
if (location > bottom_open) {
servoTop.setEaseTo(top_open);
servoBottom.setEaseTo(bottom_open);
synchronizeAllServosStartAndWaitForAllServosToStop();
location = bottom_open;
delay(600);
} else {
servoTop.setEaseTo(top_closed);
servoBottom.setEaseTo(bottom_closed);
synchronizeAllServosStartAndWaitForAllServosToStop();
location = bottom_closed;
delay(600);
}
}
}
The Nano is not a power supply. The servos need more current than the Nano can supply. Use an external power source for the servos. A 4 AA cell pack works well as does a 2A (for 2 servos) cell phone charger. Be sure to connect the external power supply grounds to the Nano ground as well as the servo grounds.
Okay so should I wire the Servos to there own 4 AA pack and power the nano with a secondary source, or would it be fine to power the nano + servo's from the same 4 cell pack?
The 4 AA pack will be about 6V. That is too much to feed directly to the Nano 5V (5.5V max.) and too little to feed to the Vin pin (7V min.). You could use 1 or 2 silicon rectifier diodes in series to drop the battery voltage and feed that to the 5V pin. 0.6 to 0.7V drop per diode. Put the diodes between the battery + output and the 5V pin.
If you use primary cells (non-rechargeable). But if you use rechargeable NiMH, the voltage will be close to 5V and ideal to power the Nano through the 5V pin. Just don't forget and change to using primary cells!
Here is a code-version that prints to the serial monitor
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
#include "ServoEasing.h"
ServoEasing servoTop;
ServoEasing servoBottom;
const int action_pin = 2;
int location = 31;
// Below numbers should be adjusted in case the facemask does not close/open to desired angle
int bottom_closed = 107;
int top_closed = 167;
int bottom_open = 20;
int top_open = 20;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
pinMode(action_pin, INPUT_PULLUP);
servoTop.attach(9);
servoBottom.attach(10);
setSpeedForAllServos(190);
servoTop.setEasingType(EASE_CUBIC_IN_OUT);
servoBottom.setEasingType(EASE_CUBIC_IN_OUT);
synchronizeAllServosStartAndWaitForAllServosToStop();
}
void loop() {
int proximity = digitalRead(action_pin);
dbgi("1:",proximity,500); // once every 500 milliseconds print to serial monitor
if (proximity == LOW) {
if (location > bottom_open) {
dbgi("2:location > bottom_open ",location,500);
dbgi("3:",bottom_open,500);
servoTop.setEaseTo(top_open);
servoBottom.setEaseTo(bottom_open);
synchronizeAllServosStartAndWaitForAllServosToStop();
location = bottom_open;
delay(600);
}
else {
dbgi("4:else ",location,500);
servoTop.setEaseTo(top_closed);
servoBottom.setEaseTo(bottom_closed);
synchronizeAllServosStartAndWaitForAllServosToStop();
location = bottom_closed;
delay(600);
}
}
}