After this week 4 class, I wanted to try controlling the Servo and LED using analog input by potentiometer. My idea was that I wanted to know if we can create different output from same input. So in this case the first output will be the movement of the servo and the second output will be the LED lighting up.
I first tried with this code:
#include <Servo.h>
Servo myServo;
int pot;
int angle;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(9);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
pot = analogRead(A0);
angle = map(pot, 0, 1023, 0, 180);
myServo.write(angle);
if(angle >90){
digitalWrite(ledPin, HIGH);
}
// Serial.println(pot);
}
I expected the LED to only light up when the angle of the servo is more than 90 degrees, but it didn’t turn out that way as you can see from the video.
After my first attempt, I used 2 LEDs instead of only one. My goal was that the yellow LED will light up when the angle of the servo is more than 90 degrees, and white LED will light up if the angle is less than 90 degrees. It seemed to work this time.
Code:
#include <Servo.h>
//int ledPin = 2;
int brightness = 0;
int analogValue = 0;
Servo myServo;
int pot;
int angle;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(9);
pinMode(2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
pot = analogRead(A0);
analogValue = analogRead(A0);
angle = map(pot, 0, 1023, 0, 180);
myServo.write(angle);
if (angle > 90) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
} else {
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
}
}
My third attempt was that I wanted to see if I can also alter the brightness of the LEDs at the same time while spinning the servo. However it didn’t turn out the way I wanted it to, and I realized that I might had the wrong code, so I tried changing the code but it still wouldn’t work the way I wanted it to be.
Code:
#include <Servo.h>
//int ledPin = 2;
int brightness =0;
int analogValue = 0;
Servo myServo;
int pot;
int angle;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(9);
pinMode(2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
pot = analogRead(A0);
analogValue = analogRead(A0);
angle = map(pot, 0, 1023, 0, 180);
myServo.write(angle);
brightness = analogValue/4;
if (angle > 90) {
analogWrite(2, brightness);
analogWrite(3,brightness);
} else {
analogWrite(2,brightness);
analogWrite(3, brightness);
}
}
Code:
#include <Servo.h>
//int ledPin = 2;
int brightness = 0;
int analogValue = 0;
Servo myServo;
int pot;
int angle;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(9);
pinMode(2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
pot = analogRead(A0);
analogValue = analogRead(A0);
angle = map(pot, 0, 1023, 0, 180);
myServo.write(angle);
brightness = analogValue / 4;
if (angle > 90) {
analogWrite(2, brightness);
digitalWrite(3, LOW);
} else {
digitalWrite(2, LOW);
analogWrite(3, brightness);
}
}
Comments