I am very new and this is my first project outside silly exercises and the intro kit book, so take it easy on me.
I am trying to make 4 servos dance when (and only when) I hit 1 on my 16 key touchpad (as a precursor to using these elements to turn my garden hoses on and off... baby steps here). They are supposed to dance once each, then stop until a button is pushed again.
However, when I upload the code below, the servos dance without any buttons being pushed. Furthermore, they continue dancing in sequence on a loop despite my loop ending with converting my variable lastPush to a non-1 number. I cannot determine what I am doing wrong. I barely understand libraries yet, and I am using 2 of them, so I suspect the issue lies there somehow. I am sure there are a number of other unnecessary or incorrect things I am doing as a result, so if you see something, feel free to mention it.
Thank you for any help you can give. I am really enjoying this hobby so far.
#include <Keypad.h>
//four rows
const byte ROWS = 4;
//four columns
const byte COLS = 4;
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//connect to the row pinouts of the keypad
byte rowPins[ROWS] = {13, 12, 9, 8};
//connect to the column pinouts of the keypad
byte colPins[COLS] = {7, 4, 3, 2};
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
#include <Servo.h>
Servo servoA;
Servo servoB;
Servo servoC;
Servo servoD;
int angleA = 0; // the angle of the servo A
int angleB = 0;// the angle of the servo B
int angleC = 0; // the angle of the servo C
int angleD = 0; // the angle of the servo D
char lastPush = 17; // the last button pushed. I am using 17 as the parking spot for no button pushed
void setup(){
Serial.begin(9600); // these lines set up the serial readout on the computer
servoA.attach(11);
servoB.attach(10);
servoC.attach(6);
servoD.attach(5);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){Serial.println(customKey);} // show button presses in serial readout
if (customKey){lastPush = (customKey);} // establish the last button pushed semi-permanently
if (customKey){Serial.println(lastPush);} // show lastPush in serial
if (lastPush = 1)
{servoA.write(90);
delay(250);
servoA.write(45);
delay(250);
servoA.write(135);
delay(250);
servoA.write(0);
delay(1000);
servoB.write(90);
delay(250);
servoB.write(45);
delay(250);
servoB.write(135);
delay(250);
servoB.write(0);
delay(1000);
servoC.write(90);
delay(250);
servoC.write(45);
delay(250);
servoC.write(135);
delay(250);
servoC.write(0);
delay(1000);
servoD.write(90);
delay(250);
servoD.write(45);
delay(250);
servoD.write(135);
delay(250);
servoD.write(0);
lastPush = 17;
Serial.println(lastPush);}
}