roundhouse door openers

I am trying to get an Uno to open and close doors on my g scale railroad roundhouse. Basically it has 2 doors in each of the 4 bays. one opens CW and one opens CCW. The intent is for a photo resistor to sense the lights are on in the bay and to open the doors (at the same time). and then close the doors when the lights go out. Using the sample code for the servo only (SG90) and some music wire attached to the door I can get a door to cycle open and closed. The problem is happening when I integrate the photo resistor and the “while statements”. The loop() does not seem to cycle around. The buzzing is also an issue for long term. The degrees setting on the servo will hopefully correct this.

Here is the code I am at right now. It is just for the Far Left Bay Clockwise Door. Figured I would get one to work and then add the other seven with different names, etc. The light level setpoints will need to be adjusted when all is installed in the roundhouse. I have a full size mock up of one bay in my den. I need the values of the light level to set the trip points is why the serial println is there. I have noticed that the COM5 only displays the light level once and when covered up (dark) nothing happens. The value for opening and closing are just trial values and the final setup will vary these as well.

appreciate the help.

/*
PHOTO RESISTOR

Use a photo resistor (light sensor) to control the operation of two servos.

Hardware connections:

Photo resistor:

Connect one side of the photoresistor to 5 Volts (5V).
Connect the other side of the photoresistor to ANALOG pin 0.
Connect a 10K resistor between ANALOG pin 0 and GND.

*/

const int sensorPin = 0;
int lightLevel, high = 0, low = 1023;
#include <Servo.h> // servo library
Servo servoFLCW; // servo control object

void setup()

{ Serial.begin(9600);
servoFLCW.attach(2);
}
void loop()
{lightLevel = analogRead(sensorPin);
while ((lightLevel) > 10)
{Serial.println(lightLevel);
delay(5000);
doorFLCW();
}
}
void doorFLCW()
{lightLevel = analogRead(sensorPin);
while ((lightLevel) < 400)
{analogWrite(2,5);
}
while ((lightLevel) > 500)
{analogWrite(2,30);
}
}

Q: might not seem close related.... but what voltage is the light in the house?

could use that to trigger open and close..

but looking at the code to spot the culprit

The light voltage is a completely different circuit. They are wired to the track which supplies 0-18 VDC to power the locomotive.

first thing noted....

int lightLevel, high = 0, low = 1023;

you should not need this one as you later on read the level and make a action depending on that

next about printing light level... i assume you want it to print light level all the time?

{Serial.println(lightLevel); delay(5000); doorFLCW(); }

you have put a loop inside the main loop... but this one is only trigered as long light level is above 10.... why not make it print all the time and make an if statement that controls the doors only?

void loop(){

lightLevel = analogRead(sensorPin); Serial.println(lightLevel); if lightLevel > 10 { delay(5000); doorFLCW(); } }

the new main loop will now read the analog value then print it if the value is above 10 it will jump to the door open loop

i redid the door open loop

void doorFLCW() {

lightLevel = analogRead(sensorPin);
Serial.println(lightLevel);

if lightLevel < 400

{
analogWrite(2,5);
}

else

analogWrite(2,30);

}

it will first read light level
then print it

if the light level is below 400 it will close the door

if not (else) it will open the door

boelle:
i redid the door open loop

void doorFLCW() {

lightLevel = analogRead(sensorPin);
Serial.println(lightLevel);

if lightLevel < 400

{
analogWrite(2,5);
}

else

analogWrite(2,30);

}

it will first read light level
then print it

if the light level is below 400 it will close the door

if not (else) it will open the door

So your problem is sorted?

we are getting close... i'm running a bit of fault search on my own suggestion

10 mins and i will post complete sketch

/*
PHOTO RESISTOR

Use a photo resistor (light sensor) to control the operation of two servos.

Hardware connections:

Photo resistor:

Connect one side of the photoresistor to 5 Volts (5V).
Connect the other side of the photoresistor to ANALOG pin 0.
Connect a 10K resistor between ANALOG pin 0 and GND.
Servo attached to digital pin 2

*/

const int sensorPin = A0;
int lightLevel = 0;
#include <Servo.h> // servo library
Servo servoFLCW; // servo control object

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
servoFLCW.attach(2);
}

void loop(){

lightLevel = analogRead(sensorPin);
Serial.println(lightLevel);

if ((lightLevel) > 10);

{
delay(5000);
doorFLCW();
}
}

void doorFLCW() {

lightLevel = analogRead(sensorPin);
Serial.println(lightLevel);

if ((lightLevel) < 400) {;

analogWrite(2,5);

}

else

{

analogWrite(2,30);

}
}

Give me a few and I will try your if, else statements and give it a go.

what this does

sensorpin is set to analog 0... if not prefixing with A it will think digital 0

lightlevel is then initiallized so to speak with the value of 0.... if you dont do this it will say its not declared when you try to use it

you then include the servo lib and name the servo servoFLCW

then you start the serial port at 9600 baud and attach the servo to digital pin 2...

in main loop it will read the lightlevel and put the value in variable lightlevel it then prints the value of lightlevel to serial.

now if the light level is above 10 it will jump to the door loop... if light level is below 10 it will just stay in the main loop...

in the door loop it will again read the lightlevel and print it to serial if the light level is below 400 it will close the door if above it will open

oh... have to check if servo needs a pwm pin.... hold on

yes i think we are missing some setup here... reading some examples

when running it is now "printing" various light which is good.

but the servo is not moving and occasionally the computer sounds like it is loosing the COM port. Beeping like when you manually connect and disconnect. "Windows 7"

nope... i figured what is wrong with the servo part...

its a simple naming issue...

let me update the code and post it...

/*
PHOTO RESISTOR

Use a photo resistor (light sensor) to control the operation of two servos.

Hardware connections:

Photo resistor:

Connect one side of the photoresistor to 5 Volts (5V).
Connect the other side of the photoresistor to ANALOG pin 0.
Connect a 10K resistor between ANALOG pin 0 and GND.
Servo attached to digital pin 2

*/

const int sensorPin = A0;
int lightLevel = 0;
#include <Servo.h> // servo library
Servo servoFLCW; // servo control object

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
servoFLCW.attach(2);
}

void loop(){

lightLevel = analogRead(sensorPin);
Serial.println(lightLevel);

if ((lightLevel) > 10);

{
delay(5000);
doorFLCW();
}
}

void doorFLCW() {

lightLevel = analogRead(sensorPin);
Serial.println(lightLevel);

if ((lightLevel) < 400) {;

servoFLCW.Write(45);

}

else

{

servoFLCW.Write(90);

}
}

before you did an analog write to the digital pin....

from an example here: http://www.hobbytronics.co.uk/arduino-tutorial2-servos

i found that you just tell the servo object what degree you want...

i just picked 45 and 90 as examples... i guess then need some fine tune so it does not try and move the door past closed...

how are your servo connected? 5V and GND is spec interesting

Now it says in error code space

'class Servo' has no member named 'Write'

THe servo is connect to the UNO using the 5V and GND from it connected via USB to laptop.

you need to connect the servos to 5V seperate supply...

they take to much power for arduino to power it... and its also to much power for the usb...

let me check code for errors