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