Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#include <SoftwareSerial.h>
#include <Servo.h>


Servo yservo;  // create servo-Y object to control a servo
Servo xservo;  // create servo-X object to control a servo

int yaxel = 0;  // analog pin used to connect the 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-Y on pin 13 to the servo object
  xservo.attach(12);  // attaches the servo-X 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 joystick (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 254);   // scale it to use it with the servo-Y (value between 0 and 254)

  val2 = analogRead(xaxel);          // reads tje value of the joystick (value between 0 and 1023)
  val2 = map(val2, 0, 1023, 0, 254); // scale it to use it with the servo-X (value between 0 and 254)

  yservo.write(val);                  // sets the servo-Y position according to the scaled value
  xservo.write(val2);                 // sets the servo-X position according to the scaled value

  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
}

...