Go Down

Topic: Help with a script (Read 338 times) previous topic - next topic

Bbkhawk

Hi all,

Could anyone please tell me why in the code below void Fastled I am getting 5 leds scrolling (which I want) but in the void Fast I am getting only one led scrolling.
Thank you all in advance.Any help gratefuly received.

Code: [Select]
[/#include <FastLED.h>
#include <OneWire.h>
#include <DallasTemperature.h>
 
 // Define the array of leds
#define LED_DT 6
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 150
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];
#define ONE_WIRE_BUS 2
 
int index1;
int index2;
int index3;
unsigned long initialtime;
 
OneWire oneWire(ONE_WIRE_BUS);
 
DallasTemperature sensors(&oneWire);
 
void setup() {
  delay (500);
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(max_bright);
  index1 = 0;
  index2 = 0;
  index3 = 0;
  initialtime = millis();
  //Serial.begin(9600);
  //Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.setResolution(9);
  sensors.begin();
}
 void loop() {
 
  sensors.requestTemperatures();
 
//Serial.print(sensors.getTempCByIndex(0));
  if (sensors.getTempCByIndex(0) >= 24) {
   FASTLED();
  }
  if (sensors.getTempCByIndex(1) >= 24) {
   FAST();
  }
}
 
// Repeat this with fast
void FASTLED() {
for(int a =0;a<=150; a++){ // ~105-110s use a-- to reverse and a=150
FastLED.clear();
  set5leds(index1);
  index1 = (index1 + 1) % (NUM_LEDS - 4);
  if (millis() - initialtime > 1000) {
   
  }
    set5leds(index2);
    index2 = (index2 + 1) % (NUM_LEDS - 4);
    if (millis() - initialtime > 2000) {
   
   set5leds(index3);
   index3 = (index3 + 1) % (NUM_LEDS - 4);
   if (millis() - initialtime > 3000) {
    }
  FastLED.show();
delay (0);
  }
 }
}
 void set5leds(int pos) {
  leds[pos].setRGB (0, 255, 0);
  leds[pos + 1].setRGB (0, 255, 0);
  leds[pos + 2].setRGB (0, 255, 0);
  leds[pos + 3].setRGB (0, 255, 0);
  leds[pos + 4].setRGB (0, 255, 0);
 }

 

 //Reverse
void FAST() {
for(int a =150;a>=10; a--){
FastLED.clear();                                                       
  set5leds2(index1);
  index1 = (index1 + 1) % (NUM_LEDS + 4);
  if (millis() - initialtime > 1000) {
   
  }
    set5leds2(index2);
    index2 = (index2 + 1) % (NUM_LEDS + 4);
    if (millis() - initialtime > 2000) {
   
   set5leds2(index3);
   index3 = (index3 + 1) % (NUM_LEDS + 4);
   if (millis() - initialtime > 3000) {
    }
  FastLED.show();
delay (0);
  }
 }
}

 

 void set5leds2(int pos) {
  pos = (NUM_LEDS-pos-1);
  leds[constrain(pos,0, NUM_LEDS)].setRGB (0, 255, 0);
  leds[constrain(pos,0, NUM_LEDS-1)].setRGB (0, 255, 0);
  leds[constrain(pos,0, NUM_LEDS-2)].setRGB (0, 255, 0);
  leds[constrain(pos,0, NUM_LEDS-3)].setRGB (0, 255, 0);
  leds[constrain(pos,0, NUM_LEDS-4)].setRGB (0, 255, 0);
}
code]

AWOL

#1
Sep 16, 2018, 08:30 pm Last Edit: Sep 16, 2018, 08:31 pm by AWOL
Code: [Select]
for(int a =0;a<=150; a++){do you have 151 LEDs, or only 150?

Where do you use 'a'?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Bbkhawk

Hi AWOL,

Thank you for your reply.I have 150 leds.Not sure what you mean by where do you use a though sorry.

Bbkhawk

Ah I think I see where you are going AWOL it should be 0 and 149 not 150 yes ?

Wawa

for(int a = 0; a < 150; a++) {

Grumpy_Mike

#5
Sep 16, 2018, 11:30 pm Last Edit: Sep 16, 2018, 11:35 pm by Grumpy_Mike
Code: [Select]
void FAST() {
for(int a =150;a>=10; a--){
FastLED.clear();                                                      
  set5leds2(index1);
  index1 = (index1 + 1) % (NUM_LEDS + 4);
  if (millis() - initialtime > 1000) {
  
  }


Count your { and } that bit of code makes no sense. It says if millis() - initialtime > 1000 then do nothing, otherwise do nothing.

then always do:-
Code: [Select]
set5leds2(index2);
    index2 = (index2 + 1) % (NUM_LEDS + 4);

and clear all the the LEDs each time round the for loop. So you end up with just that last operation at the end of the loop.

Place the cursor to the right of a { and click, the matching closing brace will be in a box so you can see your groups.

Quote
void Fastled ......... void Fast
They are called the Fastled function and the Fast function. Void is not part of the function's name, it just tells you what sort of variable it returns. Never speak of it again.  :)

Bbkhawk

Hi Wawa Hi Grumpy Mike,

Sorry guys you have totally lost me.I have only ever programmed in basic before so this is really confusing.
I really appreciate your help though.Thank you.

Wawa

There is a help>Reference tab in the IDE where you can look up basic commands like this.
Or Google "Arduino Reference".
Leo..

Bbkhawk

Hi Leo,

Thank you for that much appreciated.I will take a look.I was unaware of the tab.

Grumpy_Mike

Quote
Sorry guys you have totally lost me
So how much of that code did you write?
It is essential that you learn to read code before you start to write code.
Basically you wrote the Fast function wrong. Read it step by step, it is wrong where I said in my previous reply. But if you read the code you will find that out.

Bbkhawk

So how much of that code did you write?
It is essential that you learn to read code before you start to write code.
Basically you wrote the Fast function wrong. Read it step by step, it is wrong where I said in my previous reply. But if you read the code you will find that out.
Hi Grumpy Mike,

I just cobbled this together from bits I found on the internet.I read the code step by step as you advised but I can not see where it has gone wrong sorry.This is so frustrating.
thank you for your help and patience.

Grumpy_Mike

#11
Sep 19, 2018, 05:30 pm Last Edit: Sep 19, 2018, 05:34 pm by Grumpy_Mike
Quote
I read the code step by step as you advised but I can not see where it has gone wrong sorry.
You need to read it step by step but remember what it has done in previous steps. All these comments refer to the FAST function, the one you are having trouble with. Lets break things down.

1)
So what does this code do?
Code: [Select]
if (millis() - initialtime > 1000) {
  
  }

I say it does nothing no matter what value the variables millis() nor initialtime are. If you are reading the code what do you say it does?

2)
I would ask you the same question about this:-
Code: [Select]
 if (millis() - initialtime > 3000) {
    }


3)
So what does this code do?
Code: [Select]
void FAST() {
for(int a =150;a>=10; a--){
FastLED.clear();

I say that it loops with the variable a from 150 down to 10. Agreed? BUT every time round the loop any colours set in previous loop iterations are wiped out, so there is little point in setting anything but what is set in the last time round the loop. Have I lost you there?

In other words everything is wiped out but the last loop, so how many LEDs do you expect to see after this loop has run its course. I would expect the answer to that to be one, what do you expect?

4)
This bit of code:-
Code: [Select]
  set5leds2(index2);
    index2 = (index2 + 1) % (NUM_LEDS + 4);

Is always executed every time round to loop, it is not within the scope of an if statement, is that what you meant to do? If so then fine but I doubt it.

5)
What do you expect this to do?
Code: [Select]
delay (0);

Quote
I just cobbled this together from bits I found on the internet
The cobbling is wrong, you have not thought about what you want this function to do.

Do you think any of the above mistakes could account for:-
Quote
but in the void Fast I am getting only one led scrolling.

Go Up