Wrong servo spinning

Hey so I have this code

#include <Servo.h>
int yVal;
int xVal;
int swVal;
int yPin=A0;
int xPin=A1;
int swPin=7;
Servo servo; 
int servoPin=3;
int servoPos;
Servo servo2;
int servoPin2=5;
int servoPos2;

void setup() {
  pinMode(yPin,INPUT);
  pinMode(xPin,INPUT);
  pinMode(swPin,INPUT);
  digitalWrite(swPin,HIGH);
  Serial.begin(9600);

  servo.attach(servoPin);
  servo.attach(servoPin2);
}

void loop() {
 /* yVal=analogRead(yPin);
  xVal=analogRead(xPin);
  swVal=digitalRead(swPin);
  servo.write(servoPos);*/
  while (Serial.available()==0){
    
  }
  servoPos=Serial.parseInt();
  servo.write(servoPos);
  
  /*Serial.print("x: ");
  Serial.print(xVal);
  Serial.print(" ");
  Serial.print("y: ");
  Serial.print(yVal);
  Serial.print(" ");
  Serial.print("sw: ");
  Serial.println(swVal);*/
  }

and i don't know how to attach servoPos to servo and servoPos2 to servo2

You are already writing servopos to servo. You can easily duplicate that write() for servo2 and servopos2.

The question is what value is supposed to go into servopos2? Difficult to say because you haven't said what you are sending via Serial that is supposed to affect servo2.

Maybe Serial Input Basics - updated will help.

Steve

Change

to

  servo.attach(servoPin);
  servo2.attach(servoPin2);
1 Like

Thanks a lot Mark

Thanks

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