I learned how to control a Servo through PWM pins. By using digitalRead, digitalWrite, and servo write, I read the input switch and told the servo to point towards the left or right, while simultaneously turning on the corresponding LED.
Servo/LED Control from Soonho Kwon on Vimeo.
——— CODE ———-
// SOONHO KWON 2017
#include <Servo.h>
// PIN ASSIGNMENT
const int SERVO_PIN = 9;
const int SWITCH = 8;
const int RIGHT_LED = 2;
const int LEFT_LED = 4;
Servo SERVO;
void setup()
{
Serial.begin(9600);
pinMode(SWITCH, INPUT);
pinMode (RIGHT_LED, OUTPUT);
pinMode (LEFT_LED, OUTPUT);
// Initialize the Servo object to use the given pin for output.
SERVO.attach(SERVO_PIN);
}
void loop()
{
int value = digitalRead(SWITCH);
Serial.println(value);
//LED
if (value == HIGH) {
Serial.println(value);
digitalWrite(RIGHT_LED, HIGH);
digitalWrite(LEFT_LED, LOW);
}
if (value == LOW) {
Serial.println(value);
digitalWrite(RIGHT_LED, LOW);
digitalWrite(LEFT_LED, HIGH);
}
// SERVO STUFF
if (value == HIGH) {
SERVO.write(167);
}
if (value == LOW) {
SERVO.write(13);
}
}
Leave a Reply
You must be logged in to post a comment.