too many analog value for servos to move in to position

I have an arduino uno connected to 3 LEDs ,2 servos,a MUX and 2 light sensors. The 2 light sensors are reading data and they are suppose to be comparing values to decide whether 3 leds and 2 servos should be activated.The issue is that the values change quickly and sometimes drastically between the two sensors.which is fine for the leds but not so great for the servos I tried to add a timer so that the servos only were activated when the LED were activated for a certain amount of time but It didnt work the way I have it setup. Im also not sure if this is the most efficient way to set up everything.

OK, was there a question in there somewhere? Was there some piece of code that you were hoping to get help with?

Last posters comments plus : Take a moving average of the signals or take multiple readings and average them or a filter Such as the output = 30% of new value plus 70% of the average of the last valued. ( you can alter the ratios 30/70 to suit )
There is only so much you can do here , if the sensor is jumping all over the place then that needs sorting

Well this is what im working with so far.

3.ino (1.74 KB)

I’ll post OP’s code for him since he can’t be bothered to read the instructions.

#include <Servo.h> 
Servo servo1;   
Servo servo2;

Servo servo3;   
Servo servo4;

Servo servo5;   
Servo servo6;

int pin_Out_S0 = 2;
int pin_Out_S1 = 3;
int pin_Out_S2 = 4;
int pin_In_Mux1 = A1;
int Mux1_State[8] = {0};

int LED1 = 13;
int LED2 = 12;
int LED3 = 7;

int analogValue = 0;

void setup() {
  servo1.attach(10); 
servo2.attach(5);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
    
  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);
  //pinMode(pin_In_Mux1, INPUT);
  Serial.begin(9600);
}

void loop() {
  updateMux1();
  for(int i = 0; i < 2; i ++) {
    if(i == 7) {
      Serial.println(Mux1_State[i]);
    } else {
      Serial.print(Mux1_State[i]);
      Serial.print(",");
    }analogValue = analogRead(pin_In_Mux1);

  }
}

void updateMux1 () {
  for (int i = 0; i < 2; i++){
    digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
    digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
    digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
    Mux1_State[i] = analogRead(pin_In_Mux1);
        delay(300);// delay was mnot there before
     if (Mux1_State[i] <= 800)
  {
    servo1.write(15);
    servo2.write(95);
   
         
      
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        
    delay(600);

  }
   else if ( Mux1_State[i]>= 800 && Mux1_State[i] < 910)
   {
       servo1.write(49);
    servo2.write(70);
          digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, LOW);
 

   }
  
  else
  {
     servo1.write(71);
     servo2.write(50);
      digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
    
  }

        }
    
    }

OP. It would probably help you a LOT to format that code. When we line all our code blocks up we don’t do that just to make it pretty. It really does help you to follow what you’ve written. You can get the IDE to do it for you with control-T. It’s so easy there is really no excuse for having code that wanders all over the page.

yeah sorry about that

Frank337:
yeah sorry about that

Does that mean you are not really sorry enough to do anything about it?

...R

#include <Servo.h>
Servo servo1;
Servo servo2;

int LED1 = 13;
int LED2 = 12;
int LED3 = 7;

int pin_Out_S0 = 2;
int pin_Out_S1 = 3;
int pin_Out_S2 = 4;
int pin_In_Mux1 = A1;
int Mux1_State[8] = {0};
int analogValue = 0;





void setup() {
  servo1.attach(10);
  servo2.attach(5);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);
  //pinMode(pin_In_Mux1, INPUT);
  Serial.begin(9600);
}

void loop() {
  updateMux1();
  for (int i = 0; i < 2; i ++) {
    if (i == 7) {
      Serial.println(Mux1_State[i]);
    } else {
      Serial.print(Mux1_State[i]);
      Serial.print(",");
    } analogValue = analogRead(pin_In_Mux1);

  }
}

void updateMux1 () {
  for (int i = 0; i < 2; i++) {
    digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
    digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
    digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
    Mux1_State[i] = analogRead(pin_In_Mux1);

    delay(300);

    if (Mux1_State[i] <= 800)
    {
      servo1.write(15);
      servo2.write(95);



      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);

      delay(600);

    }
    else if ( Mux1_State[i] >= 800 && Mux1_State[i] < 910)
    {
      servo1.write(49);
      servo2.write(70);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, LOW);


    }

    else
    {
      servo1.write(71);
      servo2.write(50);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);

    }

  }

}

Hi,
What model Arduino are you using?
Why do you need a MUX for two analog inputs?
What are the light sensors looking at?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

I'm using an Arduino uno.The Mux is there because in the future i plan to add more sensors(I wanted to get two down before I stepped up to more sensors).I'm using 2x 5MM Photoresistor – GL5516
As for the circuit I don't a CAD sketch of the circuit (we went through a lot of changes sense the original plan)and I don't have have any hand drawings I would make one now but I do not have access to the circuit at this time.However I can answer some basic questions about the circuit now and add a visual of the circuit later.

Hi,
If you are happy with the response to the sounds by the LEDs, then all you need to do is make a running average of the analog input and use that as the controlling variable.
OR
Use millis and everytime you get a trigger to move the servo, it moves and will not respond for say 1Sec, to another input.
Don’t use delay as that blocks the code.

What are the LEDs?
Are they rated at 12V?

Tom… :slight_smile: