Hi All, I have a problem with a script I am working on.The section

Hi All,

I have a problem with a script I am working on.The section Void Fastled works fine scrolling 5 leds along the strip.Now Void Fast is only scrolling 1 led along the strip in reverse.I can not work out why there is only 1 led scrolling.
Thank you in advance for any help.Code below.

#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) >= 25) {
FASTLED();
}
if (sensors.getTempCByIndex(1) >= 25) {
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>=0; 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);
}

Please put code on http://gist.github.com and share the link in posts.

If your NUM_LEDS is 150, your for loops need to use 0 to 149 (ie. NUM_LEDS -1). Trying to write to pixels outside of you pixel range causes strange and bad things to happen.

Hi Marc,
Oh sorry for that.I did not realise.Yes I have tried using 0 to 149 but still the same result.

When your for loop index is 149, and then in the function you add +1 to +4 you’re going outside the range. You either need a check to avoid that, or to wrap it back around to the other end of the strip. Using modulo is one way to wrap it, see this example:

Hi Marc,

Thank you for that.Much appreciated.I will take a look at that.

DS18B20s have a nasty habit of crashing (especially with long wires), I think they return a temperature of -147C when they do. You should check for and handle this.

@Jeremy_Spencer
Hi Jeremy,
Oh I did not know that thank http://you.Do you know of any other equivalent with unique id’s please ? Or is there a way around this.

@Michael_Slevin The best way have found, is to connect them with relays, and when they crash, power them down, wait, then power them back up. Bigger pullup resistors help a lot with longer wires. See the guide below, can’t remember where I found it.

/
Try 1.9kΩ resistor

d) Be sure to put multiple 1-Wire chips in a daisy chain, not a “star”…
i.e. ONE run of two wires, with 1-wire chips across it, like the rungs of a ladder…
do NOT have several wires leading out from where your Arduino is, with 1-Wire chips on each of the several “rays” of the “star”.

if (SensorErrorCount > 10) // ongoing bus error
{

oneWire.reset(); // reset 1-wire using lib function
digitalWrite(SENSORSUPPLY, LOW); // disable sensor supply
// log
LogGLDC(“rst 1W”);
Serial.println(“Reset 1 Wire bus”);

delay(5000);

digitalWrite(SENSORSUPPLY, HIGH); // enable sensor supply
SensorErrorCount = 0; // reset count
}

I had the same problem using a Cat5 cable about 80 feet/24.25 meters long, powering a single DS18B20 with 5V and using
a 4.7k resistor on the far end of the cable. In the version of the OneWire library I started with, the read_bit() timing
was set to pull the data line low for 1 microseconds then high for 5 microseconds before reading the bit sent back from the DS18B20.
I found that pulling the data line low for 2 microseconds then high for 7-15 microseconds made it work reliably.
Then I tried the Version 2 library, which pulls the data line low for 3 microseconds then high for 9 microseconds before reading the bit.
This seems to work fine as well. I suspect the problem is that the time constant of the 4.7k resistor multiplied by the capacitance of
the cable results in a slow rise time of the data line and consequently reading 1 bits as 0s. But I don’t have my o’scope here to test that hypothesis.
/

@Michael_Slevin See this forum
https://forum.arduino.cc/index.php?topic=20574.0

@Jeremy_Spencer

Hi Jeremy,
Thank you for that.Very much appreciated.I was also thinking of using cat5 cables for the sensors too.I think that would help a bit.

@Michael_Slevin I have two sensors in my thermal solar panels, I used alarm wire. They’re about 7 or 8m from the controller.

@Jeremy_Spencer
Hi Jeremy,
Im thinking around 50 mtrs.Maybe I will try rf modules.

@Michael_Slevin I’d read the data locally with an esp8266 and then send it to the hub via WiFi. Cheaper then 50m of CAT5 cable :wink:

@Jeremy_Spencer Yes good idea.Thank you Jeremy.I will buy one and see how it pans out.

@Michael_Slevin I still cant figure out why I am only getting 1 led scrolling though.Its driving me nuts now.