/* Sweep
Testing testing, my first attempt to control a servo
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int end = 180;
int pos = 0; // variable to store the servo position
int speed = A0; // analog pin use a potentiometer to control the speed, Niklas
int stroke =A1; // analog pin use a potentiometer to control the stroke, Niklas
int position =A2; // analog pin use a potentiometer to control the position, Niklas
int val1 = speed; // variable to read the value from the analog pin, Niklas
int val2 = stroke; // variable to read the value from the analog pin, with this pot you move the 0 piont (pos value). Niklas
int val3 = position; // arbetsområdet är 100 då har jag 80 kvar att röra mig på
void setup() {
//Add Serial.Begin(9600) to see values from pots
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// Forward program
//Add Serial.printIn() here and add pot to read value
for (pos = 0; pos <= val2; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree, and potentiometer control the stroke
val1 = analogRead(speed); // reads value from potentiometer, Niklas
val2 = analogRead(stroke); // reads value from potentiometer, Niklas
val3 = analogRead(position);
val1 = map(val1, 0, 1023, 0, 20);
val2 = map(val2, 0, 1023, 0, end);
val3 = map(val3, 0, 1023, 0, end);
myservo.write(pos+val3); // tell servo to go to position in variable 'pos'
delay(val1); // Speed potentiometer
}
//Backward program
//Add Serial.printIn() here and add pot to read value
for (pos = val2; pos >= 0; pos -= 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree, and potentiometer control the stroke
val1 = analogRead(speed); // reads value from potentiometer, Niklas
val2 = analogRead(stroke); // reads value from potentiometer, Niklas
val3 = analogRead(position);
val1 = map(val1, 0, 1023, 0, 20);
val2 = map(val2, 0, 1023, 0, end);
val3 = map(val3, 0, 1023, 0, end);
myservo.write(pos+val3); // tell servo to go to position in variable 'pos'
delay(val1); // Speed potentiometer
}
}