Go Down

Topic: LED mills problem...probably something stupid..but I'm stumped..please help :) (Read 491 times) previous topic - next topic

andigoodman

Hi...i'm new to arduino and programming but loving learning about it. So i got to the point where i asked the question "how do you get 2 loops working at the same time" and found the answer is you can't...probably best to use mills() to call functions...so thats where i'm at.

Im using fastLed to control a string of meds and where i'm heading is to create some kind of random rainfall effect....but i'm stumped early on.

rred is a random number (this works) and defines an LED...what is supposed to happen is that if rred>fromred, the loop is entered...this works as "mid" appears on the serial monitor. the line in red is where i'm stuck...if it's off (Black) then that part of the loop should be entered and if the time criteria is met then it goes red...this condition fails as the light does not turn red and mid1 never appears in the serial monitor...it carries on to the next part and mid2 appear in the monitor.

I know the code is scrappy but i have used this condition before to check the colour of an led and it has worked...i've even tried forcing it to black earlier in the loop to ensure the led wasn't in some undefined colour but no joy...

can anyone help...hope I've provided enough information :)


if (rred>fromred)  {
 
Serial.println("mid");
                if ( leds[rred]= CRGB::Black) {
Serial.println("mid1");
 
                                                  if (currentMillis - previousred_Millis >1) {
                                                  leds[rred-1] = CRGB :: Red;
                                                  FastLED.show();
                                                  delay(5000);
                                                  previousred_Millis += led_red_Interval;
                                                                                             }
                                                  }
                else {
                  Serial.println("mid2");
                                                  if (currentMillis - previousred_Millis >= blinkDuration_red) {
                                                    Serial.println("mid3");
                                                  leds[rred-1] = CRGB:: Black;
                                                  FastLED.show();
                                                   previousred_Millis += blinkDuration_red;
                                                                                                                }
   
                      }
}

else { rred=random (fromred,tored) ;
Serial.println("ended");
previousred_Millis += led_red_Interval;
}

Paul__B

Well now.

To start with, you need to use Auto Format (Ctrl-T) on the code in the IDE to help make sense of it.

Now, go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

While you are at it, post the actual code, not useless snippets.

Clearly, this code still includes a "delay()", so that isn't going to go anywhere.

JoeK1973

I may be misunderstanding the problem here, but if you want the CPU to switch between doing two different functions, then you can do that from the main loop() function. That's what I'm doing to switch between displaying two different pieces of information on an LCD matrix.

The way I've done it is through the use of a counter:

Code: [Select]

int counter = 0;

void setup(){
  //Setup devices (LCD, LED displays etc)
}

void loop(){
  if (counter <= 500) {
    // do first task here or call function that will do first task
  } else {
    // do second task here or call function that will do second task
    }
  if (counter >=1000) {
    counter = 0;
  }
  counter++;
}
 


In this way, the program can still read in data from pins, serial bus, etc, whilst also performing two seperate pieces of code. The amount of time the program spends doing each function can be adjusted by increasing or decreasing the number that the 'counter' variable counts to.

Hope this makes sense. I'm not an expert programmer but this worked for me.

andigoodman

thank for your replies...i think the issue is with the usage of the colour Black. I will post this in a more succinct thread and hopefully follow the forum instructions at the same time :)

thanks again

Paul__B

I will post this in a more succinct thread and hopefully follow the forum instructions at the same time
No, don't do that - just make the necessary corrections to the first post and continue here.


marco_c

FYI the problem has been explained in the thread pointed to by codlink
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

Go Up