// Controlling a servo position using a potentiometer (variable resistor) // by Michal Rinott #include Servo a; // create servo object to control a servo Servo b; // create servo object to control a servo Servo c; // create servo object to control a servo Servo d; // create servo object to control a servo int potpin0 = 0; // analog pin used to connect the potentiometer int potpin1 = 1; // analog pin used to connect the potentiometer int potpin2 = 2; // analog pin used to connect the potentiometer int potpin3 = 3; // analog pin used to connect the potentiometer int val0; // variable to read the value from the analog pin int val1; // variable to read the value from the analog pin int val2; // variable to read the value from the analog pin int val3; // variable to read the value from the analog pin void setup() { a.attach(0); // attaches the servo on pin 0 to the servo object b.attach(1); // attaches the servo on pin 1 to the servo object c.attach(2); // attaches the servo on pin 2 to the servo object d.attach(3); // attaches the servo on pin 3 to the servo object } void loop() { val0 = analogRead(potpin0); // reads the value of the potentiometer (value between 0 and 1023) val0 = map(val0, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) a.write(val0); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there val1 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023) val1 = map(val1, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) b.write(val1); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023) val2 = map(val2, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) c.write(val2); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023) val3 = map(val3, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) d.write(val3); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }