...
Code Block |
---|
#include <SoftwareSerial.h> #include <Servo.h> Servo yservo; // create servo object to control a servo Servo xservo; int yaxel = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin int xaxel = 1; int val2; void setup() { yservo.attach(13); // attaches the servo on pin 13 to the servo object xservo.attach(12); Serial.begin(115200); pinMode(11, OUTPUT); pinMode(10, OUTPUT); } void loop() { val = analogRead(yaxel); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 254); // scale it to use it with the servo (value between 0 and 180) val2 = analogRead(xaxel); val2 = map(val2, 0, 1023, 0, 254); yservo.write(val); // sets the servo position according to the scaled value xservo.write(val2); delay(15); // waits for the servo to get there Serial.println(val); Serial.println(val2); if(val2>134) { digitalWrite(11, HIGH); } else(digitalWrite(11, LOW)); if(val2<120) { digitalWrite(10, HIGH); } else(digitalWrite(10, LOW)); } |