Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Tässä

...

esimerkissä

...

käytetään

...

Arduino

...

Mega 2560 sekä

...

joystickia

...

kahden

...

servomoottorin

...

ohjaamiseen.

...

Toinen

...

servomoottori

...

on

...

ohjelmoitu

...

toimimaan

...

joysticin

...

kahvan

...

sivuttaisliikkeen

...

mukaan

...

ja

...

toinen

...

pystyliikkeen

...

mukaan.

...

Systeemiin

...

on

...

liitetty

...

myös

...

punainen

...

ja

...

vihreä

...

led,

...

kuvaamaan

...

toisen

...

servomoottorin

...

liikesuuntaa

...

(vihreä

...

vasemmalle,

...

punainen

...

oikealle).

...

Servomoottorien

...

korvaaminen

...

DC

...

moottoreilla

...

on

...

myös

...

mahdollista,

...

silloin

...

systeemiin

...

on

...

kuitenkin

...

lisättävä

...

nopeudensäädin

...

ja

...

akku

...

(katso

...

esimerkki).

...

Joystick

...

on

...

kytketty

...

Arduinon

...

5,5V

...

jännitteeseen,

...

maadoitukseen

...

(GND)

...

sekä

...

kahteen

...

analogiseen

...

sisäänmenoon

...

(A0

...

ja

...

A1).

...

Servomoottorit

...

on

...

kytketty

...

5,5V

...

jännitteen

...

ja

...

maadoituksen

...

lisäksi

...

myös

...

molemmat

...

omaan

...

digitaaliseen

...

ulostuloon

...

(12

...

ja

...

13).

...

Molemmat

...

ledit

...

on

...

kytketty

...

omaan

...

digitaaliseen

...

ulostuloon

...

(10

...

ja

...

11)

...

sekä

...

maadoitukseen.

...

Käytetty

...

koodi:

Code Block


\#include <SoftwareSerial.h>
\#include <Servo.h>&nbsp;


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

int yaxel = 0;&nbsp;  // analog pin used to connect the potentiometer joysticks Y-axis.
int val;&nbsp;&nbsp;&nbsp;        // variable to read the value from the analog pin&nbsp;
int xaxel = 1;  // analog pin used to connect the joysticks X-axis.
int val2;&nbsp;       // variable to read the value from the analog pin
void setup()&nbsp;
{&nbsp;
&nbsp;
  yservo.attach(13);&nbsp;  // attaches the servo-Y on pin 13 to the servo object
&nbsp;  xservo.attach(12);&nbsp;
&nbsp;  // attaches the servo-X on pin 12 to the servo object
  Serial.begin(115200);
&nbsp;  pinMode(11, OUTPUT);
&nbsp;  // Green led
  pinMode(10, OUTPUT);  // Red led
}&nbsp;
&nbsp;
void loop()&nbsp;
{&nbsp;
&nbsp;
  val = analogRead(yaxel);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            // reads the value of the potentiometerjoystick Y-axis (value between 0 and 1023)&nbsp;
&nbsp;
  val = map(val, 0, 1023, 0, 254);&nbsp;&nbsp;   // scale it to use it with the servo-Y (value between 0 and 180254)&nbsp;
&nbsp;

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

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

  delay(15);                           // waits for the servo to get there&nbsp;
&nbsp;
  Serial.println(val);
&nbsp;  Serial.println(val2);
&nbsp;  if(val2>134)
&nbsp;  {
&nbsp;&nbsp;&nbsp;    digitalWrite(11, HIGH);
&nbsp;           // Light to Green Led if value is higher enough
  }
&nbsp;  else(digitalWrite(11, LOW));
&nbsp;  if(val2<120)
&nbsp;  {
&nbsp;&nbsp;&nbsp;    digitalWrite(10, HIGH);
&nbsp;  }
&nbsp;  else(digitalWrite(10, LOW));      // Light to Red Led if value is lower enough
}

Image Added