Hello, I need help with this LED issue I'm struggling with right now. my goal for this project is to get the LED light turn on by pressing the button with the servo motor moving to position and then press the button again to get the LED light turn off with the servo motor back in place. In this project, I have four servo motors with two push buttons that are functioning well. I only need two servos with a LED light to function with a button. When I connected the LED to the correct pin and write the code to upload to the Arduino Uno board, the LED is showing low dimming light which is not what I want; I want strong light up, and the servos are moving fine when I pressed the button. I don't know what is wrong with it. I looked at the code carefully and everything looks okay. I use a fully charged portable power bank as a power source. I might have missed something in the code. Please help me out. Thank you.
Here is the code I created:
#include <Servo.h>
#include <ezButton.h>
//constants won't change
const int BUTTON_PIN = 7;
const int BUTTON_PIN2 = 6;
const int SERVO_PIN = 9;
const int SERVO_PIN2 = 8;
const int SERVO_PIN3 = 5;
const int SERVO_PIN4 = 4;
const int LED_PIN = 3;
int buttonState = 0;
int ledState = LOW;
ezButton button1(7);
ezButton button2(6);
Servo servo;
Servo servo2;
Servo servo3;
Servo servo4;
// variables will change:
int angle1 = 0;
int angle2 = 0;
int angle3 = 0;
int angle4 = 0;
void setup() {
Serial.begin(9600);
button1.setDebounceTime(15);
button2.setDebounceTime(15);
servo.attach(SERVO_PIN);
servo2.attach(SERVO_PIN2);
servo3.attach(SERVO_PIN3);
servo4.attach(SERVO_PIN4);
}
void loop() {
button1.loop();
button2.loop();
buttonState = digitalRead(BUTTON_PIN);
if(button1.isPressed() ) {
Serial.println("The button1 is pressed");
// toggle state of LED
ledState =! ledState;
// control LED arccoding to the toggleed sate
digitalWrite(LED_PIN, ledState);
// change angle of servo motor
if(angle1 == 0 ) {
angle1 = 90;
angle2 = 0;
}
else {
angle1 = 0;
angle2 = 90;
}
if(angle2 ==0) {
angle1 = 90;
angle2 = 0;
}
else {
angle1 = 0;
angle2 = 90;
}
//control servo motor according to the angle
servo.write(angle1);
delay(15);
servo4.write(angle2);
delay(15);
}
if(button2.isPressed() ) {
Serial.println("The button2 is pressed");
// change angle of servo motor
if(angle3 == 0 ) {
angle3 = 90;
angle4 = 0;
}
else {
angle3 = 0;
angle4 = 90;
}
if(angle4 ==0) {
angle3 = 90;
angle4 = 0;
}
else {
angle3 = 0;
angle4 = 90;
}
servo3.write(angle3);
delay(15);
servo2.write(angle4);
delay(15);
}
}