Hey all,
I'm trying to create something which will use a photoresistor and a servo motor. Once the photoresistor detects light above 5 on the serial monitor, it should trigger the servo motor. I am able to detect the photoresistor reading through the serial monitor when I run the code.
Here is the code I have so far, but I cant get them to work in unison. Can anyone help me figure it out please?
#include <Servo.h>
int photoPin = A0;
Servo myservo;
int pos = 0;
int light = analogRead(photoPin);
void setup() {
Serial.begin(9600);
}
void loop() {
int light = analogRead(photoPin);
Serial.println(light);
delay(100);
}
void setupB() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loopB() {
if (Serial.println( light > 1))
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
}
A sketch cannot contain two separate setup() functions and two separate loop() functions. Start by merging the code from the bodies of setupB() and loopB() into the bodies of setup() and loop(), respectively. Then use that as a starting point for debugging.
Thank you
I revised it to this code, but the issue now is that the servo is only triggered by the photoresistor once (when the code it uploaded). After that, the photoresistor properly adjusts the readings on the serial monitor, but the motor is not triggered once the value goes above 10
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int photopin1 = A0;
int value = 0;
int threshold = 300;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
value = analogRead(photopin1);
if(value > 10) {
for (pos <= 145; pos <= 75; pos += 15) { // goes from 145 degrees to 75 degrees
// in steps of 15 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
} } else { (value < 5)
;for (pos <= 0; pos <= 90; pos += 15) { // goes from 0 degrees to 90 degrees
// in steps of 15 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
}
Serial.println(value);
delay(20);
}
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int photopin1 = A0;
int value = 0;
int threshold = 300;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
value = analogRead(photopin1);
if (value > 10) {
for (pos <= 145; pos <= 75; pos += 15) { // goes from 145 degrees to 75 degrees
// in steps of 15 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
} else {
(value < 5)
; for (pos <= 0; pos <= 90; pos += 15) { // goes from 0 degrees to 90 degrees
// in steps of 15 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
}
Serial.println(value);
delay(20);
}
What do you expect this to do?
} else {
(value < 5)
; for (pos <= 0; pos <= 90; pos += 15) { // goes from 0 degrees to 90 degrees
Maybe start with something simpler, like what you had in your original post, but merged. Does it work as intended, or not? If not, what is not working as you had intended?
After you get the original code to work, you can add more complexity to the behavior.
You need a full (even hand drawn) schematic of your circuit. Is the photoresistor a typical LDR set up as a voltage divider? Did you calibrate your photoresistor?
What model servo do you have and how are you powering it?
This section of code is a good, separate code snippet to use just to calibrate your sensor, without using any servo output at all. Test it in your anticipated environment, be sure your values are what you think they should be before trying anything with the servo.
After that works, set up your servo alone in another sketch with just the servo function and have it rotate back and forth. Try just using the example in the built in Servo library in the IDE.
When that is done and you are satisfied with just reading the sensor, and just rotating the servo, put the two together to try the entire circuit.
for (pos <= 145; pos <= 75; pos += 15) { // goes from 145 degrees to 75 degrees
// in steps of 15 degrees
Apart from the fact that the sentence is misspelled, there is another detail... 145 - 75 = 70 which is not a multiple of 15 so it will never reach 75°, it will remain at 85°.
The for loop makes zero sense, and does not follow the usual simple pattern.
As written, 145 doesn't enter into it at all, and the pos variable will take on values 0, 15, 30 all the way to 90 once.
Which may be consistent with @ianbn3's observation.
It is likely meant to read
for (pos = 145; pos >= 75; pos -= 15) {
which has the initialization, test and increment sections that form the basis of a simple loop. Where to start; whether to continue; and how to adjust the loop variable.
@ianbn3 review the syntax of the for loop, check your typing… you need to be accurate, the compiler will not guess and correct your intentions.
I think it was not understood that I clarified that the for() statement was misspelled and what I was trying to say is that, regardless, the limits of the servo movement are incorrectly defined