Single Servo POT Control
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int POT=2; //POT on Analog Pin 0
Servo myServo;
void setup()
{
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
int val = digitalRead(POT); //Read Pot
val = map(val, 0, 1, 0, 179); //scale it to servo range
Serial.println(val);
delay(500);
if (val==179)
{
myServo.write(val); //sets the servo
delay(15); //waits for the servo
}
}
HOMEWORK:
Single Servo Control with PIR Sensor
//Servo PIR Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR=2; //POT on Digital Pin 2
Servo myServo;
void setup()
{
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
int val = digitalRead(PIR); //Read Pot
val = map(val, 0, 1, 0, 179); //scale it to servo range
Serial.println(val);
delay(500);
if (val>=178)
{
myServo.write(133); //sets the servo
delay(15); //waits for the servo
}
else
{
myServo.write(37);
delay(15);
}
}
No comments:
Post a Comment