Hi, mate.
You are gettig there.
I like the inclusion of the constraint.. function as the map is just a simple linear formula for a striaght line and calculates out side the sample points as well as inside.
I have had a edit of your code, to show how to use the map function and transfer it to servo.
If you open the IDE monitor and select 9600 baud, the displaytomonitor function will display your LDR and Servo Angle values.
I have slowed the loop to loop every 500ms, but you can play with that to slow or speed it up.
You may find the servo will jitter, if so then there will need to be some more code written.
I hope I have commented it enough for you to understand and play with.
The 260 , 900 and 0 , 180 values in map may have to be adjusted to suit your servo range.
#include <Servo.h>
Servo servo1;
int photoresInpPin = A0;
int photoresVal = 0;
int angulo = 0;
int servo1Pin = 9;
void setup()
{
Serial.begin(9600);
servo1.attach(servo1Pin);
}
void loop()
{
photoresVal = analogRead( photoresInpPin); // read the LDR
angulo = map(photoresVal, 260, 900, 0, 180); // maps 260 to 0deg all the way to 900 to 180deg
angulo = constrain(angulo, 0, 180); // constrains the value of angulo
servo1.write(angulo); // commands the servo
dipsplaytomonitor();
delay(500); // slows the read cycle
}
//===============================
void dipsplaytomonitor() // function to display values
{
Serial.print("LDR Value \t"); // the \t are the equivalent of tab instructions
Serial.print(photoresVal);
Serial.print("\t Servo Angle \t");
Serial.print(angulo);
Serial.println(" degrees");
}
Tom.... ![]()