Hey, so I'm new to programming and Arduino. I'm trying to design apertures that open and close based on light changes caused by shadows that go over the ldr sensor. Due to this, I need the motors to turn a bit more radically. I need them to turn about 90 degrees to open and close the apertures with the change in light intensity being a little from about 650 to about 550.
Right now I have the basic code which just turns it linearly based on the change in light intensity. It only gets to 90 degrees when its really dark but keeps ongoing. Can anyone help me, please! I've attached the code I'm working with atm.
Thanks!
//include the servo library
#include <Servo.h>
Servo servo1; //create a servo object called servo1
int pos = 0;
int ldr1 = A0; // select the input pin for LDR
int ldrValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //
servo1.attach(9); // servo output pin
}
void loop() {
ldrValue = analogRead(ldr1);
Serial.println(ldrValue); //prints the values coming from the sensor on the screen
ldrValue = map (ldrValue, 0, 700, 0, 180); // map the light readings to the angle possible by the servo motor
// control the servo motor based on the light value read, adjust linearly by angles
servo1.write (ldrValue);
}
What you wrote in your code is that you want the servo to 0 when the LRD reads 550 and to 90 when it reads 650. But that's not anything like what you wrote in your code. In your code you wrote:
ldrValue = map (ldrValue, 0, 700, 0, 180);
That puts 0 degrees at a reading of 0 and 180 degrees at a reading of 700. Just fix those numbers to the ones you described earlier in the description. Make 0 degrees 550 and 90 degrees 650.
Then you will also need to use constrain as well. Because if the value from the LDR goes outside of 550-650 then the map function will find a value outside of 0 to 90 to match it to. Map works like that, it extends the range. So you'll also need constrain to keep the value written to the servo to between 0 and 90.
Oh, thank you I wasn't too sure of that part of the code, so thanks for explaining it.
So I put it in as
ldrValue = map (ldrValue, 550, 650, 0, 90);
How do I constrain it then to work on higher or lower values? Would that be the if and if else commands? I remember seeing something like that somewhere.
I would rather if I could code it for the servo to go 90 if the change in light level changes by a minimum of 10. If it increases by 10 close and if it decreases by 10 open. Is there a way for me to program it as such?
LoL, thank you.
So how do I use the constrain () command?
Do I just put
constrain (ldrValue, 550, 650);
Do I need the if commands?
Also, would that also help me program it to rotate by a change of light level of at least 10? So that if it increases by 10 it will close and if it decreases by 10, open
Did you try doing it like the examples on that page? Did you even look at that page? The one for the constrain function? It has examples of how to use the function on that page.
Sorry, but did you read the whole post? I read it and below that question, I put the example they had but I'm trying to figure out the placement of the code and all. I'm also asking about and you're not answering my follow up question and that's what I'm trying to figure out. If constrain can work in the same way, cause to me that sounds a bit different from constrain.
Especially since I know other values might come up but I still need the servo to move. Cause right now since I'm testing it in a building and the values are low, but it's going to be operational outside where the light values are more intense and could fluctuate due to clouds. That's why I'm asking about a difference of 10 in the light intensities... Do you understand what I'm asking now?
So that way it would open if the light intensity changes by 10 or more. Constrain only looks like it would work on a particular range but I need it to still work if the range changes.... Do you understand?
You wrote what is at the top of the page. If you read on down it says how to use that function in code. I did read your whole question. Did you read the whole link? Cause from your question it looks like you didn’t.
Or you can just use if statements. It’s not esoteric. Say it out in English, it works the same in code.
...I appreciate you trying to help. but if you don't want to help you don't have to. I'm new to this and you're directing me somewhere like I'm a programmer. I'm an architecture student, this is not my field but I need to program this for my project. Giving me bits and pieces are awfully confusing.
Include what you're saying in my code cause I don't know the placements and all. You're talking to me like I should know this but I do not.
Everything is hard cause you're just giving me pieces and I don't know how they fit... like where does the if go? do I still need the constrain if using if...like whats going on?
Please if you want to be rude, you don't have to help me but I appreciate everything you've helped with so far.
Where does it go? Code runs like a set of instructions from the top to the bottom. It would make most sense to me to put that line either right after you give the variable a value or right before you use it. So find the line of code that gives that variable a value. If you don’t know how to find that then you’re in over your head and you need to put this down a while and study the basics first. Once you find where you put a value to that variable then right after that fix the value to be within the limits.
You’re think I’m talking to you like a programmer? I look at the code you’ve got and expect that you know something. If that’s just a copy and paste job then you’ll have to find some code that already does what you want to copy and paste.
Thank you. Hopefully, someone who wants to help me comes over and not someone making me feel bad for not knowing what to do when I'm asking for help...
Your time has been much appreciated.
From the help previously given I've come up with this code but I'm trying to understand if I can set instructions for the servo to move if the light intensity changes by at least 10. My only issue is I'm thinking if it's used outside and the clouds affect the servo rotations due to light intensity changing by clouds. I want the light intensity to change based on shadows cast by people only. Can I fix it so it opens if the light variable changes by 10 or so, holds its position for a couple of secs and then recalibrates its max intensity in the case the overall light intensity changes? Hopefully, that makes sense
This is the code so far...
//include the servo library
#include <Servo.h>
Servo servo1; //create a servo object called servo1
int pos = 0;
int ldr1 = A0; // select the input pin for LDR
int ldrValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //
servo1.attach(9); // servo output pin
}
void loop() {
ldrValue = analogRead(ldr1);
Serial.println(ldrValue); //prints the values coming from the sensor on the screen
ldrValue = map (ldrValue, 550, 650, 0, 90); // map the light readings to the angle possible by the servo motor
constrain (ldrValue,550, 650)
// control the servo motor based on the light value read, adjust linearly by angles
servo1.write (ldrValue);
}