Что мешает использовать массивы?
#include <Servo.h>
const int BUTTON_PIN[2] = {7,5};
const int SERVO_PIN[2] = {9,10};
Servo servo[2];
int angle[2] = {0,0};
int lastButtonState[2];
int currentButtonState[2];
void setup() {
Serial.begin(9600);
for(int i=0;i<2;i++){
pinMode(BUTTON_PIN[i], INPUT_PULLUP);
servo[i].attach(SERVO_PIN[i]);
servo[i].write(angle[i]);
currentButtonState[i] = digitalRead(BUTTON_PIN[i]);
}
}
void loop() {
for(int i=0;i<2;i++){
lastButtonState[i] = currentButtonState[i];
currentButtonState[i] = digitalRead(BUTTON_PIN[i]);
if(lastButtonState[i] == HIGH && currentButtonState[i] == LOW) {
Serial.println("The button "+i+" is pressed");
if(angle[i] == 0)
angle[i] = 90;
else
if(angle[i] == 90)
angle[i] = 0;
servo[i].write(angle[i]);
}
}
}
#include <Servo.h>
const int BUTTON_PIN = 7;
const int SERVO_PIN = 9;
const int BUTTON1_PIN = 5;
const int SERVO1_PIN = 10;
Servo servo;
Servo servo1;
int angle = 0;
int lastButtonState;
int currentButtonState;
int lastButton1State;
int currentButton1State;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
servo.attach(SERVO_PIN);
Serial.begin(9600);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
servo.attach(SERVO1_PIN);
servo.write(angle);
currentButtonState = digitalRead(BUTTON_PIN);
currentButton1State = digitalRead(BUTTON1_PIN);
}
void loop() {
lastButtonState = currentButtonState;
currentButtonState = digitalRead(BUTTON_PIN);
lastButton1State = currentButton1State;
currentButton1State = digitalRead(BUTTON1_PIN);
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
servo.write(angle);
}
if (lastButton1State == HIGH && currentButton1State == LOW) {
Serial.println("The button is pressed");
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
servo.write(angle);
}
}