Answer the question
In order to leave comments, you need to log in
It is necessary to combine the work of a photoresistor and a potentiometer. Who will help?
I need to make it so that the servo can be controlled by a photoresistor or a potentiometer, for example, I want it to turn from the light, but if I don’t want it, then from the potentiometer.
wrote this code, but the movement is jerky
# include
Servo nikiServo;
intservoPin=9;
int photoPin = 0; // the first photoresistor is connected to the 0th analog input
int potPin = 2; // second photoresistor
int val1 = 1; // variables for storing the value of the input voltage
int val2 = 1; //
void setup()
{
Serial.begin(9600);
nikiServo.attach(servoPin);
}
void loop()
{
val1 = analogRead(photoPin); // read the value from the photoresistors
val2 = analogRead(potPin);
Serial.print("values: "); // output them to the COM port
Serial.print(val1);
Serial.print(" : ");
Serial println(val2);
delay(500);
if(val1>val2) // if the illumination of one is greater, turn
on { // the corresponding LED, and turn off the second one
int fot=analogRead(photoPin);
int pos=map(fot,490,630,0,180);
nikiServo.write(pos);
}
else
{
int fot=analogRead(potPin);
int pos=map(fot,0,400,0,180);
nikiServo.write(pos);
}
}
Answer the question
In order to leave comments, you need to log in
Tell me, please, in more detail int potPin = 2; // второй фоторезистор
- is it still a photoresistor? Or a potentiometer?
Twitching may well be due to uneven lighting. Those. light hits the photoresistor in different ways, the signal from it is uneven.
What about the option when the value of the potentiometer is less, say 5 (i.e. the voltage coming from the potentiometer is close to zero) we read the photoresistor, if on the potentiometer from 5 to 1023 it comes to the ADC, then we convert this value into an angle angle = map(potPin, 5, 1023, 0, 180)
And how do you know when you want to switch to control from a potentiometer and vice versa?
Now you have: who has abs. more value - he won.
Perhaps, if you need to "correct" the brains of the machine a little and for a short time, then you can remember the value of the potentiometer and compare each poll with the current one. If it has changed, then they switched to manual control. After N seconds of inactivity, return control to the photoresistor, and the smoothness of the control transition will be invented.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question