Im my basement washroom I have a servo that is connected to the arduino. The servo is operated when a pin is grounded. The pin is wired to a dry contact relay on a Z-Wave relay then out to ground. I can then operate the servo with the push of a button on my cell. Problem is when I turn on a light near the servo (not sure if other lights will do it) the servo will operate. Im not sure were to start looking for the problem.
// Include the Servo library
#include <Servo.h>
#include <TimerThree.h>
// Declare the Servo pins
const byte servoPin1 = 9;
const byte servoPin2 = 10;
const byte servoPin3 = 11;
const byte mosfet = 22;
const byte interruptPin1 = 18; //push button 1
const byte interruptPin2 = 19; //push button 2
const byte interruptPin3 = 20; //push button 2
//flags to push buttons interrupts
volatile boolean servo1Button = false;
volatile boolean servo2Button = false;
volatile boolean servo3Button = false;
volatile boolean buttonPressed = false;
//count 30 sec variable
volatile byte count = 0;
// 5 sec delay counter for each servo
volatile byte countServo1 = 0;
volatile byte countServo2 = 0;
volatile byte countServo3 = 0;
// Create servo objects
Servo Servo1, Servo2, Servo3;
void delay30Sec() {
//mosfet delay counter update
if (buttonPressed) {
count++;
if (count == 30) {
digitalWrite(mosfet, LOW);
buttonPressed = false;
count = 0;
}
}
//counter for each element according to servo#ButtonValue
if (servo1Button) {
if (countServo1 == 0) {
digitalWrite(mosfet, HIGH);
buttonPressed = true;
Servo1.write(110);
count = 0;
}
countServo1++;
if (countServo1 == 10) {
Servo1.write(0);
servo1Button = false;
countServo1 = 0;
}
}
if (servo2Button) {
if (countServo2 == 0) {
digitalWrite(mosfet, HIGH);
buttonPressed = true;
Servo2.write(110);
count = 0;
}
countServo2++;
if (countServo2 == 10) {
Servo2.write(0);
servo2Button = false;
countServo2 = 0;
}
}
if (servo3Button) {
if (countServo3 == 0) {
digitalWrite(mosfet, HIGH);
buttonPressed = true;
Servo3.write(110);
count = 0;
}
countServo3++;
if (countServo3 == 10) {
Servo3.write(0);
servo3Button = false;
countServo3 = 0;
}
}
}
void servo1Interrupt() {
servo1Button = true;
}
void servo2Interrupt() {
servo2Button = true;
}
void servo3Interrupt() {
servo3Button = true;
}
void setup() {
// We need to attach the servo to the used pin number
//set the mode of the pins..
//note that the buttons are used with negative logic as the internal pullup resistors are used
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(interruptPin3, INPUT_PULLUP);
pinMode(mosfet, OUTPUT);
digitalWrite(mosfet, LOW);
Servo1.attach(servoPin1);
Servo2.attach(servoPin2);
Servo3.attach(servoPin3);
Servo1.write(0);
Servo2.write(0);
Servo3.write(0);
attachInterrupt(digitalPinToInterrupt(interruptPin1), servo1Interrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin2), servo2Interrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin3), servo3Interrupt, FALLING);
Timer3.initialize(1000000);
Timer3.attachInterrupt(delay30Sec);
}
void loop() {
}
From Your description it looks like the light equipment causes a disturbance that hits Your Arduino. Give us the wireing diagram, cable lengths, type of cables.
There is code coming before Setup is reached. That looks odd to me. I am used to declarations, Setup, containing some code and then loop(). Putting the code in the end of setup also works, it looks like.
Thanks D-G. I see that now. Time for sleeping, 3 am here...
the wire that goes to the servo is a cat 4 telephone wire 4 conductor for about 50 feet there is two runs one for each of 2 servos.
In the scetch the only way the servo is to operate is when the pin is grounded. Why would the run on the wire to the servo matter.
I have removed the arduino and servo from that location to my work bench and I have notice when I attach a volt meter to pin 18 (the pin that is grounded to operate serrvo) the servo opperates Y is that happening
Ive also notice that even when I take a long wire 2 pair 22 gauge still on the wire reel and touch one end to pin 18 the servo will operate even though the voltage does not drop.
I dont understand in the sketch its only to operate when pin 18 goes low (by grounding the pin)
It sounds like the signal is a logic signal. Add a pullup near the servo.
Hi,
Please post your entire code, there is no code in the void loop().
Can I suggest you remove ALL interrupts, I don't see a need for them in a non time critical situation.
Just poll the inputs at the start of the void loop().
Use external pullups, like 10K resistors.
Does the problem only occur on pin 18?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please include your power supplies and their power rating.
What model Arduino are you using?
Thanks.. Tom... 
Ok to Railroader for now the servo is 8 inch from the arduino so I dont think the pullup will do anything
I have added my wiring diagram for one servo. Its the same for the other two just using pins 9,10 for data and 19,20 for relay or pushbutton.
Yes it does it on all three pins that are set up as bushbuttons
Like I said I dont understand the servo operating when I touch pin 18 with the lenght of wire thats not grounded
The entire code is in the first post
Thanks
I don't argue against interrupts or not but how are those pins setup within the controller? Pin 18 and similiar. I urge You to make sure that pullups are used within, or close to, the controller. I only use UNOs but You probably use another one.
Sorry Im slow at this. Your saying place maybe a 10kR at the arduino between 5v and the data on the servo or 5v to pin 18 the pushbutton.
Using a Mega
Hi,
I see that you are high side switching the servo supply, what happens if you bypass the MOSFET so the servo is powered continuously?
You are fortunate that the servo supply is 5V as your high side switching will not work with any higher servo supply voltage than 5V.
Another suggestion, if to write some code that just uses one input and one servo without interrupts.
Thanks.. Tom...
@TG. Circuit does not show up here, a White X on black background inside Square lines.
10kR ought to be good.
Thanks for your input since I couldn't get the servo's to work the way I wanted I deleted the sketch and started again. I found a sketch online with one pushbutton and one servo and got it working the way I wanted. I tried to add another pushbutton and servo but both servos operate with each button can you tell what I have done wrong
Thanks
// Include the Servo library
#include <Servo.h>
Servo myservo1; //servo1
Servo myservo2; //servo2
int pos1 = 0; //variable for number of degrees for servo1
int pos2 = 0; //variable for number of degrees for servo2
int buttonPin1 = 2; //activating pin 2 for Push Button
int buttonPin1State = 0; //variable for Push Button
int buttonPin2 = 3; //activating pin 3 for Push Button
int buttonPin2State = 0; //variable for Push Button
void setup() {
myservo1.attach(9); //activating pin 9 for servo1
myservo2.attach(10); //activating pin 10 for servo2
pinMode(buttonPin1 , INPUT); //setting Push ButtonNo. 1 as an input
pinMode(buttonPin2 , INPUT); //setting Push ButtonNo. 2 as an input
}
void loop() {
buttonPin1State = digitalRead(buttonPin1); //reading buttonPin1 Pin and storing it as buttonState.
buttonPin2State = digitalRead(buttonPin2); //reading buttonPin2 Pin and storing it as buttonState.
if (buttonPin1State == HIGH){ //if button no.1 is pressed...
if (buttonPin2State == HIGH){ //if button no.2 is pressed...
myservo1.write(110); //setting servo1 to 110 degrees
myservo2.write(110); //setting servo2 to 110 degrees
delay(5000); //wait one second
myservo1.write(0); //setting servo1 to 0 degrees
myservo2.write(0); //setting servo2 to 0 degrees
}
}
}
That's the way Your code should work. I used autoformat and got this:
// Include the Servo library
#include <Servo.h>
Servo myservo1; //servo1
Servo myservo2; //servo2
int pos1 = 0; //variable for number of degrees for servo1
int pos2 = 0; //variable for number of degrees for servo2
int buttonPin1 = 2; //activating pin 2 for Push Button
int buttonPin1State = 0; //variable for Push Button
int buttonPin2 = 3; //activating pin 3 for Push Button
int buttonPin2State = 0; //variable for Push Button
void setup() {
myservo1.attach(9); //activating pin 9 for servo1
myservo2.attach(10); //activating pin 10 for servo2
pinMode(buttonPin1 , INPUT); //setting Push ButtonNo. 1 as an input
pinMode(buttonPin2 , INPUT); //setting Push ButtonNo. 2 as an input
}
void loop() {
buttonPin1State = digitalRead(buttonPin1); //reading buttonPin1 Pin and storing it as buttonState.
buttonPin2State = digitalRead(buttonPin2); //reading buttonPin2 Pin and storing it as buttonState.
if (buttonPin1State == HIGH) { //if button no.1 is pressed...
if (buttonPin2State == HIGH) { //if button no.2 is pressed...
myservo1.write(110); //setting servo1 to 110 degrees
myservo2.write(110); //setting servo2 to 110 degrees
delay(5000); //wait one second
myservo1.write(0); //setting servo1 to 0 degrees
myservo2.write(0); //setting servo2 to 0 degrees
}
}
}
What do You Think about this?
void loop()
{
buttonPin1State = digitalRead(buttonPin1); //reading buttonPin1 Pin and storing it as buttonState.
buttonPin2State = digitalRead(buttonPin2); //reading buttonPin2 Pin and storing it as buttonState.
if (buttonPin1State == HIGH)
{ //if button no.1 is pressed...
myservo1.write(110); //setting servo1 to 110 degrees
delay(5000); //wait one second
myservo1.write(0); //setting servo1 to 0 degrees
}//end of b P1
if (buttonPin2State == HIGH)
{ //if button no.2 is pressed...
myservo2.write(110); //setting servo2 to 110 degrees
delay(5000); //wait one second
myservo2.write(0); //setting servo2 to 0 degrees
}//end of if b P2
}//end of loop
Thanks but still no difference pushbutton 1 will control both servo's
Button 1 is to control servo 1
Button 2 is to control servo 2
That can't be possible.
Attach Your wiring diagram. Maybe You should use "INPUT_PULLUP" for the button inputs. It depends on how the buttons are connected. Free floating inputs will give random activities.
Ok. You use 10kOhm pulldowns and feeds +5 from the buttons. That is okey.
You feed the servos from the Arduino and that can be hazardeous. What current do the servos draw? UNOs, as I am used to, have a maximum capacity of 20 mA and a destruction limit of 40 mA. Overloading the outputs can possibly cause strange things to happend.
If You use the code I sent pressing button 2 should activate servo2 and nothing else. The same for button1/servo1.
Sorry, I made a mistake. You use the Arduino 5 volt for the servos. How is the Arduino powered? By a 7 - 12 volt? In that case maybe the Arduino 5 volt converter is owerloaded. If powered by the USB the same applies, maybe the servo overload the USB power aource.
I just have one servo connected to a mega. If I can get this problem fixed my intent is to have an another pin go high when any of the buttons are pressed and this will control a mosfet and in turn power the servos from an outside sorce. Right now the mega is powered by a usb cable from my computer. That I dont think should be my problem. Right now I just have one button connected to pin 2 it will control servo 1 on pin9 if i move servo data wire from pin 9 to pin 10 and try button 1 the servo will operate and it shouldnt