10 Mart 2015 Salı

Arduino Experiment 8 - Driving a Servo Motor Modify


Definition : Driving servo with 2 buttons. If left button is pushed, servo turns to the left and if right button is pushed, servo will turn to the right.

Circuit Diagram





Components

  •          2 x Button
  •          2 x 10 KΩ Resistor
  •          1 x Servo Motor
  •          Arduino Uno
  •          Bread Board


Code

#include <Servo.h>  // servo library
Servo servo1;  // servo control object
int ledred = 13;
int ledyellow = 12;
int ledgreen = 11;
int button1 = 2;
int button2 = 3;
int pos = 0;
boolean check = false;

void setup()
{
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);

  servo1.attach(9);
}


void loop()
{
  int button1State, button2State;
  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
 if(button1State == LOW && button2State == HIGH) {
   if(pos <170){
   pos = pos+100;
  check = true;
}
   }
  
   if(button2State == LOW && button1State == HIGH ) {
     if(pos > 10){
   pos = pos-100;
  check = true;
    }
   }
  if(check){
  servo1.write(pos);
  check = false;
  }
delay(200);


}

 Video






Hiç yorum yok:

Yorum Gönder