Problem with servo.read() and array (Solved)

Hello to all.

Sorry if this has already been asked but I could not find it.

I have three servos that do the same after pushing the respective push button.
For that I am using arrays to code the servos and the push buttons.

I know that some are against it but since the servos are "inactive" for a while (minutes), I added the option "detach" to save energy.

For that, I thought in the following code:

  • read the last position written in the servo with servo.read()
  • check if the button is pressed
  • if the button is pressed, update position to new angle, else, position remain unchanged
  • compare the reading with the position
  • if it is the same, detach and update last position, else, attach, write and update last position

Here is the code:

#include <Servo.h>

const byte numservo = 3;
int buttonpin[numservo];
int buttonstate[numservo];
int buttonread[numservo];
int buttonprevious[numservo];

Servo servo[numservo];
int servopin[numservo];
int servopos[numservo];
int servostate[numservo];
int servoon = 100;
int servooff = 125;
int servoread[numservo];

unsigned long timecurr;
unsigned long timebutton[numservo];
unsigned long timedeb = 50;
unsigned long timeservo;

void setup() {
  Serial.begin(9600);
  Serial.println("Pimanel_Rolling_Car");
  for (int i = 0; i < numservo; i++) {
    buttonpin[i] = 2 + i;
    servopin[i] = 2 + numservo + i;
    pinMode(buttonpin[i], INPUT_PULLUP);
    servostate[i] = 1;
    servopos[i] = map(servostate[i], 0, 1, servoon, servooff);
    buttonprevious[i] = HIGH;
  }
}

void loop() {
  timecurr = millis();
  for (int i = 0; i < numservo; i++) {
    buttonread[i] = digitalRead(buttonpin[i]);
    servoread[i]= servo[i].read(servopos[i]);
    if (buttonread[i] != buttonprevious[i]) {
      timebutton[i] = millis();
    }
    if (timecurr - timebutton[i] > timedeb) {
      if (buttonread[i] != buttonstate[i]) {
        buttonstate[i] = buttonread[i];
        if (buttonstate[i] == LOW) {
          servostate[i] = !servostate[i];
          servopos[i] = map(servostate[i], 0, 1, servoon, servooff);
          Serial.print("Servo ");
          Serial.print(i + 1);
          Serial.print(" is moving to ");
          Serial.println(servopos[i]);
        }
      }
    }
    buttonprevious[i] = buttonread[i];
    if (servoread[i]==servopos[i] {
      servo[i].detach();
    }
    else {
      servo[i].attach(servopin[i]);
      servo[i].write(servopos[i]);
    }
  }
}

I get the following error when validating the code:

Arduino: 1.8.12 (Windows 10), Placa:"Arduino Uno"

C:\***\Pimanel_Rolling_Car.ino: In function 'void loop()':

Pimanel_Rolling_Car:39:44: error: no matching function for call to 'Servo::read(int&)'

     servoread[i]= servo[i].read(servopos[i]);

                                            ^

In file included from C:\***\Pimanel_Rolling_Car.ino:1:0:

C:\Program Files (x86)\Arduino\libraries\Servo\src/Servo.h:113:7: note: candidate: int Servo::read()

   int read();                        // returns current pulse width as an angle between 0 and 180 degrees

       ^~~~

C:\Program Files (x86)\Arduino\libraries\Servo\src/Servo.h:113:7: note:   candidate expects 0 arguments, 1 provided

exit status 1
no matching function for call to 'Servo::read(int&)'

What am I doing wrong?

Thanks in advance
Filipe Almeida

The servo read function doesn't take a parameter. Drop the

servopos[i]

From the line showing you an error.

Hi again

Ok. Now I see the "two" errors that I made.

First

servopos[i] instead of servopin[i]

and second

servo[i].read() do not require parameters at all

Thanks very much
Filipe Almeida