Hi everyone, I am new to this forum and to Arduino and to C++ but I am really looking forward to learning more and becoming more involved here as it is something I have wanted to do for quite some time!
I am currently working on a project that would open and close my blinds depending on the amount of light outside. I hate sleeping with the blinds open but love waking up to light coming in through the windows.
The problem I am having is mainly due to my total lack of experience but I am hoping this project is simple yet complicated enough to be a good intro for me (I have already done all of the simple tutorials like blink and such). The part I am currently working on is seemingly very simple although I am having trouble executing it and I have plans to add more complicated functionality later on (like multiple modes, bluetooth smartphone control and an indoor facing motion sensor).
I am currently going off of various beginnings of tutorials and forum posts from a variety of sources and don't have much knowledge of the platform. I most definitely look forward to learning more about this summer, which I plan on dedicating to learning C++ and Arduino.
Anyway, on to the project =>>> ((nothing above this really matters to this problem, sorry))
I am currently using an Arduino UNO R3, a LDR, a 10K resistor and a FeeTech (FS5103R) continuously rotating servo motor. LINK TO SERVO SPECS
The setup so far is very simple and these are the schematics I followed to set up the circuit (except I used Pin 11 for the servo signal pin):
I had the code to the point where the servo would turn one way when it was exposed to light and the other when I blocked the light with my hand but it was always continuously rotating at different speeds depending upon the amount of light unless I blocked just the right amount of light to write a "90" to the servo signal pin. Although this is kind of mesmerizing and entertaining, it isn't what I was shooting for.
The goal is:
- Get the servo to rotate 3 full rotations one direction when the LDR senses light (sunrise)
- Wait for the light to go away (sunset)
- Turn 3 full rotations in the other direction.
(3 full rotations is how many times you need to turn the rod on blinds to open/close them)
I have tried to modify the code to make the servo only turn one way or the other way (at one constant speed) depending on the amount of light hitting the LDR but it isn't working.
Here is the code I started with from an Instructables article:
#include <Servo.h>
Servo myservo;
int val;
void setup()
{
myservo.attach(12);
}
void loop()
{
val = analogRead(0);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}
And here is what I am currently working with. Again, I based it off of the above code but made some changes to var names and such to help make it less confusing and also attempted the if else statements. I do not see why this doesn't work. I added comments to the code the best I could.
#include <Servo.h>
Servo myservo; //name servo
int photocellPin = 0; //define photo cell reading input
int photocellReading; //define photo cell reading variable
void setup()
{
Serial.begin(9600); //initiate serial @ 9600 baud
myservo.attach(11); //define pin 11 as servo signal pin
}
void loop()
{
Serial.print("Brightness = "); //print "Brightness = "
Serial.println(photocellReading); //print the photocell reading
photocellReading = analogRead(photocellPin); //define photocellReading as pin 0 input from LDR
photocellReading = map(photocellReading, 0, 1023, 0, 179); //map the LDR input to a value between 1-180 so the servo can understand it
{
if (photocellReading <= 89); //if the LDR is showing less than half light intensity
myservo.write(50); //then tell the servo to rotate backwards at a steady rate
else (photocellReading >= 91) //if the LDR is showing more than half light intensity
myservo.write(130); //then tell the servo to rotate forwards at a steady rate
delay(15);
}
}
So I know this is probably a super simple solution but I am hoping to get some pointers and help so that I will be able to learn by example. I plan to keep this thread open until the project is done (with all the fancy features) no matter how long it takes so it might be an interesting learning log on top of helping out an aspiring maker.
Thanks in advance for your time and sorry this post was so long winded!
Regards,
Sky