arduino code. 2 potis to control 2 servos
- User
arduino code. 2 potis to control 2 servos
Assistant
Here's Arduino code to control 2 servos with 2 potentiometers:
#include <Servo.h> Servo servo1; Servo servo2; int pot1Pin = A0; // First potentiometer on analog pin A0 int pot2Pin = A1; // Second potentiometer on analog pin A1 int servo1Pin = 9; // First servo on digital pin 9 int servo2Pin = 10; // Second servo on digital pin 10 void setup() { servo1.attach(servo1Pin); servo2.attach(servo2Pin); Serial.begin(9600); // Optional: for debugging } void loop() { // Read potentiometer values (0-1023) int pot1Value = analogRead(pot1Pin); int pot2Value = analogRead(pot2Pin); // Map potentiometer values to servo angles (0-180 degrees) int servo1Angle = map(pot1Value, 0, 1023, 0, 180); int servo2Angle = map(pot2Value, 0, 1023, 0, 180); // Move servos to calculated positions servo1.write(servo1Angle); servo2.write(servo2Angle); // Optional: print values for debugging Serial.print("Pot1: "); Serial.print(pot1Value); Serial.print(" -> Servo1: "); Serial.print(servo1Angle); Serial.print(" | Pot2: "); Serial.print(pot2Value); Serial.print(" -> Servo2: "); Serial.println(servo2Angle); delay(15); // Small delay for smooth servo movement }Wiring:
- Potentiometer 1: Connect to A0, 5V, and GND
- Potentiometer 2: Connect to A1, 5V, and GND
- Servo 1: Signal wire to pin 9, power to 5V, ground to GND
- Servo 2: Signal wire to pin 10, power to 5V, ground to GND
The code reads the analog values from both potentiometers, maps them to servo angles (0-180°), and continuously updates the servo positions. The serial output is optional for monitoring the values.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.