Hi guys,
I am having some trouble with an arduino project and I was hoping you could give me a little help.
I am trying to make an LED fade and a motor vibrate at the same time. I got them working separately, but when I try to combine them, the script dosn't work. They are supposed to work independent of each other.
Here is my script for the LED:
int led = 1;
int brightness = 0;
int fadeAmount= 5;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(50);
}
And my motor has this script:
const int motorPin = 2;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
digitalWrite(motorPin, HIGH);
delay(100);
digitalWrite(motorPin, LOW);
delay(780);
}
When I tried to combine them, I used the code below. The motor works well, but the LED is going crazy and I can't figure out why:
const int motorPin = 2;
int led = 1;
int brightness = 0;
int fadeAmount= 5;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(motorPin, HIGH);
delay(100);
digitalWrite(motorPin, LOW);
delay(780);
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(50);
}
![]()
Thank you in advance!