...
Code Block |
---|
#include <SoftwareSerial.h> #include <Servo.h> Servo yservo; // create servo 1 object to control a servo Servo xservo; // create servo 2 object to control a servo int yaxel = 0; // analog pin used to connect the potentiometer joysticks Y-axis. int val; // variable to read the value from the analog pin int xaxel = 1; // analog pin used to connect the joysticks X-axis. int val2; // variable to read the value from the analog pin void setup() { yservo.attach(13); // attaches the servo on pin 13 to the servo object xservo.attach(12); // attaches the servo on pin 12 to the servo object Serial.begin(115200); pinMode(11, OUTPUT); // Green led pinMode(10, OUTPUT); // Red led } void loop() { val = analogRead(yaxel); // reads the value of the potentiometerjoystick (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); // Light to Green Led if value is higher enough } else(digitalWrite(11, LOW)); if(val2<120) { digitalWrite(10, HIGH); } else(digitalWrite(10, LOW)); // Light to Red Led if value is lower enough } |