Multiple servo code help

Alright so I have 5 flex sensors and 5 servos I have a code for 1 flex to controll 1 servo but I need a code that will have 1 flex controll 1 servo fir 5 of each

alright so could u maby show me for 2 servos so i get the main idea. this is the main code that i want five times but if you could help me do it to it would be very helpful:

/*
 Servo control from an analog input

The minimum (minPulse) and maxiumum (maxPulse) values
will be different depending on your specific servo motor.
Ideally, it should be between 1 and 2 milliseconds, but in practice,
0.5 - 2.5 milliseconds works well for me.
Try different values to see what numbers are best for you.

This program uses the millis() function to keep track of when the servo was
last pulsed.  millis() produces an overflow error (i.e. generates a number
that's too big to fit in a long variable) after about 5 days. if you're
making a program that has to run for more than 5 days, you may need to
account for this.

by Tom Igoe
additions by Carlyn Maw & Rob Faludi
Created 28 Jan. 2006
Updated 10 Jun. 2008
*/

int servoPin = 2;     // Control pin for servo motor
int minPulse = 500;   // Minimum servo position
int maxPulse = 2500;  // Maximum servo position
int pulse = 0;        // Amount to pulse the servo

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int analogValue = 0;  // the value returned from the analog sensor
int analogPin = 0;    // the analog pin that the sensor's on

void setup() {
 pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
 pulse = minPulse;           // Set the motor position value to the minimum
 Serial.begin(9600);
}

void loop() {
 analogValue = analogRead(analogPin);      // read the analog input
 pulse = map(analogValue,0,1023,minPulse,maxPulse);    // convert the analog value
                                                       // to a range between minPulse
                                                       // and maxPulse.

 // pulse the servo again if rhe refresh time (20 ms) have passed:
 if (millis() - lastPulse >= refreshTime) {
   digitalWrite(servoPin, HIGH);   // Turn the motor on
   delayMicroseconds(pulse);       // Length of the pulse sets the motor position
   digitalWrite(servoPin, LOW);    // Turn the motor off
   lastPulse = millis();           // save the time of the last pulse
 }
}

Of course, this screams for using arrays for the pin numbers, analogRead output and map input/output.

I'd use a servo library, instead of trying to drive them with software, as per the posted example.

Wait so how do u use the servo libary do u go to examples in the arduono program or what

alright so could u maby write a code to controll 1 servo with 1 flex its confusing wwithout a example code

analogValueA = analogRead(analogPinA); analogValueB = analogRead(analogPinB); analogValueC = analogRead(analogPinC); analogValueD = analogRead(analogPinD); analogValueE = analogRead(analogPinE); .... pulseA = map(analogValueA,0,1023,minPulse,maxPulse); pulseB = map(analogValueB,0,1023,minPulse,maxPulse); pulseC = map(analogValueC,0,1023,minPulse,maxPulse); pulseD = map(analogValueD,0,1023,minPulse,maxPulse); pulseE = map(analogValueE,0,1023,minPulse,maxPulse); ..... and so on

Just take each of the variable lines and duplicate them five times each. Get the idea?

So this is what i have so far and i made a new post about my problem: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278390294

In answer to your 'new post' (which is 100% related to this post, and therefore was not needed).

You are nesting your if's... When completely indented, it looks like this:

if (millis() - lastPulse1 >= refreshTime1) {
  digitalWrite(servoPin1, HIGH);  
  delayMicroseconds(pulse1);
  digitalWrite(servoPin1, LOW);  
  lastPulse1 = millis();  
  if (millis() - lastPulse2 >= refreshTime2) {
    digitalWrite(servoPin2, HIGH);  
    delayMicroseconds(pulse2);
    digitalWrite(servoPin2, LOW);  
    lastPulse2 = millis();  
    if (millis() - lastPulse3 >= refreshTime3) {
      digitalWrite(servoPin3, HIGH);  
      delayMicroseconds(pulse3);
      digitalWrite(servoPin3, LOW);  
      lastPulse3 = millis();  
      if (millis() - lastPulse4 >= refreshTime4) {
        digitalWrite(servoPin4, HIGH);  
        delayMicroseconds(pulse4);
        digitalWrite(servoPin4, LOW);  
        lastPulse4 = millis();  
        if (millis() - lastPulse5 >= refreshTime5) {
          digitalWrite(servoPin5, HIGH);  
          delayMicroseconds(pulse5);
          digitalWrite(servoPin5, LOW);  
          lastPulse5 = millis();
        }
      }
    }
  }
}

In answer to your 'new post' (which is 100% related to this post, and therefore was not needed).

You are nesting your if's... When completely indented, it looks like this:

Code: if (millis() - lastPulse1 >= refreshTime1) { digitalWrite(servoPin1, HIGH); delayMicroseconds(pulse1); digitalWrite(servoPin1, LOW); lastPulse1 = millis(); if (millis() - lastPulse2 >= refreshTime2) { digitalWrite(servoPin2, HIGH); delayMicroseconds(pulse2); digitalWrite(servoPin2, LOW); lastPulse2 = millis(); if (millis() - lastPulse3 >= refreshTime3) { digitalWrite(servoPin3, HIGH); delayMicroseconds(pulse3); digitalWrite(servoPin3, LOW); lastPulse3 = millis(); if (millis() - lastPulse4 >= refreshTime4) { digitalWrite(servoPin4, HIGH); delayMicroseconds(pulse4); digitalWrite(servoPin4, LOW); lastPulse4 = millis(); if (millis() - lastPulse5 >= refreshTime5) { digitalWrite(servoPin5, HIGH); delayMicroseconds(pulse5); digitalWrite(servoPin5, LOW); lastPulse5 = millis(); } } } } }

So all I have to do us make it like thy an that should fix it? And sry about that I didn't relize this was the same thing

No, the code I posted is what your code looks like when indented. Indenting helps you see what is happening and which code goes where... I also don't like giving answers... you learn by doing, not by being given the answer.

Don't nest your if's..

if (this) { if (that) { } }


if (this){ } if (that){ }

If you are unable to fix your code with this much of a hint, I would strongly recommend buying a book or two on programming.

You are making extra work (and confusion) for yourself by trying to bit-bang all those servos. Exactly what is the issue with using the servo library that several of us have suggested. You would probably find people here more willing to help as well.

well I totally would and it looks easier but I justsonr no how to set up any of the analog inputs ad stuff idk if I could see a code for 1 servo and 1 flex sensor I'm sure ibwoukd ketch on fast and use that insted if what I'm ussing now

Have you read the servo tutorial? http://arduino.cc/en/Tutorial/Sweep

No, the code I posted is what your code looks like when indented. Indenting helps you see what is happening and which code goes where... I also don't like giving answers... you learn by doing, not by being given the answer.

Don't nest your if's..

if (this) { if (that) { } }


if (this){ } if (that){ }

If you are unable to fix your code with this much of a hint, I would strongly recommend buying a book or two on programming.

alright so i made the bottom like this: if (millis() - lastPulse1 >= refreshTime1) { digitalWrite(servoPin1, HIGH); delayMicroseconds(pulse1); digitalWrite(servoPin1, LOW); lastPulse1 = millis(); } if (millis() - lastPulse2 >= refreshTime2) { digitalWrite(servoPin2, HIGH); delayMicroseconds(pulse2); digitalWrite(servoPin2, LOW); lastPulse2 = millis(); if (millis() - lastPulse3 >= refreshTime3) { digitalWrite(servoPin3, HIGH); delayMicroseconds(pulse3); digitalWrite(servoPin3, LOW); lastPulse3 = millis(); } if (millis() - lastPulse4 >= refreshTime4) { digitalWrite(servoPin4, HIGH); delayMicroseconds(pulse4); digitalWrite(servoPin4, LOW); lastPulse4 = millis(); } if (millis() - lastPulse5 >= refreshTime5) { digitalWrite(servoPin5, HIGH); delayMicroseconds(pulse5); digitalWrite(servoPin5, LOW); lastPulse5 = millis(); } } }

and now its not working just the first servo is working

alright alright i got it i got ahead of myselfs and it was a wiring problem with the sensors but thank you to all your advise i learn somthing new each day