Hello everyone, I hope you are doing well. Over these past few days I have attempted to code a day and night cycle using timers, delays etc. however none of these solutions have worked, I am still a big Arduino noob.
What I would like to do is control, 2 servo groups separately using 2 groups of photoresistors. So basicially I would like an effect where the 'Morning Group' of servos react to light under some timer, and a 'Night Group' reacting in a similar fashion, without the Morning Group interupting. I hope this makes sense lol.
Thank you for your time, all help is appreciated.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo 2
// twelve servo objects can be created on most boards
int sensorPin1 = A0; // the number of the sensor pin
int sensorPin2 = A1; // the number of the sensor pin
int morningServo = 9;
int nightServo = 8;
int sensorValue = 0; // sensor values
int val; // variable to read the value from the analog pin
int servoGrad = 90; // servo grad level
int tolerance = 40; // light sensor tolerance
void setup() {
myservo1.attach(8); // attaches the servo on pin 8 to the servo object
pinMode( sensorPin1, INPUT);
pinMode(buttonPin, INPUT);
myservo1.attach( nightServo );
myservo1.write( servoGrad );
myservo2.attach(9); // attaches the servo on pin 8 to the servo object
pinMode( sensorPin2, INPUT);
myservo2.attach( morningServo );
myservo2.write( servoGrad );
}
void loop() {
morningState ();
nightState ();
}
void morningState (){
sensorValue = analogRead(sensorPin1);
{
if ( sensorValue < (512-tolerance) )
{
if (servoGrad < 180) servoGrad++;
}
if ( sensorValue > (512+tolerance) )
{
if (servoGrad > 0) servoGrad--;
}
}
myservo2.write( servoGrad );
}
void nightState (){
sensorValue = analogRead(sensorPin2);
{
if ( sensorValue < (512-tolerance) )
{
if (servoGrad < 180) servoGrad++;
}
if ( sensorValue > (512+tolerance) )
{
if (servoGrad > 0) servoGrad--;
}
}
myservo1.write( servoGrad );
}
Your program does not compile, no definition of buttonPin. What is buttonPin for anyway?
What Arduino? Why do you attach() the servos twice? What exactly are your conditions for it being Night or Day? Just time of day or some measure of the external light? If time of day it will be a lot easier to use an RTC (Real Time Clock). Most Arduinos are not able to keep time accurately over long periods on their own.
I forgot to remove the push button code so that is my fault, apologies for the confusion.
The Arduino that I am using is an Uno.
What I was trying to do is actually is separate a servo group for the day cycle, and separate a group for the night cycle. A time of day measurement would be fantastic, I will look into this RTC thing!
I have another question, is there a way that I could use photo resistors , and send the information to one servo motor group? For example the day group.
jacobsm:
I have another question, is there a way that I could use photo resistors , and send the information to one servo motor group? For example the day group.
Probably. It's easy enough to make such information available and to have only some servos act on it. But how many LDRs and what do you expect the servo(s) to do with the information?
I would like for there to be 2 LDRs, in the code that I posted above I have made it so that the servos actually move towards the light for example, if the light is coming from the left side, the servo would rotate left and very much the same for the right side.
I have tested this before and it does work, but for some odd reason I am not able to move separate groups this way!
As usual, if you show us what you tried we might be able to help explain the problem. The only code I've seen so far has only 2 servos and no visible sign of day/night groups or LDRs.
Thank you for the diagram, I have not seen one like this before actually, again sorry I am really a starter to all this Arduino stuff! I will investigate this diagram now