1

I got a breadboard with 10 LEDs, 4 of them fade, to be specific: 3, 5, 6 and 9 are working. 0, 1, 2, 4, 7, 8 are not working.

I'm using LEDs and resistors on all spots, they're all the same. Tried changing things around but the problem doesn't seem to be the resistors or the LEDs.

I'm using the following code (I'll highlight the parts for the fading, by marking it between //** comments).

Does anyone know a way to fix this? Or see any problem?

const int inButton = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int delayTime = 100;
int brightness = 0;
int fadeAmount = 5;

void setup() {
  //**
  pinMode(inButton, INPUT);
  for (int out = 0; out < 10; out++){
    pinMode(out, OUTPUT);
  }
  //**

  pinMode(testLed, OUTPUT);
}

void loop() {
  buttonState = digitalRead(inButton);
  if (buttonState != lastButtonState){
    if (buttonState == 1){
      buttonPushCounter++;
    }
  }

  lastButtonState = buttonState;

  if (buttonPushCounter % 5 == 0){
    for (int i = 0; i < 10; i++){
      digitalWrite(i, 1);
      delay(delayTime / 2);
      digitalWrite(i, 0);   
    }
  }
  else if (buttonPushCounter % 5 == 1){
    for (int i = 0, j = 9; i < 10, j >= 0; i++, j--){    
      digitalWrite (i, 1);
      digitalWrite (j, 1);
      delay(delayTime / 2);
      digitalWrite (i, 0);
      digitalWrite (j, 0);
    } 
  }
  else if (buttonPushCounter % 5 == 2){   
    for (int i = 0; i < 10; i+=2){
      digitalWrite(i, 1);
    } 
    delay(delayTime);
    for (int i = 0; i < 10; i+=2){
      digitalWrite(i, 0);
    }
    delay(delayTime);
    for (int i = 1; i < 10; i+=2){
      digitalWrite(i, 1);
    }
    delay(delayTime);
    for (int i = 1; i < 10; i+=2){
      digitalWrite(i, 0);
    }
  } 
  else if (buttonPushCounter % 5 == 3){
    for (int i = 0 ; i < 10 ; i++){
      analogWrite(i, brightness);
    }
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255){
      fadeAmount = -fadeAmount; 
    }
    delay(25);      
  }
  else {
    int R1 = random(10);
    int R2 = random(10);
    int R3 = random(10);
    int R4 = random(10);
    digitalWrite(R1, 1);
    digitalWrite(R2, 1);
    digitalWrite(R3, 1);
    digitalWrite(R4, 1);
    delay(delayTime);
    digitalWrite(R1, 0);
    digitalWrite(R2, 0);
    digitalWrite(R3, 0);
    digitalWrite(R4, 0);
  }
}
4

Only pins that support PWM support analogWrite() fully. 3, 5, 6, 9, 10, and 11 do. The rest do not.

  • Ahh, that explains a lot! Is there any way around this, to get them working anyway? – Kees Bakker Dec 9 '14 at 9:38
  • You need to either implement software PWM or use a chip with more PWM channels such as the TLC5940. – Ignacio Vazquez-Abrams Dec 9 '14 at 9:40
  • Alright, thanks for the help, I'll start looking into that! – Kees Bakker Dec 9 '14 at 9:44
2

To add to @ignacio-vazquez-abrams answer, the pins that support analogWrite() have ~ sign next to their number (on my Uno board they are 9~, 10~, 11~ e.g.);

  • It's worth noting that this doesn't necessarily apply to 3rd-party clones/derivatives of Arduino boards. – Peter Bloomfield Dec 9 '14 at 11:45

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.