Servo activated by light spikes/flashes

I’ve written a code for what my circuit should do, but sadly I can’t make it work in practice. The code is this: #include <Servo.h>Servo myservo;int LDR = 0; // light sensor connected to - Pastebin.com
EDIT:

#include <Servo.h>
Servo myservo;
 
int LDR = 0; // light sensor connected to A0
int prevValue = 0; // reference point
int curValue = 0; // to check against prevValue
void setup() {
  myservo.attach(13);  
  pinMode(LDR, INPUT); //put light sensor as input
  myservo.write(140); // put servo in starting position
  Serial.begin(9600); // speed to interact with Arduino
  curValue = analogRead(LDR); //initialize curValue
}
 
void loop() {
  prevValue = curValue; // put prevValue to reference point
  curValue = analogRead(LDR); // get new value for light sensor
  Serial.println(curValue); //print sensor data (0-1023)
  if(curValue - prevValue >= 20) { // if light spikes up
    myservo.write(180); //move servo to button
    delay(400); //hold servo in current position
    myservo.write(140); //move servo back to initial position
  }
  delay(100); // wait a tenth of a second for new data
}

Funny thing is I can make the LDR work seperately without errors as well as the servo seperately without errors, but when combined I get a lot of oscillation from the sensor. Basically getting readings like a sine function. I’m sorry for not having schematics for what I tried, so I’ll just try to explain the best I can even though I’d rather you create a circuit without this knowledge.

Arduino Uno connected to USB for power. 5v and ground to breadboard. Servo to 5v and ground on breadboard and signal to/from pin 13. 5v breadboard to LDR to pin A0 with a 10k ohm resistor from 2nd LDR pin to ground.

I’m willing to buy more components if necessary (already have a short list of things I need to buy (capacitors, transistors and a much needed multimeter for troubleshooting)).

Also, I checked the following: No electronics nearby that could cause the oscillation. LDR and servo work as intended on their own, so those are not broken.

Why not post your code as outlined in the "how to use the forum" stickies? It makes it much easier on those who would want to help you.

Fixed the syntax

when combined I get a lot of oscillation from the sensor.

That is because you are trying to run the servo off the same power supply as the rest of the Arduino. Servos generate interference and this is causing the errors. So either:- 1) have a separate power supply for the servo, but remember to connect the ground of this supply to the ground of the Arduino.

or 2) Use the Pi filter circuit which is the last schematic on this page:- http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Ok, I do have a 9v battery with... how do you call that... some battery holder that on the other end fits in the arduino uno (that round black thing).

Can I connect my servo to 9v battery and the LDR to 5v from arduino without problems? If yes, can I make it work with this battery holder thingy? Or can I get two seperate 5v outputs from one arduino (one from USB, one from the round power source connected to 9v battery).

LDR's have a very slow response, so may not detect short pulses of light. A phototransistor or photodiode would be much faster.

Allan

Can I connect my servo to 9v battery

Most servos are 4.8 to 6V so a 9V battery is too much voltage. Another problem is that even if the servo can handle 9V the 9V transistor (smoke alarm) battery will not provide near the current that a servo requires. A much better servo supply is a 4 AA battery pack.

My servo is this: http://www.micropik.com/PDF/SG90Servo.pdf

Would a simple voltage divider work? - 9v to 10k resistor to another 10k resistor to ground with the servo's 5v between the two resistors.

Would a simple voltage divider work?

No.

groundFungus: No.

I appreciate you answering my question, but for educational purposes (which I clearly need), could you elaborate a bit on the why this wouldn't work.

Because the output impedance of a potential divider is too high to supply the current you need. As a rule of thumb you need ten times the current down the two resistors as you pull out at the bottom to prevent the voltage changing too much as the current changes. The current will constantly changes as circuits switch, producing a large change in voltage.

In practice what you will see is your calculated voltage collapse as you connect the load.

Well, with this I understand even less what WOULD work. I'm already making a big list of components to buy so I don't have to worry too much about workarounds for future circuits. Can someone make a circuit that would work out of components that I either have or should have. So referrably with 9v battery and the servo linked above and if not include holder for batteries and/or which specific servo.

Use a fixed voltage regulator as shown here http://www.circuitstoday.com/fixed-positive-voltage-regulators

Or buy a switching voltage regulator, sometimes called a buck converter, or DC to DC converter, that has an adjustable voltage that covers 6V.

Stop trying to use a 9V square battery, they are rubbish.