What would that be? Your code does something that you've kept a secret. You expect it to do something, but you've kept that a secret, too.
int ldrPin = A0;
int ldrPin2 = A1;
When you start numbering variables, you need to number ALL "related" variables.
int ldr1 = 0;
int ldr2 = 0;
like you did here.
Serial.println("PIN1");
Serial.println(analogRead(ldrPin));
Serial.println("PIN2");
Serial.println(analogRead(ldrPin2));
int val = analogRead(ldrPin);
int val2 = analogRead(ldrPin2);
Printing one value and acting on another is not the mark of the sharpest crayon in the box.
val = constrain(val, 0, 1023);
val2 = constrain(val2, 0, 1023);
Constraining the values after you've used them is pointless, isn't it?
if(ldr1<350 && ldr2<350) // Night Mode
In my experience, it is either night or day. Moving the servos to the day position, and then discovering that it is night and moving them again is not the proper sequence of events.
You need to decide whether it is night or day, and then move the servos appropriately for each mode.
Constraining the values after you've used them is pointless, isn't it?
Yes, I understand. I looked at the description of the "constrain" command here and there in the example this code was specified: sensVal = constrain(sensVal, 10, 150);
I made some changes in my code.
#include <Servo.h>
Servo hServo;
Servo vServo;
int ldrPin = 0;
int ldrPin2 = 1;
void setup()
{
Serial.begin(9600);
hServo.attach(11);
vServo.attach(10);
}
void loop()
{
Serial.println("PIN1");
Serial.println(analogRead(ldrPin));
Serial.println("PIN2");
Serial.println(analogRead(ldrPin2));
int val = analogRead(ldrPin);
int val2 = analogRead(ldrPin2);
delay(100);
int Angle = map(val, 1023, 230, 165, 0);
hServo.write(Angle);
int Angle2 = map(val2, 1023, 230, 82, 35);
vServo.write(Angle2);
val = constrain(val, 1023, 230);
val2 = constrain(val2, 1023, 230);
}
You are using contrain(val ...)after you use map(val ...). Doing it like that means that it will have no effect. You constrain the values of val and val2, the throw the constrained values away. Why would you want to do that?
You are getting a new value, on every pass through loop(). Then, you are mapping that value, to a different range. Then, you tell the servo to move to that position. Then, you constrain the value to be in some range, and then throw the constrained value away (because loop() ends, and the variable goes out of scope).
I see no value in calling constrain(), unless you do it BEFORE you use the (constrained) value.
Lets say that you got a reading from the LDR that was 112. map() is, because the value is not in the from range, going to map it to a value that is outside of the to range, resulting in some negative value around -20 maybe; I didn't crunch the numbers), which you write to the servo. Oops. Then, you constrain the value to be in the range 1023 to 200. constrain() expects the 2nd argument to be less than the third argument. It will check that the input value (-20, maybe) is greater than or equal to the 2nd argument (it isn't) and less than the third argument. If it is not greater than or equal to the second argument, it will make it equal to the second argument, 1023.
You need to constrain the value to be in the from range before mapping (with the values in the proper order) OR constrain the value to be in the to range (again in the proper order) after mapping, BEFORE you use the mapped/constrained value, or don't bother constraining.
More interesting about this constrain is that it's redundant to begin with: analogRead() returns a value ranging from 0 to 1023 so constraining it to that range is not going to have any effect, ever.
wvmarle:
More interesting about this constrain is that it's redundant to begin with: analogRead() returns a value ranging from 0 to 1023 so constraining it to that range is not going to have any effect, ever.
OPs from range isn't 0 to 1023, and the range he/she is constraining to isn't 0 to 1023. So, the constrain() isn't useless.
Your Fritzing (using that for lack of a proper circuit diagram) suggests you power the servo off the Arduino. That's asking for problems. Have them powered independently (power supply can be shared with the Arduino but it can't be the Arduino powering the servos), if separate power supply connect the grounds.
acostyle:
I am trying to link 2 LDRs with 2 Servos on my Solar Tracker. 1 LDR = 1 Servo.
Let's go back to basics.
What are the LDRs intended to do?
If they are for the purpose of detecting the direction of the sun then you will need 4 LDRs - two for the horizontal axis and two for the vertical axis. However, to start off it would probably be sufficient to leave the vertical axis fixed and concentrate on the horizontal axis - in which case 2 LDRs will be sufficient for now.
To point your LDRs towards the sun you should have a shield (piece of black cardboard will be fine) between them so that if they are not pointing at the sun one of them will be in shadow.
Then the logic for moving towards the sun is simple. Read the values from the two LDRs and move a little bit towards the one that is brighter. Repeat.
This sort of code is a bad idea
Serial.println("PIN1");
Serial.println(analogRead(ldrPin));
Serial.println("PIN2");
Serial.println(analogRead(ldrPin2));
int val = analogRead(ldrPin);
int val2 = analogRead(ldrPin2);
because the values that go into the variables may be different from the values that are printed. Much better to do it like this
int val = analogRead(ldrPin);
int val2 = analogRead(ldrPin2);
Serial.println("PIN1");
Serial.println(val);
Serial.println("PIN2");
Serial.println(val2);