I have general coding experience, mainly MatLab and Excel/VBA, so Arduino is a whole new ball game for me. I have narrowed it down to a coding error as the hardware works perfectly fine on a simple but not ideal code.
The general idea of the code is to have 2 buttons do two versions of something similar. In this case, the 1st button will run it 1 time, the 2nd button will run it multiple times. The code is doing everything as expected, except for the fact it will register 2 button presses when either button is pressed, executing the actions inside either buttons IF statement 2 times. I added the internal IF statements to the button.isPressed() IF statements, thinking it might fix it, but it still runs 2 times.
// -------------------- LIBRARIES --------------------------------------------------------------------------------------------------------------------------------------------------- //
#include <AccelStepper.h> // includes AccelStepper library
#include <ezButton.h> // includes ezButton library
// -------------------- CONSTANTS --------------------------------------------------------------------------------------------------------------------------------------------------- //
const int piece_Distance = 1670; // distance stepper motor needs to run
const int pieces_1 = 1; // number of pieces to run for button 1
const int pieces_2 = 10; // number of pieces to run for button 2
const int button_Interval = 50; // number of millisecs between button readings
AccelStepper stepper; // create stepper object, Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
const int button_Num = 2; // number of buttons used
const int button_1_Pin = 6; // defines pin number for button 1
const int button_2_Pin = 7; // defines pin number for button 2
ezButton buttonArray[] = {ezButton(button_1_Pin), ezButton(button_2_Pin)}; // creates button array for multiple buttons
const int relay_Pin = 8; // create relay_Pin object that attaches to pin 8
// -------------------- VARIABLES --------------------------------------------------------------------------------------------------------------------------------------------------- //
int count_1 = 1; // creates count object to keep track of for pieces
int count_2 = 0; // creates count object to keep track of for pieces
int button_1 = 0; // creates button 1 variable for code run
int button_2 = 0; // creates button 2 variable for loop run
unsigned long stepper_Distance_1 = 0; // set initial distance of stepper to 0 (max val of 4,294,967,295)
unsigned long stepper_Distance_2 = 0; // set initial distance of stepper to 0 (max val of 4,294,967,295)
// -------------------- SETUP (RUNS ONCE AT BEGINNING OF PROGRAM) ------------------------------------------------------------------------------------------------------------------- //
void setup()
{
Serial.begin(9600); // sets data rate in bits per second for serial data transmission
/*Stepper motor setup:*/
stepper.setMaxSpeed(2500.0); // sets maximum drive speed for stepper motor in steps per second
stepper.setAcceleration(5000.0); // sets acceleration for stepper motor in steps per second^2
pinMode(relay_Pin, OUTPUT); // declare Relay as output
for (byte i = 0; i < button_Num; i++) // for loop to establish debounce for each button
{
buttonArray[i].setDebounceTime(100); // set debounce time to 50 milliseconds
}
}
// -------------------- LOOP (RUNS UNTIL POWERED OFF OR STOPPED) -------------------------------------------------------------------------------------------------------------------- //
void loop()
{
for (byte i = 0; i < button_Num; i++)
buttonArray[i].loop(); // MUST call the loop() function first
if (buttonArray[0].isPressed())
{
button_1 = 1;
if (button_1 == 1)
{
Serial.println("The button 1 is pressed");
Serial.println(count_1);
/*Stepper motor control:*/
delay(2000); // delay stepper motor start
stepper_Distance_1 = stepper_Distance_1 + piece_Distance; // calculate stepper motor position
stepper.runToNewPosition(stepper_Distance_1);
delay(1000); // delay to turn on LED/Relay
digitalWrite (relay_Pin, HIGH); // turn on relay high
delay(500); // delay to switch to relay low
digitalWrite (relay_Pin, LOW); // turn on low relay (turn off)
button_1 = 0;
}
}
if (buttonArray[1].isPressed())
{
button_2 = 1;
if (button_2 == 1)
{
Serial.println("The button 2 is pressed");
for (count_2 = 1; count_2 < pieces_2 + 1; count_2++)
{
Serial.println(count_2);
/*Stepper motor control:*/
delay(2000); // delay stepper motor start
stepper_Distance_2 = stepper_Distance_2 + piece_Distance; // calculate stepper motor position
stepper.runToNewPosition(stepper_Distance_2);
delay(1000); // delay to turn on LED/Relay
digitalWrite (relay_Pin, HIGH); // turn on relay high
delay(500); // delay to switch to relay low
digitalWrite (relay_Pin, LOW); // turn on low relay (turn off)
}
button_2 = 0;
}
}
}
When I press button 1, I get the following in my Serial Monitor:
The button 1 is pressed
1
The button 1 is pressed
1
When I press button 2, I get the following in my Serial Monitor:
The button 2 is pressed
1
2
3
4
5
6
7
8
9
10
The button 2 is pressed
1
2
3
4
5
6
7
8
9
10
Any help is appreciated. I can provide more details if necessary. Thanks.