Newbie need help with multiple LDR and servo

hi there,

i'm really new to this, and have a presentation tomorrow that i need this for, so any help would be great.

i have connected 5 ldr to the aduino in analog input 1 to 5 (i have attached them using a breadboard and resistors as shown on the website) I have also attached 5 servos to digital 6 to 10

i need to try and rotate to certain angles dependent on light on each ldr.

i am now at a complete loss with the programming code. i have looked at the different types of code (esp. srvo timer 2), and am afraid don't understand it well enough to deliver tomorrow.

can anyone help me work this out.

thank you

You'll need the MegaServo library; the standard servo library won't support more than two servos.

What do you want to do (and why did you leave it so late) ?

What's your programming experience?

thanks for the reply.

programming experience: pretty much zero.

why so late: i was last week working with someone who has worked with this before from another department of the university, but he has not had the time to help further, but unfortunately he didn't tell me there was a problem. so i waited, and here i am the night before.

is the megalibrary a software package for the aduino?

sorry about my lack of knowledge on all this.

regarding what i want to do:

i am creating a prototype for a building skin that has panels on the external surface rotating on the horizontal axis. the respond to the ldr's on an internal wall. over large amounts of panels creating movement responding to the internal light and movement.

for tomorrow i need to prove that the technology works.

hope this helps

At this stage, I'd downgrade your expectations, and just go for getting just about anything working, so start with the standard servo library for now, and if you've got time later, upgrade.

Not clear what you're trying to achieve - point a servo at the darkest or lightest servo?

[edit] simultaneous posts - some sort of louvre/venetian blind thingy?

ahh ok so the standard servo library only does two.

i was looking at the script which was servotimer2 which could do 8, but is that using the megaservolibrary?

the aim is to create large pixelated animations on the skin. howver i would need a lot of these motors, so as a starting point need to prove the technology, then later do a larger sample.

last week with the help of the other individual i was able to present 1 working sample, i think to push upto 2 wouldn't impress too well. but right now i'll take what i can get :)

First start by checking you’re getting sensible readings on your LDRs

#define FirstAnalog 1
#define nAnalog   5

void setup ()
{
  Serial.begin (9600);
}

void loop ()
{
  for (int i = FirstAnalog; i < (FirstAnalog + nAnalog); ++i) {
    short x = analogRead (i);
    Serial.print (x, DEC);
    Serial.print (" ");     
  }  
  Serial.println ("");
  delay (500);
}

If you run this (don’t forget to run the serial monitor at 9600 bps), you should (I haven’t compiled it) get a row of decimal integers corresponding to your LDRs, every half second.
Cover up four of them, and shine a bright light on the other.
You should be able to see easily which is which.

Simple diagnostic program, save it.

OK, now you need to map the LDR readings to servo angles.
Not clear if this is meant to be a linear mapping (ie LDR reading zero, servo angle zero, up to LDR reading 1023, servo angle 180) of something simpler.

Servotimer2 - as in:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230479947

ok, just an that through the programme and it came up with:


Binary sketch size: 2030 bytes (of a 14336 byte maximum)

avrdude: stk500_getsync(): not in sync: resp=0x00 avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51'


i'm guessing there is a problem with my setup. sall i remove all the servos, i have aatched.

i have attached the ldr, as suggested for one ldr in the aduino website, and then repeated it across 4 more times

If the only supply you’ve got is USB, you won’t be able to drive 5 servos properly.
Do you have a battery pack to drive the servos, or a bench supply of some sort?

Do you see the serial LEDs flicker when you download the software?

(Aside: My sketch came in at 2016 byes, but we’ll ignore that for now)

ahh ... ok that is a problem.

the first i can get to power is in the morning, which is 12 hrs from now.

although i can maybe get to a power adapter which has variable volt settings. would that work? i'm pretty sure it is a 5 or 6v setting

Six volts is fine. Just connect the ground ( 0V) to the Arduino, and the plus six volts to the supply pins of the servos. You need probably about 100mA per servo, so anything capable of above 500mA should be OK.

Got the sketch to download yet?

Can you post the sketch for the single servo demo you've already made? It is likely that a couple of "for" loops and a couple of arrays may sort you out.

really sorry for the dumb questions. the power adapter i have is similar type in this ppicture

http://www.arduino.cc/playground/Learning/WhatAdapter

is that the same of whats required?

i am now heading home for the power adapter , which will take an hour. so will be back then.

thank you very much for this help.

Difficult to say - the adapter should have a rating on it saysing so-many volts and so many mA. You have to be sure it supplies regulated DC (it should say so), and that the polarity of the leads is known. I'd ignore it for now, if you write it correctly, you can prove it works for one servo which USB can manage (probably) to supply, and then adding the others should be simple.

Sorry very late here, and I'm off to bed, maybe someone else can take over?

The servotimer2 link I posted probably is a good basis, but instead of having three separate servo objects (roll, pitch,yaw), I'd probably go for an array of servos. As I said, it may be possible to simple add some for loops to your existing code.

Good luck.

MegaServo is probably the most appropriate for you, if only because the example in the playground is very close to what you want.

Here is a version of that sketch that should get you going if you connect the LDRs to analog pins 0 through 4

#include <MegaServo.h>

#define NBR_SERVOS 5  // the number of servos
#define FIRST_SERVO_PIN 6 

MegaServo Servos[NBR_SERVOS] ;

int pos = 0;      // variable to store the servo position 

void setup()
{
  for( int i =0; i < NBR_SERVOS; i++)
    Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);
}
void loop()
{ 
  for( int i = 0; i <NBR_SERVOS; i++){ 
    pos = analogRead(i);   // read a value from 0 to 1023
    Servos[i].write( map(pos, 0,1023,0,180));   
  }  
  delay(15);   
}

I am off now as well so I also wish you the best of luck