I'm working on some code for a Nerf blaster with an LED indicator light. When attempting to use analogWrite to PWM my LED pins, only one pin (blue) behaves as expected and dims down. The other two (red, green) don't light up at all. The LED is confirmed working and the Arduino.cc fade example works fine on each pin individually when testing. Here is the code:
#include <ClickButton.h>
#include <Servo.h>
Servo esc;
ClickButton rev(6, LOW, CLICKBTN_PULLUP);
ClickButton mode(7, LOW, CLICKBTN_PULLUP);
ClickButton trig(8, LOW, CLICKBTN_PULLUP);
int flyPin = 2;
int escPin = 3;
int pushPhasePin = 4;
int pushSpeedPin = 5;
int RED = 9;
int GREEN = 10;
int BLUE = 11;
int m = 1;
int singleShot = 100;
bool shot = false;
void setup() {
pinMode(flyPin, OUTPUT);
pinMode(pushPhasePin, OUTPUT);
digitalWrite(pushPhasePin, HIGH);
pinMode(pushSpeedPin, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
esc.attach(escPin, 1000, 2000);
esc.write(0);
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
delay(500);
digitalWrite(RED, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
delay(500);
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
delay(500);
digitalWrite(RED, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
delay(1500);
}
void loop() {
rev.Update();
mode.Update();
trig.Update();
if (mode.clicks != 0) {
m++;
if (m > 3) {
m = 1;
}
}
if (m == 1) {
digitalWrite(RED, LOW);
digitalWrite(BLUE, LOW);
analogWrite(GREEN, 120);
}
else if (m == 2) {
digitalWrite(RED, LOW);
analogWrite(BLUE, 120);
digitalWrite(GREEN, LOW);
}
else if (m == 3) {
analogWrite(RED, 120);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
}
if (rev.depressed) {
digitalWrite(flyPin, HIGH);
esc.write(40);
}
else {
digitalWrite(flyPin, LOW);
esc.write(0);
}
if (trig.depressed) {
if (m == 1) {
if (!shot) {
digitalWrite(pushSpeedPin, HIGH);
}
delay(singleShot);
shot = true;
digitalWrite(pushSpeedPin, LOW);
}
if (m == 2) {
if (!shot) {
digitalWrite(pushSpeedPin, HIGH);
}
delay(singleShot * 3);
shot = true;
digitalWrite(pushSpeedPin, LOW);
}
if (m == 3) {
digitalWrite(pushSpeedPin, HIGH);
}
}
else {
digitalWrite(pushSpeedPin, LOW);
shot = false;
}
}
The relevant portion that is failing is under the "if m == 1,2,3" blocks. Where m [for mode] == 2, the blue light works but the other leds do not light when cycling through the modes. Note the leds are on PWM pins (9,10,11). Thanks for any help!
analogWrite(color, 20)
without all the other code? Does it work to turn the color on and off withdigitalWrite
? – piojo Feb 21 '18 at 9:28