arduinoarduino-nano

Some LED fade and others will flash when all LED should flash Adrunio Nano


I am having trouble with my Arduino code and circuit. The goal is to have each LED fade one after another. This is not happening correctly. Some LED will fade up and down properly and then some will blink instead. I have been trying to troubleshoot and here is what I have done and has not fixed it.

  1. used a different board
  2. Swap LEDs
  3. Used different resistors
  4. Swapped pins that blink to pins that faded and the blink will be fading
  5. Moved circuit to a different breadboard
  6. Check that the code is sending the correct light level through the serial monitor

Here is a picture of my board Pictured is my circuit

Here is the code:

const int BUTTON = 2; // Naming switch button pin
const int LED1 = 3;   // Namin LED pin
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 8;
const int LED7 =9;
const int LED8 = 10;
const int LED9 = 11;

int BUTTONstate = 0; // A variable to store Button Status / Input


int brightness = 0;
int fadeAmount =5;

void setup(){ 
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
   pinMode(LED7, OUTPUT);
   pinMode(LED8, OUTPUT);
   pinMode(LED9, OUTPUT);
   

   pinMode (BUTTON, INPUT);
   Serial.begin(9600);
}

void loop() {
   BUTTONstate = digitalRead(BUTTON);  // Reading button status / input
   if (BUTTONstate == HIGH)  // Condition to check button input
   {
      FlashingLight();
  
   }
   else
   {
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
      digitalWrite(LED5, LOW);
      digitalWrite(LED6, LOW);
      digitalWrite(LED7, LOW);
      digitalWrite(LED8, LOW);
      digitalWrite(LED9, LOW);
      
  
   }
}
void FlashingLight()
{ 
  
  for ( int i = 0; i<=4;i++){
      
      //digitalWrite(LED, LOW);
    
    fading(LED1);  //Fades
    fading(LED2);  // blinks
    fading(LED3);  //fades
    fading(LED4);  //fades
    fading(LED5);  //blinks
    fading(LED6);  //blinks
    fading(LED7);  //fades
    fading(LED8);  //fades
    fading(LED9);  //fades

    delay(1000);
  }
}

void fading(int val) {
//brightness =0;
//digitalWrite(LED, LOW);
    analogWrite(val,brightness);
    for (brightness = 0; brightness <= 150; brightness += 5) {
      analogWrite(val,brightness);
  
      delay(30);
      Serial.println(brightness);
  
    }
    for (brightness = 150; brightness >= 0; brightness -= 5) {
    analogWrite(val,brightness);
  
    delay(30);
    Serial.println(brightness);
 
    }
    delay(100);
    //brightness =0;

}

Thank you for your help and let me know if you have any questions,


Solution

  • According to this Arduino.cc reference not all pins of the Arduino Nano are suitable for PWM control (using analogWrite()). On the Nano, only pins 3, 5, 6, 9, 10, 11 can be used to output a PWM signal.