0

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!

  • Does it work to call analogWrite(color, 20) without all the other code? Does it work to turn the color on and off with digitalWrite? – piojo Feb 21 '18 at 9:28
  • It does. The below answer is correct though! I missed that bit of info. – jjumbc Feb 21 '18 at 13:24
2

You are trying to combine the use of the Servo library and PWM on pins 9 and 10. You can't do both. PWM on pins 9 and 10 uses the same timer as the servo library.

As it states on the Servo library web page:

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.