im at the very beginning of a project on WOKWI where im learning about servos. My first step is to connect 4 push buttons (so far only need two) to control a select amount of servos. currently ive made it so if you press the up pushbutton the servo moves to the left and if you press the down one vice versa. I was wondering how i would be able to put this into a function. Yes i've read about functions but cant quite figure out how to create one for this type of code. the part I want to put in a function is the code right after void loop() and before the checkbutton functions. Here is the code - any help would be great
#include <Servo.h>
Servo servo1;
// Define the pins that the pushbuttons are connected to
const int buttonPinYup = 11;
const int buttonPinXr = 10;
const int buttonPinYdown = 9;
const int buttonPinXl = 8;
int servo1_pin = 7; // declare variable for digital output pin for servo1
int servo2_pin = 12; // declare variable for digital output pin for servo2
int initial_position = 90; // declare variable for initial position of servo1
int initial_position1 = 90; // declare variable for initial position of servo2
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial communication with baud rate of 9600
servo1.attach(servo1_pin); // attach servo1 to its digital output pin
servo1.write(initial_position); // set initial position of servo1
// Set the button pins as inputs with pull-up resistors
pinMode(buttonPinYup, INPUT_PULLUP);
pinMode(buttonPinYdown, INPUT_PULLUP);
pinMode(buttonPinXl, INPUT_PULLUP);
pinMode(buttonPinXr, INPUT_PULLUP);
}
// this is a function
/*
The function starts by reading the state of the button connected to the given pin number using the
digitalRead function. The resulting value (HIGH or LOW) is stored in a variable called buttonState.
Next, the function checks if the buttonState is LOW, which indicates that the button is pressed.
If the button is pressed, the function prints the message to the Serial Monitor using the Serial.println function. This message can be used to identify which button was pressed.
If the button is not pressed (i.e., buttonState is HIGH), nothing happens, and the function returns.
This function is used in the loop function to check the state of each button and print a corresponding
message to the Serial Monitor if the button is pressed. By calling checkButton for each button,
the code avoids duplicating the same lines of code for each button and improves readability and maintainability.
*/
void checkButton(int buttonPin, const char* message) {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Serial.println(message);
}
}
void loop() {
int currentButtonState = digitalRead(buttonPinYup); // read new state
int currentButtonState1 = digitalRead(buttonPinYdown); // read new state
if (currentButtonState == LOW) {
// change angle of servo motor
if (initial_position < 10) { // if initial position is less than 10 degrees
} else {
initial_position = initial_position - 15; // decrease initial position by 20 degrees
servo1.write(initial_position); // set new position of servo1
delay(25); // wait 100 milliseconds
}
}
if (currentButtonState1 == LOW) {
// change angle of servo motor
if (initial_position > 180) { // if initial position is less than 10 degrees
} else {
initial_position = initial_position + 15; // decrease initial position by 20 degrees
servo1.write(initial_position); // set new position of servo1
delay(25); // wait 100 milliseconds
}
}
// Print which button is being pressed to the Serial Monitor
checkButton(buttonPinYup, "Up");
checkButton(buttonPinYdown, "Down");
checkButton(buttonPinXl, "Left");
checkButton(buttonPinXr, "Right");
// Wait a short time before reading the button states again
delay(50);
}