Hello Arduino experts,
I am trying to make an LED pulsate (fade-in and fade-out) when a servo motor is in movement when a button is pressed. What I am trying to do is that when I press the button : 2 servo motor start turning and an LED starts fading in and out. I want the fading to stop when the motors stop or when the button is unpressed.
I got both servos to work as expected when pressing the button but the LED is not fading in the expected behavour, it seems more like it's flickering or fading inconsistently.
But I tested the code for fading (line 39-54) without the servo motors and the fading worked properly. but when I add them both together the LED doesn't pulsate as expected, any advice ? Here is the code and schematics :
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin1 3 //~
#define pushButtonPin 2
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
int angle = 179; // initial angle for servo (beteen 1 and 179)
int angleStep = 1;
const int minAngle = 0;
const int maxAngle = 180;
int pos = 0;
const int type =2;
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(servoPin1); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
//myservo.write(angle);//initial position
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
//begin LED dimming code
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
//end LED dimming
if (angle > 0 && angle <= 180) {
angle = angle - angleStep;
if(angle < 0){
angle = 0;
}
else{
myservo.write(angle); // move the servo to desired angle
}
}
delay(20); // waits for the servo to get there
}
if(digitalRead(pushButtonPin) == HIGH){
analogWrite(led, 0);
// Servo button demo by Robojax.com
if (angle >= 0 && angle <= 180) {
angle = angle + angleStep;
if(angle >180){
angle =180;
}
else{
myservo.write(angle); // move the servo to desired angle
}
}
delay(20); // waits for the servo to get there
}
}
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin1 3 //~
#define pushButtonPin 2
int led = 11; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
int angle = 179; // initial angle for servo (beteen 1 and 179)
int angleStep = 1;
const int minAngle = 0;
const int maxAngle = 180;
int pos = 0;
const int type =2;//watch video for details. Link is at the top of this code (robojax)
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(servoPin1); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
//myservo.write(angle);//initial position
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
//begin LED dimming code
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(10);
//end LED dimming
if (angle > 0 && angle <= 180) {
angle = angle - angleStep;
if(angle < 0){
angle = 0;
}
else{
myservo.write(angle); // move the servo to desired angle
}
}
delay(20); // waits for the servo to get there
}
if(digitalRead(pushButtonPin) == HIGH){
analogWrite(led, 0);
// Servo button demo by Robojax.com
if (angle >= 0 && angle <= 180) {
angle = angle + angleStep;
if(angle >180){
angle =180;
}
else{
myservo.write(angle); // move the servo to desired angle
}
}
delay(20); // waits for the servo to get there
}
}
Probably messed up your sketch, however, try this to see is the servo moves and the LED fades.
#include <Servo.h>
Servo myservo;
#define servoPin1 3
#define pushButtonPin 2
int led = 11;
int brightness = 0;
int fadeAmount = 1;
int angle = 179;
int angleStep = 1;
const int minAngle = 0;
const int maxAngle = 180;
int pos = 0;
const int type = 2;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
myservo.attach(servoPin1);
pinMode(pushButtonPin, INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
//myservo.write(angle);//initial position
} //END of setup()
void loop()
{
// P U S H E D
if (digitalRead(pushButtonPin) == LOW)
{
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness < 0 )
{
fadeAmount = -fadeAmount;
brightness = 0;
}
else if (brightness > 255)
{
fadeAmount = -fadeAmount;
brightness = 255;
}
delay(10);
if (angle >= 0 && angle <= 180)
{
angle = angle - angleStep;
if (angle < 0)
{
angle = 0;
}
myservo.write(angle);
}
delay(20); // waits for the servo to get there
}
// R E L E A S E D
if (digitalRead(pushButtonPin) == HIGH)
{
brightness = 0;
analogWrite(led, brightness);
if (angle <= 180)
{
angle = angle + angleStep;
if (angle > 180)
{
angle = 180;
}
myservo.write(angle);
}
delay(20); //waits for the servo to get there
}
} //END of loop()
Servo work but LED stays ON. I am also trying this on a simulator. I will try it using the hardware. How can I connect the servos on a different power source ? Also I will powering the Arduino using 12 V power (not USB)
I only tried it on the TinkerCad simulator I will be trying with the real hardware.
But is it ok how I am connecting multiple servos to the same pin ? if not how should I be connecting them ? NOTE: I power my Arduino Uno using 12 Vpower source NOT USB.