Wiring of a SPDT swtich issues with Servo Motors

I am attempting to wire a SPDT switch I am using this switch is the double positive and one negative position in the center here included is also a wiring diagram showing the entire wiring diagram. What I need is that in one position the servo motor moves to the 0 degree position and in another positive position to 180 and in the off position the serov motor moves to the 90 degree postion, now I when I use a INUPUT_PULLUP I cannot get any type of signal from the switches, however when I dont use INPUT_PULLUP . I get alot of interference that then causes the servo motor to move erratically, the INPUT_PULLUP of course removes this but also any other signal.

/*  This code is a test for a 3D printed Eye Mechanism and Eye Brow Mechanism this is my first really ever code writing Project I am using youtube.
Tutorials as well as the Stan Winston School tutorials. My Name is Bucky.
According to this paper The Human eye can rotate down vertialcally 30 Deg, and upwards 25deg and left to right 35 off of center
https://biology.stackexchange.com/questions/72511/what-are-the-max-angles-of-human-eyeball-rotation
refrences Nilheim Mechatromics Simplified Eye Mechanism Code
https://howtomechatronics.com/projects/arduino-rc-airplane-diy/
People and forum's that have helped Cathode on Twitter, Luci on Discord, The Arduino Forums members
The analog smoothing is to help with motor longevity, and from the servos twitching. When this was impletmented the Servos for X and Y began to act as one on the Y axis.

Turns out both values were over writing each other so a new set of values had to be created JoyYval and JoyXval these took the place of JoyY and JoyX. These new values
did were used for the real computing.The Previous Values were set as now place holders for reserving pins. 
The analog smoothing is to help with motor longevity, and from the servos twitching. When this was impletmented the Servos for X and Y began to act as one on the Y axis.
Turns out both values were over writing each other so a new set of values had to be created JoyYval and JoyXval these took the place of JoyY and JoyX. These new values
did were used for the real computing.The Previous Values were set as now place holders for reserving pins.  Newpotval = JoyY + Potval; was changed to Newpotval = JoyYval + Potval;
because the eyelids werent following the Y axis
*/
#include <Servo.h>
Servo ServoX;// Creates X axis Servo
Servo ServoY;// Creates Y axis Servo
Servo ServoUplid;// Creates upper Eye lid Servo
Servo ServoDownlid;// Creates  Lower eyelid Servo
Servo ServoBrow;// Creates eyebrow servo
int JoyY =A1;// Joy Stick Y axis connected to Analog pin 1 and reserves that pin preventing it from being over written
int JoyX =A0;// Joystick X axis connect to analog pin 0 and reserves that pin preventing it from being over written
int JoyYval;// Deals with computing the Servo Movement for the joy stick Y value
int JoyXval;// deals with computing the servo movement for the Joy stick X value
int Potval; // the holding Value for eyelids
int Lidpot= A2 ; // the poteniometer for controlling the Eyelids
int Newpotval; // this creates a holding value for other servos
int Lidbutton= 13 ;//Button for Blinking the Eyelid PWM 13
const int lidnumreadings = 10; // sets the sample size for data
int lidreadings [lidnumreadings]; // the readings from analog input from the eye lid potentiometer
int lidreadIndex = 0;// index of the current reading
int lidtotal = 0;// running total;
int Browpin1=7; // intializes pin 7
int Browpin2=6;// intializes pin 6
int BrowVal1; // eye brow values
int BrowVal2;

void setup() {
ServoX.attach (9);// Attaches  X axis Servo to the PWM pin 9
ServoY.attach (10); // Attaches Y axis Servo to the PWM pin 10
ServoUplid.attach (11); // Attaches the Upper Eyelid Servo to the 11 PWM pin
ServoDownlid.attach (12);// Attaches the lower Eyelid Servo to the 12 PWM pin
ServoBrow.attach (8);// Attaches the Eyebrow servo to the PWM pin 8
Serial.begin (9600);
pinMode(Browpin1,INPUT_PULLUP);// sets PWM Pin 7 to input
pinMode(Browpin2,INPUT_PULLUP); // sets PWM Pin 6 to input
}

void loop() {
  JoyXval = analogRead(JoyX);
   JoyYval = analogRead(JoyY);
   Potval = analogRead(Lidpot);
  Lidbutton = digitalRead (13);
 

//Analog smoothing section for the Eyelid
lidtotal = lidtotal - lidreadings [lidreadIndex]; // subtract last reading
lidreadings [lidreadIndex] = analogRead (Lidpot);// sets lidreadings to equal whatever is being analog read from the eye lid potentiometer
lidtotal = lidtotal + lidreadings [lidreadIndex]; // add the reading to the total
lidreadIndex = lidreadIndex + 1; // advance to the next postion of the array
if (lidreadIndex >= lidnumreadings) {// if you are at the end of the array
  lidreadIndex = 0;// loop back around to the begining 
}
Potval = lidtotal / lidnumreadings;// calculates the average and makes the average be the potval

  JoyXval = map(JoyXval, 0, 1023, 55, 125);//maps values to realistic range of motion this might need to be changed later for loss of movement through cable housing
  JoyYval = map(JoyYval, 0, 1023, 60, 115);//maps value to realistic range of motion
  Potval = map(Potval, 0, 1023, -20, 20);

  Newpotval = JoyYval + Potval;
  Newpotval = constrain(Newpotval, 0, 180);// limits Joy Stick Y axis from telling motor to move out of 180 deg 

  ServoX.write(JoyXval); // Writes the servo the signal
  ServoY.write(JoyYval); // This line then writes servo motor Y the Angle Interger value
  ServoUplid.write(Newpotval);//writes to the upper eyelid
  ServoDownlid.write(Newpotval);// writes to the lower eyelid

// if then statements for blinking
if ( Lidbutton == HIGH ) {
ServoUplid.write (JoyY);
ServoDownlid.write (JoyY);
}
else { 
 ServoUplid.write(Newpotval);//writes to the upper eyelid
  ServoDownlid.write(Newpotval);// writes to the lower eyelid
}
// This section is for the usage of the Eyebrow mechanism
BrowVal1=digitalRead(Browpin1);// reads the input from Browpin1 and assigns it to Browval1
if (BrowVal1== HIGH)// if input to BrowVal1 is HIGH
{ServoBrow.write (180);}

BrowVal2=digitalRead(Browpin2);
if (BrowVal2== HIGH)
{ServoBrow.write (0);}

if (BrowVal1== HIGH && BrowVal2 == HIGH)
{ServoBrow.write (90);}

delay (50);

// Serial Print stuff for trouble shooting
/*Serial.print(JoyXval);
Serial.print (",");
Serial.print(JoyYval);
Serial.print (",");
Serial.print (Newpotval);
Serial.print (",");
Serial.print(A4);
Serial.print (",");
Serial.print ("6"); */
Serial.print (BrowVal1);
Serial.print (",");
Serial.print (BrowVal2);
Serial.println (",");
}

Connect the middle switch pin to GND and use INPUT_PULLUP.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.