Multiple (int)'s from single input

Hello again!

I have:

5 servos
5 LEDs
1 button

I wrote a function (or I should say a function was written for me... thanks to Pauls) where the first push of a button lit an LED and sent a servo to 90 degrees. The second push turned off the LED and brought back the servo to 0 degrees.

I'm trying to modify this code so that the push of a button would send 5 servos to 90 degrees and light 5 LEDs, and pushed again bring back to 0 and LEDs off.

I will later need the servos to each go at different angles at the push of a single button, then each go to other different angles at the push of a second button.

I get this error message that highlights the code in void loop():

In function 'void loop()':
error: invalid initialization of reference of type 'int&' from expression of type 'const int'

I just don't know how to insert all the variables and constants in the function.

here's the code:

#include <Servo.h> 

Servo myservo7;                // creates servo objects to control servos 
Servo myservo8;               
Servo myservo9;               
Servo myservo10;              
Servo myservo11;               

const int buttonPin2 = 2;      // the pins that the pushbuttons are attached to
const int buttonPin3 = 3;     
const int buttonPin4 = 4;      
const int buttonPin5 = 5;     
const int buttonPin6 = 6;      

const int ledPin12    = 12;     // the pins that the LEDs are attached to
const int ledPin13    = 13;      
const int ledPin14    = 14;     
const int ledPin15    = 15;     
const int ledPin16    = 16;

int buttonState2     = 0;      // current state of the buttons
int buttonState3     = 0;     
int buttonState4     = 0;     
int buttonState5     = 0;     
int buttonState6     = 0;    

int lastButtonState2 = 0;      // previous state of the buttons
int lastButtonState3 = 0;     
int lastButtonState4 = 0;      
int lastButtonState5 = 0;    
int lastButtonState6 = 0;   

int ledState12       = 0;      // remembers current LED states
int ledState13       = 0;  
int ledState14       = 0;    
int ledState15       = 0;    
int ledState16       = 0;     

int pos7             = 0;      // variable to store the servos positions
int pos8             = 0;      
int pos9             = 0;      
int pos10            = 0;     
int pos11            = 0;      

void setup()
{
  myservo7.attach(7);          // attaches the servo pins to the servo objects
  myservo8.attach(8);        
  myservo9.attach(9);         
  myservo10.attach(10);      
  myservo11.attach(11);       
  
  pinMode(buttonPin2, INPUT);  // initializes the button pins as inputs
  pinMode(buttonPin3, INPUT); 
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  
  pinMode(ledPin12, OUTPUT);    // initializes the LED pins as outputs
  pinMode(ledPin13, OUTPUT);    
  pinMode(ledPin14, OUTPUT);
  pinMode(ledPin15, OUTPUT);
  pinMode(ledPin16, OUTPUT);
}

void  MoveServosIfButton6Pressed(int servoPin7,            // creates independent function 
                                 int servoPin8,            // to move servos if button 6 is pressed
                                 int servoPin9,
                                 int servoPin10,
                                 int servoPin11,
                               int buttonPin6,           
                               int ledPin16,
                                 int ledPin15,
                                 int ledPin14,
                                 int ledPin13,
                                 int ledPin12,
                               int &lastButtonState6,
                               int &ledState16,
                                 int &ledState15,
                                 int &ledState14,
                                 int &ledState13,
                                 int &ledState12)
{
  buttonState6 = digitalRead(buttonPin6);                  // read the pushbutton input pin
    if (buttonState6 != lastButtonState6)                  // compare buttonState to previous state
    {
      if (buttonState6 == 1)
      {
        if(ledState16 == 1)
        if(ledState15 == 1)
        if(ledState14 == 1)
        if(ledState13 == 1)
        if(ledState12 == 1)
          {
            ledState16 = 0;
            ledState15 = 0;
            ledState14 = 0;
            ledState13 = 0;
            ledState12 = 0;
            
            for(pos7 = 0; pos7 <= 90; pos7 += 5)           // goes from 0 degrees to 90 degrees 
            for(pos8 = 0; pos8 <= 90; pos8 += 5)           // in steps of 5 degrees 
            for(pos9 = 0; pos9 <= 90; pos9 += 5)
            for(pos10 = 0; pos10 <= 90; pos10 += 5)
            for(pos11 = 0; pos11 <= 90; pos11 += 5)
            {                                              
            myservo7.write(pos7);                          // tells servo to go to position 'posX' 
            delay(15);                                     // waits 15ms for the servo to reach the position
            myservo8.write(pos8);
            delay(15);
            myservo9.write(pos9);
            delay(15);
            myservo10.write(pos10);
            delay(15);
            myservo11.write(pos11);
            delay(15); 
            }
          } 

        else  
          {  
            ledState16 = 1;
            ledState15 = 1;
            ledState14 = 1;
            ledState13 = 1;
            ledState12 = 1;
            
            for(pos7 = 90; pos7 >= 1; pos7 -= 5)    // goes from 90 degrees to 0 degrees
            for(pos8 = 90; pos8 >= 1; pos8 -= 5)
            for(pos9 = 90; pos9 >= 1; pos9 -= 5)
            for(pos10 = 90; pos10 >= 1; pos10 -= 5)
            for(pos11 = 90; pos11 >= 1; pos11 -= 5) 
            {                                
            myservo7.write(pos7);                          // tells servo to go to position 'posX' 
            delay(15);                                     // waits 15ms for the servo to reach the position
            myservo8.write(pos8);
            delay(15);
            myservo9.write(pos9);
            delay(15);
            myservo10.write(pos10);
            delay(15);
            myservo11.write(pos11);
            delay(15);                              // waits 15ms for the servo to reach the position 
            }
          }
      }
     
    lastButtonState6 = buttonState6;                // remembers the current state of the button
    }
digitalWrite(ledPin16, ledState16);
digitalWrite(ledPin15, ledState15);
digitalWrite(ledPin14, ledState14);
digitalWrite(ledPin13, ledState13);
digitalWrite(ledPin12, ledState12);
delay(20);
}

void loop() 
{
    MoveServosIfButton6Pressed(7,8,9,10,11,12, buttonPin6, ledPin16, ledPin15, ledPin14, ledPin13, ledPin12, lastButtonState6, ledState16, ledState15, ledState14, ledState13, ledState12);
 
}

Thanks

Can I suggest you stop, before you get bogged down in mass of undebuggable code, and read about arrays? (and possibly structures, and arrays thereof)

There are 18 arguments in the function call, and 17 in the function definition.

By the way, that function will not do what you think it will.

And here is some example code that shows how you can create and use arrays. You will need change the logic to do what you want, but you can use this as a guide for how to set up and access the arrays.

#include <Servo.h>

const int NumberOfObjects = 5;
Servo myservos[NumberOfObjects];                // creates array of servo objects
const int servoPins[NumberOfObjects] = {7,8,9,10,11};
int servoPos[NumberOfObjects]= { 0};      // variable to store the servos positions

const int buttonPins[NumberOfObjects] = {2,3,4,5,6};      // the pins that the pushbuttons are attached to
const int ledPins[NumberOfObjects] = {12,13,14,15,16};     // the pins that the LEDs are attached to
int buttonStates[NumberOfObjects] = {0};      // current state of the buttons
int lastButtonStates[NumberOfObjects] = {0};      // previous state of the buttons
int ledStates[NumberOfObjects] = {0};      // remembers current LED states


int index;

void setup()
{
  for(index = 0; index < NumberOfObjects; index++)
  { 
    myservos[index].attach(servoPins[index]);          // attaches the servo pins to the servo objects
    pinMode(buttonPins[index], INPUT);  // initializes the button pins as inputs
    pinMode(ledPins[index], OUTPUT);    // initializes the LED pins as outputs 
  } 
}

void  MoveServoIfButtonPressed(int index)        
{
//this is random example code, it probably does not yet do what you want
  buttonStates[index] = digitalRead(buttonPins[index]);                  // read the pushbutton input pin
  if (buttonStates[index] != lastButtonStates[index])                  // compare buttonState to previous state
  {
    if (buttonStates[index] == 1)
    {        
      ledStates[index] = 0;

      for(servoPos[index] = 0; servoPos[index] <= 90; servoPos[index] += 5)           // goes from 0 degrees to 90 degrees
      {                                              
        myservos[index].write(servoPos[index]);                          // tells servo to go to position 'posX'
        delay(15);                                     // waits 15ms for the servo to reach the position
      }
    }

    else  
    {  
      // your else code here....
    }
  }

  lastButtonStates[index] = buttonStates[index];                // remembers the current state of the button
  digitalWrite(ledPins[index], ledStates[index]);
  delay(20);
}

void loop()
{
  for(index = 0; index < NumberOfObjects; index++)
    MoveServoIfButtonPressed(index);
}