Go Down

Topic: LEDs turning off - not supposed to! (Read 1 time) previous topic - next topic

bvandyke

Ok, here is a code segment.  My leds are turning off randomly in this code (they should be dimming slightly).  My other code works fine on them.  It is just this section. 

Using a Arduino Nano for the controller. 

Initially though leds might be over heating but others (same spec) are staying on constantly.

Any ideas?

This is in the main loop.

 for (int y = 0; y < 1000; y++) {
   
    int x = random(1,6);
        Serial.print(y);
        Serial.print("     ");
      Serial.println(x);
     switch(x)
      {
       case 1:
            analogWrite(led1, 220);
            delay(750);
            analogWrite(led1, 256);
       case 2:
            analogWrite(led2, 220);
            delay(750);
            analogWrite(led2, 256);
        case 3:
            analogWrite(led3, 220);
            delay(750);
            analogWrite(led3, 256);
        case 4:     
            analogWrite(led4, 220);
            delay(750);
            analogWrite(led4, 256);
        case 5:     
            analogWrite(led5, 220);
            delay(750);
            analogWrite(led5, 256);
        case 6:     
            analogWrite(led6, 220);
            delay(750);
            analogWrite(led6, 256);
      }
      delay(10000);
  } 

larryd

#1
Feb 19, 2016, 03:16 am Last Edit: Feb 19, 2016, 03:17 am by LarryD
For PWM PINS, the duty cycle: between 0 (always off) and 255 (always on).

To get help, you must show us your "complete" sketch. Attach your code using the </> icon on the left side of the posting menu.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

bvandyke

Sorry, here is the full code:
Code: [Select]
const int led1=3;
const int led2=5;
const int led3=6;
const int led4=9;
const int led5=10;
const int led6=11;

const int tran=12;

const int BUTTON=2;

boolean lastButton = LOW;
boolean currentButton = LOW;

void setup() {

  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (led4, OUTPUT);
  pinMode (led5, OUTPUT);
  pinMode (led6, OUTPUT);
  pinMode (BUTTON, INPUT);
  pinMode (tran, OUTPUT);

  Serial.begin(9600);
 
}

void stage1() {
  digitalWrite(tran, HIGH);
  for (int i=0; i<64; i++)
  { 
     analogWrite(led1, i);
     analogWrite(led2, i);
     analogWrite(led3, i);
     analogWrite(led4, i);
     analogWrite(led5, i);
     analogWrite(led6, i);

     int x = random(1,6);
     delay(100);
     switch(x)
      {
       case 1:
            analogWrite(led1, i*0.5);
       case 2:
            analogWrite(led2, i*0.5);
        case 3:
            analogWrite(led3, i*0.5);
        case 4:     
            analogWrite(led4, i*0.5);
        case 5:     
            analogWrite(led5, i*0.5);
        case 6:     
            analogWrite(led6, i*0.5);
      }
    delay(100);
 
    switch(x)
      {
        case 1:
            analogWrite(led1, i);
        case 2:
            analogWrite(led2, i);
        case 3:
            analogWrite(led3, i);
        case 4:
            analogWrite(led4, i);
        case 5:     
            analogWrite(led5, i);
        case 6:     
            analogWrite(led6, i);
      }
      Serial.print("section 1: ");
      Serial.println(i);
  }
delay(100);
}

void stage2() {
for (int i=64; i<128; i++)
  {
     analogWrite(led1, i);
     analogWrite(led2, i);
     analogWrite(led3, i);
     analogWrite(led4, i);
     analogWrite(led5, i);
     analogWrite(led6, i);

     delay(100);
    int x = random(1,6);

     switch(x)
      {
       case 1:
            analogWrite(led1, i*0.63);
       case 2:
            analogWrite(led2, i*0.63);
        case 3:
            analogWrite(led3, i*0.63);
        case 4:     
            analogWrite(led4, i*0.63);
        case 5:     
            analogWrite(led5, i*0.63);
        case 6:     
            analogWrite(led6, i*0.63);
      }
     delay(100);
    switch(x)
      {
        case 1:
            analogWrite(led1, i);
        case 2:
            analogWrite(led2, i);
        case 3:
            analogWrite(led3, i);
        case 4:
            analogWrite(led4, i);
        case 5:     
            analogWrite(led5, i);
        case 6:     
            analogWrite(led6, i);
      }
      Serial.print("section 2: ");
      Serial.println(i);
  }
delay(100); 
}

void stage3() {
  for (int i=128; i<192; i++)
  {
     analogWrite(led1, i);
     analogWrite(led2, i);
     analogWrite(led3, i);
     analogWrite(led4, i);
     analogWrite(led5, i);
     analogWrite(led6, i);

    delay(100);
    int x = random(1,6);

     switch(x)
      {
       case 1:
            analogWrite(led1, i*0.76);
       case 2:
            analogWrite(led2, i*0.76);
        case 3:
            analogWrite(led3, i*0.76);
        case 4:     
            analogWrite(led4, i*0.76);
         case 5:     
            analogWrite(led5, i*0.76);
        case 6:     
            analogWrite(led6, i*0.76);
      }
     delay(100);
    switch(x)
      {
        case 1:
            analogWrite(led1, i);
        case 2:
            analogWrite(led2, i);
        case 3:
            analogWrite(led3, i);
        case 4:
            analogWrite(led4, i);
        case 5:     
            analogWrite(led5, i);
        case 6:     
            analogWrite(led6, i);
      }
      Serial.print("section 3: ");
      Serial.println(i);
  }
delay(100);
}

void stage4() {
 
for (int i=192; i<256; i++)
  {
     analogWrite(led1, i);
     analogWrite(led2, i);
     analogWrite(led3, i);
     analogWrite(led4, i);
     analogWrite(led5, i);
     analogWrite(led6, i);
     delay(100);
    int x = random(1,6);

     switch(x)
      {
       case 1:
            analogWrite(led1, i*0.89);
       case 2:
            analogWrite(led2, i*0.89);
        case 3:
            analogWrite(led3, i*0.89);
        case 4:     
            analogWrite(led4, i*0.89);
        case 5:     
            analogWrite(led5, i*0.89);
        case 6:     
            analogWrite(led6, i*0.89);
      }
     delay(100);
    switch(x)
      {
        case 1:
            analogWrite(led1, i);
        case 2:
            analogWrite(led2, i);
        case 3:
            analogWrite(led3, i);
        case 4:
            analogWrite(led4, i);
        case 5:     
            analogWrite(led5, i);
        case 6:     
            analogWrite(led6, i);
      }
      Serial.print("section 4: ");
      Serial.println(i);
  }
  delay(100);
}


void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Main");
  stage1();
  stage2();
  stage3();
  stage4();

  for (int y = 0; y < 1000; y++) {
   
    int x = random(0,6)+1;
        Serial.print(y);
        Serial.print("     ");
      Serial.println(x);
     switch(x)
      {
       case 1:
            analogWrite(led1, 220);
            delay(750);
            analogWrite(led1, 256);
       case 2:
            analogWrite(led2, 220);
            delay(750);
            analogWrite(led2, 256);
        case 3:
            analogWrite(led3, 220);
            delay(750);
            analogWrite(led3, 256);
        case 4:     
            analogWrite(led4, 220);
            delay(750);
            analogWrite(led4, 256);
        case 5:     
            analogWrite(led5, 220);
            delay(750);
            analogWrite(led5, 256);
        case 6:     
            analogWrite(led6, 220);
            delay(750);
            analogWrite(led6, 256);
      }
      delay(100);
  } 

analogWrite(led1, 0);
analogWrite(led2, 0);
analogWrite(led3, 0);
analogWrite(led4, 0);
analogWrite(led5, 0);
analogWrite(led6, 0);

}

bvandyke

an update:

Watching code on console.  The LEDs are turning off on this command:  analogWrite(led2, 220)  instead of dimming by a bit.

larryd

What does this line do?

analogWrite(led1, 256);

.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

bvandyke

That is making the LED come back on, but then turning right off again after the delay.

larryd

#6
Feb 19, 2016, 03:53 am Last Edit: Feb 19, 2016, 03:53 am by LarryD
What is the range of  X?

analogWrite(led1, X);
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

bvandyke

In the main loop - in switch statement, x should be 1 through 6

But where do you see analogWrite(led1, X) ?


larryd

#8
Feb 19, 2016, 04:11 am Last Edit: Feb 19, 2016, 04:12 am by LarryD
analogWrite(led1, X);
The range of X is: an integer from 0 to 255. You have 256.
analogWrite(led1, 256);
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

bvandyke

Thanks!, I could have looked at that for weeks and not seen it.  Used to programming with values going from 1 not 0.


larryd

Also:
analogWrite(led6, i*0.63);

You do realize, the second argument is to be an INTEGER, not a FLOAT.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

bvandyke

Yes, but it get close enough and makes the flicker ramp up like I want.

I'm using it for engines on a star wars star destroyer to make it look like they are coming up to full power.


larryd

Don't let it get to full power, it could explode!
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Grumpy_Mike

You do realise don't you that the code, if written properly, reduces that case statement to just three lines?
The same is true of the other very much longer code you posted in another thread.

Go Up