Question about photocell sensitivity and servo

Hey all i am an absolute complete novice of all this just wondering if any one can help me out
im using a Arduino nano

Q1 Is it possibile to have a photocell move a servo in one hit (see light moves to positon no light moves back) instead of reading the amount of light and moving accordingly

Q2 Is it also possibile for a digispark attiny85 to do the same knowing i need different coding

Code im using for Nano

#include <Servo.h>

Servo servo;

int photocellPin = 0;
int servoPin = 9;
int pos = 0;

void setup()
{
  Serial.begin(9600);
  servo.attach(9);
}

void loop()
{
  Serial.print("Cell = ");
  Serial.println(analogRead(photocellPin));

  pos = analogRead(photocellPin);
  pos = constrain(pos, 220, 740);

  int servoPos = map(pos, 220, 740, 255, 0);
  int servoDegree = map(servoPos, 255, 0, 45, 0);
  servo.write(servoDegree);
  Serial.print("Servo Degree = ");
  Serial.println(servoDegree);

}

is it possibile to use the code for the digispark but just change and pin numbers
#include <SoftRcPulseOut.h>

SoftRcPulseOut servo;

Thanks
Scott

Q1 Is it possibile to have a photocell move a servo in one hit (see light moves to positon no light moves back) instead of reading the amount of light and moving accordingly

Don’t get what you are asking there? What behavior do you actually want?

Q1: Yes, but... The photocell is going to give you an analog value, but there's no reason you can't react to that in a digital way. If the value is greater than some number then do something else do something else. Usually you add a little hysteresis so that when it is right on the line it doesn't flicker back and forth. But you do have to pick a number to divide the light from the dark. How dark is dark? It's never truly dark.

Q2: Absolutely. The code really isn't going to change much at all.

Ah - with Delta_G answer I see - are you asking to be able to do this without an arduino, in hardware?

It’s possible to indeed drive a relay from a photocell - see how IR digital sensors are built (cf for example this first hit on google search)

Commercial PIR modules have a built in comparator and potentiometer to define the triggering threshold and they also have a potentiometer for duration, which is a way to deal with hysteresis - most design are open source so you can read about that on line

You can see other ideas online too

Cheers might look into going the analog value way might be a bit easier will let u now how things go

Delta_G:
Q1: Yes, but... The photocell is going to give you an analog value, but there's no reason you can't react to that in a digital way. If the value is greater than some number then do something else do something else. Usually you add a little hysteresis so that when it is right on the line it doesn't flicker back and forth. But you do have to pick a number to divide the light from the dark. How dark is dark? It's never truly dark.

Q2: Absolutely. The code really isn't going to change much at all.

Ok i gave it a crack and honestly i have no idea what im doing. i looked up using analog values but couldnt find anything that would help all ive been doing is copy and paste then mucking around with the codes ive also tried putting a servo stop command in to with no luck. As for the digispark the code i used but just changed it to doesnt work

 #include <SimpleServo.h>


SimpleServo servo;

int photocellPin = 0;
int servoPin = 1;
int pos = 0;

void setup()
{
  servo.attach(1);
}

void loop()
{
  Serial.print("Cell = ");
  Serial.println(analogRead(photocellPin));

  pos = analogRead(photocellPin);
  pos = constrain(pos, 220, 740);

  int servoPos = map(pos, 220, 740, 255, 0);
  int servoDegree = map(servoPos, 255, 0, 45, 0);
  servo.write(servoDegree);
  Serial.print("Servo Degree = ");
  Serial.println(servoDegree);

} [code] 

 
any help would be super appreciative

You are missing a Serial.begin(115200); in the setup() for your prints to work (console at 115200 of course)

You don’t want to attach the servo to pin #1, it’s the serial communication. Don’t use 0 nor 1 on the digital side. Better use A0 to be very clear this is the analog input and not the digital input for the photocell. Confirm you indeed connected to A0 and not D0

The pins are constant, so define them with [color=green]const byte[/color] photocellPin = A0; for example

You should have a delay to wait for the time it takes for your servo to physically move. It’s not instantaneous and your code will spin hundreds of time per seconds

pos does no need to be a global variable since it’s only used in the loop(), remove clutter and define it there

You read the analog value twice. If you want to print it, read it first into a variable and print that variable and then use the variable to do the math to decide what angle should be used for the servo

The way this is coded is that the angle will be proportional to the lighting - is that what you wanted? Or did you want either full “close” or full “open” based on a light threshold?

====== also please read how to use the forum =======

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Thanks ill have another go at it

Thanks for your help ive got the nano how i want it but cant seem to get anything out of the digispark

#include <SoftRcPulseOut.h>

SoftRcPulseOut servo;

const byte photocellPin = A0;
int servoPin = 3;

void setup()
{
 Serial.begin(115200);
 servo.attach(3);
}

void loop()
{
 Serial.print("Cell = ");

 pos = analogRead(photocellPin);
 pos = constrain(pos, 220, 740);

 int servoPos = map(pos, 220, 740, 255, 0);
 int servoDegree = map(servoPos, 255, 0, 90, 0);
 servo.write(servoDegree);
 Serial.print("Servo Degree = ");
 Serial.println(servoDegree);

}
 Serial.print("Cell = ");

What cell? Equals what?

but cant seem to get anything out of the digispark

What is "the digispark" and how is it connected to the Arduino?

digispark attiny85 not connected at all to the arduino. I found a code online for the arduino and moded it for my needs did u read the entire posts or just skip to the bottom

digispark attiny85 not connected at all to the arduino. I found a code online for the arduino and moded it for my needs did u read the entire posts or just skip to the bottom

I read the last post. It contained some code that clearly has NOTHING to do the problem. Why did you post that code?

? Did u read all post or just last one not sure what you’re getting at I posted that because that’s were im at i was working with one code for 2 different boards an arduino nano and the digispark ive worked out the nano but still the digi I’m having trouble with please read through post as u will understand i have not much of a clue what im really doing as I’ve been mucking around with these for just over 2weeks and its just for 1 project
Thanks

but still the digi I'm having trouble with

So, the code we would need to see is not the code that is working perfectly on some other hardware, is it?

i have not much of a clue what im really doing

Ah. I see.

No need to be a smart arse mate thats the code in working on for the digispark READ back thoroughly to get a understanding of where im at after weeks before getting the 2 arduinos I googled alot before joining this fourm just for some help
The art of getting a good answers lies in patronising overs