[HELP] Servos and comparators

Guys, I'm currently developing a code for my project, that basically turns a servo a certain number of degrees, by a corresponding pulse from the comparator circuit that i built. It is working fine, along with the reactions, but there is one problem, as there is a continuous pulse on the input of the arduino, it turns the servo, but, it goes back and forth to the desired position, making it move quite a lot, rather than staying put on the position. How to eliminate this? Here's a copy of the code.

#include <Servo.h>

Servo MainS;

int pos = 90;

int L = 12;
int C = 11;
int R = 10;


void setup() {
  MainS.attach(8);
  pinMode(L, INPUT);
  pinMode(C, INPUT);
  pinMode(R, INPUT);
}

void loop() 
{

  int Ls = digitalRead(L);
  int Cs = digitalRead(C);
  int Rs = digitalRead(R);
    
  if (Ls == 1) {
      for(pos = 90; pos <= 179; pos += 5)  
        {                                
          MainS.write(pos);             
          delay(15);  
        }    
      }
      
  if (Rs == 1) {
      for(pos = 90; pos >=0; pos -= 5)  
        {                                
          MainS.write(pos);             
          delay(15);
        }    
      } 
  if (Cs == 1) {
        pos = 90;
        MainS.write(pos);
      }
      delay(15);
}

When the L or R pin is on you tell it to move to 90 and then in steps to either 0 or 179. If you don't want it to move to 90 you could track the current position and move from there:

int position = 90;

void loop() 
{

  int Ls = digitalRead(L);
  int Cs = digitalRead(C);
  int Rs = digitalRead(R);
    
  if (Ls == 1) {
      for(pos = position; pos <= 179; pos += 5)  
        {                                
          MainS.write(pos);             
          delay(15);  
        }
      position = 179;
      }
      
  if (Rs == 1) {
      for(pos = position; pos >=0; pos -= 5)  
        {                                
          MainS.write(pos);             
          delay(15);
        }
      position = 0;
      } 

  if (Cs == 1) {
        pos = 90;
        MainS.write(pos);
        position = 90;
      }
      delay(15);
}

Thanks for help. It is working! :smiley:

I have modified the code, and i would like to get the last state of the sensor, in this case, when it turns 1 1 1 1 1, the uC will follow the last state it was in...

Here's the code

//Code by O.L.

#include <Servo.h>

Servo MainS;

int pos = 90;
int LM = 13;
int L = 12;
int C = 11;
int R = 10;
int RM = 9;
int position = 90;



void setup() {
  MainS.attach(8);
  pinMode(LM, INPUT);
  pinMode(L, INPUT);
  pinMode(C, INPUT);
  pinMode(R, INPUT);
  pinMode(RM, INPUT);
}

void loop() 
{
  
int LMs = digitalRead(LM);
int Ls = digitalRead(L);
int Cs = digitalRead(C);
int Rs = digitalRead(R);
int RMs = digitalRead(RM);

if (LMs == 0 && Ls == 0 && Cs == 0 && Rs == 0 && RMs == 0){
   pos = 90;
        MainS.write(pos);
        position = 90;
      }
      
if (LMs == 0 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {
      for(pos = position; pos <= 179; pos += 5)  
        {                                
          MainS.write(pos);             
          delay(15);  
        }
      position = 179;
      }
if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 0) {
      for(pos = position; pos >=0; pos -= 5)  
        {                                
          MainS.write(pos);             
          delay(15);
        }
      position = 0;
      } 
if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 0 && RMs == 1) {
      for(pos = position; pos >=45; pos -= 5)  
        {                                
          MainS.write(pos);             
          delay(15);
        }
      position = 45;
      } 
if (LMs == 1 && Ls == 0 && Cs == 1 && Rs == 1 && RMs == 1) {
      for(pos = position; pos >=135; pos -= 5)  
        {                                
          MainS.write(pos);             
          delay(15);
        }
      position = 135;
      } 
if (LMs == 1 && Ls == 1 && Cs == 0 && Rs == 1 && RMs == 1) {
        pos = 90;
        MainS.write(pos);
        position = 90;
      }
if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {
        pos = position;
        MainS.write(pos);
        position = 90;}
}
if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {
        pos = position;
        MainS.write(pos);
        position = 90;}

Two things:

  1. If you want it to maintain the same position, don't change position to 90.
  2. The servo will maintain position until you tell it otherwise.

If you eliminate the un-needed assignment (1):

if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {
        pos = position;
        MainS.write(pos);}

If you then eliminate the un-needed write (2):

if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {  }

Now that the if-statement is empty you can eliminate that, too:

// In the case of (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) we don't need to do anything.