Sweeping without delay

Hi!

When I use this code for sweeping two servos in opposite direction I get a problem… the problem is that ch4 gets really high values when ch3 goes over 1600 (like 15.000). Could you guys help me out?

Thanks!

int ch2;
int ch3;
int ch4;

#include <Servo.h>

Servo servo1;
Servo servo2;           
Servo servo3;           
Servo servo4;            // create servo object to control a servo
                        // twelve servo objects can be created on most boards

int pos1 = 0;           // variable to store the servo position
int pos2 = 90;

class Sweeper1
{
  Servo servo1;              // the servo
  int pos;                   // current servo position 
  int increment;            // increment to move for each interval
  int  updateInterval;      // interval between updates
  unsigned long lastUpdate; // last update of position
 
public: 
  Sweeper1(int interval)
  {
    updateInterval = interval;
    increment = 1;
  }
  
  void Attach(int pin)
  {
    servo1.attach(pin);
  }
  
  void Detach()
  {
    servo1.detach();
  }
  
  void Update()
  {
    if((millis() - lastUpdate) > updateInterval)  // time to update
    {
      lastUpdate = millis();
      pos += increment;
      servo1.write(pos);
      Serial.println(pos);
      if ((pos >= 90) || (pos <= 0)) // end of sweep
      {
        // reverse direction
        increment = -increment;
      }
    }
 }

};

class Sweeper2
{
  Servo servo2;              // the servo
  int pos;                   // current servo position 
  int increment;            // increment to move for each interval
  int  updateInterval;      // interval between updates
  unsigned long lastUpdate; // last update of position
 
public: 
  Sweeper2(int interval)
  {
    updateInterval = interval;
    increment = 1;
  }
  
  void Attach(int pin)
  {
    servo2.attach(pin);
  }
  
  void Detach()
  {
    servo2.detach();
  }
  
  void Update()
  {
    if((millis() - lastUpdate) > updateInterval)  // time to update
    {
      lastUpdate = millis();
      pos -= increment;
      servo2.write(90-pos);
      Serial.println(pos);
      if ((pos >= 90) || (pos <= 0)) // end of sweep
      {
        // reverse direction
        increment = -increment;
      }
    }
 }

};
 

 
Sweeper1 sweeper1(5);
Sweeper2 sweeper2(5);

void setup() {

  pinMode(3, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(9, INPUT);



  Serial.begin(115200);


  servo3.attach(7);
  servo4.attach(8);

  sweeper1.Attach(2);
  sweeper2.Attach(4);

}

void loop() {


Serial.println("Table");

  ch2 = pulseIn(5, HIGH);
   Serial.print (", Ch2:"); Serial.print (ch2);

  ch3 = pulseIn(6, HIGH);
   Serial.print (", Ch3:"); Serial.print (ch3);
      
  ch4 = pulseIn(9, HIGH);
   Serial.print (", Ch4:"); Serial.print (ch4);   
   
// Sweeping-condition
if (ch3 >= 1600 && 1400 <= ch4 && ch4 <= 1600) {
  sweeper1.Update();
  sweeper2.Update();
}

}

Can you explain why Sweeper1 looks so much like Sweeper2 ?

Because I want it to the same but swipe in the opposite direction. Maybe there is a easier way? I'm new to this kind of programming.

EDIT: Sorry, forgot to change the Sweeper2. In the if-loop it should be a - instead of +. Edited it now in the code.

Maybe there is a easier way?

A single class with a extra boolean member flag that tells the class to do write "90 - angle" - it's be a whole lot shorter.

Ok, thanks. But I have still the problem that ch4 gets really hugh values when ch3>1600.. any ideas there?