#include <Servo.h>
//create a servo object called servo1 Servo servo1;
Servo servo1;
void setup()
{ // put your setup code here, to run once:
// set the servo pin, pin 9, as an servo output pin
servo1.attach(9);
}
void loop() { // put your main code here, to run repeatedly:
int lightValue = analogRead(A0);
// map the light readings to the angle possible by the servo motor
lightValue = map(lightValue,0,1023,0,180);
// control the servo motor based on the light value read, adjust linearly by angles
servo1.write (lightValue);
}
How would I add this into into my code?
Use the map function to scale this range to a minimum of 200 and a maximum of 600. Then use an LED or LEDs and the servo to keep this mapped input from the photo resistor between 350 and 450. The LEDs will help you do this because they can provide light to the photo resistor if it gets too dark.
Is this a homework assignment? If so, ask the instructor what the servo is supposed to do.
Don't try to run a servo from the Arduino 5V, as that can destroy the Arduino. Use a 4xAA battery pack to power the servo, and be sure to connect the grounds.
Will im trying to shine a light on it (making the shade cover the photoresistor) and when I dim the lights in my room (making the LED's turn on). While this is happening I should be printing the value of the analogread from the photoresistor to the serial monitor...and this value should stay between 350 and 450.
I wouldn't be here if my Professor wasn't already half way into wanting his summer break already. His office hours aren't readily open and he is vague with his response, leaving most of his students in the dark.
Will we weren't told to use a battery or anything besides what we got in our kit. not to mention most of our projects are just given and not much explaining, this has much more information, but kind of threw us in the pit without notice this week.
//include the servo library
#include<Servo.h>
#define RED 5
//create a servo object called servo1
Servo servo1;
int pos = 0; // variable to store the servo position
const int ledPin = 13; //the number of the LED pin
const int ldrPin = A0; //the number of the LDR pin
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //initialize the LED pin as an output
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
// set the servo pin, pin 9, as an servo output pin
servo1.attach(9);
}
void loop() {
int lightValue, pos;
//Read the light value from photoresistor
lightValue = analogRead(A0);
// map the RANGE of light readings to the angle RANGE of the servo
pos = map (lightValue, 100, 600, 10, 160);
// control the servo motor based on the light value read, adjust linearly by angles
servo1.write (pos);
int ldrStatus = analogRead(ldrPin); //read the status of the LDR value
//check if the LDR status is <= 300
//if it is, the LED is HIGH
if (ldrStatus <=300) {
digitalWrite(ledPin, HIGH); //turn LED on
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(ledPin, LOW); //turn LED off
Serial.println("---------------");
}
delay(300); //wait a bit, allow the servo to reach position
}
If you can at least help me with this part, I'll switch over to the bat. just need to see why my servo isn't responding to the light or LED, not covering the Photoresistor when the LED is on
well my servo is working now, the LED remains off when ther servo is overing the Photo and my map isn't working. And range? Im sorry, i've been working on this since I got off work, about 4 hours now.
//Read the light value from photoresistor
lightValue = analogRead(A0);
Serial.print(”lightValue = “);
Serial.println(lightValue);
// map the RANGE of light readings to the angle RANGE of the servo
pos = map (lightValue, 100, 600, 10, 160);
Serial.print(”pos = “);
Serial.println(pos);
it's currently 0, 600, 0, 460.
0 starting when it reaches 600 im guessing, servo removes itself from giving shade and once the light leaves it goes bakc to 460? We just learned map last week, still fresh to me. But those are high and low values correct?
Last question becuase I think I figured it out and just now it's my map that shows no indication of brightness or decress in brightness, after inputting the code you gave, i got a error stray/342
Nvm I figured it out, had to type things out instead of copying and pasting. But the servo now isn't working. Oof but the map works now.
//include the servo library
#include<Servo.h>
#define RED 5
//create a servo object called servo1
Servo servo1;
int pos = 0; // variable to store the servo position
const int ledPin = 13; //the number of the LED pin
const int ldrPin = A0; //the number of the LDR pin
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //initialize the LED pin as an output
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
// set the servo pin, pin 9, as an servo output pin
servo1.attach(9);
}
void loop() {
int lightValue, pos;
//Read the light value from photoresistor
lightValue = analogRead(A0);
Serial.print("lightValue = ");
Serial.println(lightValue);
pos = map (lightValue, 0, 600, 0, 480);
Serial.print("pos = ");
Serial.print(pos);
int ldrStatus = analogRead(ldrPin); //read the status of the LDR value
//check if the LDR status is <= 300
//if it is, the LED is HIGH
if (ldrStatus <=300) {
digitalWrite(ledPin, HIGH); //turn LED on
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(ledPin, LOW); //turn LED off
Serial.println("-");
}
delay(300); //wait a bit, allow the servo to reach position
}
//include the servo library
#include<Servo.h>
#define RED 5
//create a servo object called servo1
Servo servo1;
int pos = 0; // variable to store the servo position
const int ledPin = 13; //the number of the LED pin
const int ldrPin = A0; //the number of the LDR pin
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //initialize the LED pin as an output
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
// set the servo pin, pin 9, as an servo output pin
servo1.attach(9);
}
void loop()
{
//Read the light value from photoresistor
int lightValue = analogRead(ldrPin);
Serial.print("\n lightValue = ");
Serial.println(lightValue);
int pos = map (lightValue, 0, 600, 0, 480);
Serial.print("pos = ");
Serial.println(pos);
//**********************************
//check if the LDR status is <= 300
//if it is, the LED is HIGH
int ldrStatus = analogRead(ldrPin); //read the status of the LDR value
if (ldrStatus <= 300)
{
digitalWrite(ledPin, HIGH); //turn LED on
Serial.println("LDR is DARK, LED is ON ");
}
else
{
digitalWrite(ledPin, LOW); //turn LED off
Serial.println("LDR is LIGHT, LED is OFF ");
}
delay(1000); //wait a bit, allow the servo to reach position
}