Taking a longer time than normal for void loop()

Hi there,

I’ve recently bought a 1 meter long Adafruit Dotstar strip and am trying to work on the strip alternating colors. My code is down below. (Sorry for any badly named variables) Some of the code is based off the strandtest example in the Adafruit_Dotstar library for Arduino. So, here’s my problem: the strip runs fine when I put in the Serial “red” or any other color (basically when rainOn. However, when I put in “rain,” it takes significantly longer to get through the void loop(). Any suggestions about why it takes significantly longer to run through the if(rainOn) statement and what it encompasses when it’s true? Also, when I send text through the Serial, it cuts off the first letter. Not really sure why. Oh, and just to clarify, the entire code thus far is meant so the user can send text to the Serial and the LEDs will turn that color OR when rainOn is on, each LED will start from Color(0,0,0) and eventually get to Color(255, 255, 0) before it restarts as Color(0,0,0). It alternates down the strip in this way. Sorry for any style errors or inconviences. Thanks for any help!

(I’m using an Arduino Uno)

#include <Adafruit_DotStar.h>

// Because conditional #includes don’t work w/Arduino sketches…
#include <SPI.h> // COMMENT OUT THIS LINE FOR GEMMA OR TRINKET
//#include <avr/power.h> // ENABLE THIS LINE FOR GEMMA OR TRINKET

#define NUMPIXELS 144 // Number of LEDs in strip

// Here’s how to control the LEDs from any two pins:
#define DATAPIN 4
#define CLOCKPIN 5
Adafruit_DotStar strip = Adafruit_DotStar(
NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BGR);
// The last parameter is optional – this is the color data order of the
// DotStar strip, which has changed over time in different production runs.
// Your code just uses R,G,B colors, the library then reassigns as needed.
// Default is DOTSTAR_BRG, so change this if you have an earlier strip.

// Hardware SPI is a little faster, but must be wired to specific pins
// (Arduino Uno = pin 11 for data, 13 for clock, other boards are different).
//Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BRG);

void setup() {
Serial.begin(9600);
Serial.println(“We are gucci”);
#if defined(AVR_ATtiny85) && (F_CPU == 16000000L)
clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif

strip.begin(); // Initialize pins for output
strip.show(); // Turn all LEDs off ASAP
}

// Runs 10 LEDs at a time along strip, cycling through red, green and blue.
// This requires about 200 mA for all the ‘on’ piels + 1 mA per ‘off’ piel.

int head = 0, tail = -140; // Index of first ‘on’ and ‘off’ piels
uint32_t black = strip.Color(0,0,0);
uint32_t color = strip.Color(0, 0, 0); // ‘On’ color (starts red)
uint32_t red = strip.Color(255,0,0), blue = strip.Color(0,0,255);
uint32_t green = strip.Color(0,128,0), magPurp = strip.Color(160,0,160);
uint32_t yellow = strip.Color(255,255,0), orange = strip.Color(255,69,0);
uint32_t indigo = strip.Color(75,0,130);
int ii = 0, iii = 0, iiii = 0;

void turnOff()
{
for(int i = 0; i < NUMPIXELS; i++)
{
strip.setPixelColor(i, 0);
strip.show();
}
}

void colorMaker(String str)
{
if(str.equals(“yellow”)) { color = yellow; }
if(str.equals(“red”)){ color = red; }
if(str.equals(“green”)){ color = green;}
if(str.equals(“blue”)) {color = blue;}
if(str.equals(“mp”)){ color = magPurp;}
if(str.equals(“orange”)){color=orange;}
if(str.equals(“indigo”)){color=indigo;}

}
void loop() {
if(Serial.available() && Serial.read() != 0)
{
String str = Serial.readStringUntil(‘x’);
Serial.println(str);
if(str.equals(“rain”)) {rainOn = true;}
if(str.equals(“22”)) {rainOn = false;}
if(str.equals(“q”))
{
turnOff();
Serial.println(“Quitting”);
delay(100);
exit(0);
}
if(str.equals(“stop”))
{
turnOff();
Serial.println(“Clearing Colors”);
}
if(!(str.equals(“q”) || str.equals(“stop”)))
{
colorMaker(str);
}
}
if(rainOn)
{
int strTemp;
strTemp = Serial.parseInt();
if(strTemp == 22)
{
rainOn = false;
ii = 0, iii = 0;
return;
}
color = strip.Color(ii, iii, 0);
ii++;
if(ii == 256) { ii–; iii++; }
if(iii = 256) {ii = 0; iii = 0; }

}
strip.setPixelColor(head, color); // ‘On’ pixel at head
strip.setPixelColor(tail, 0); // ‘Off’ pixel at tail
strip.show(); // Refresh strip
delay(20); // Pause 20 milliseconds (~50 FPS)

if(++head >= NUMPIXELS) { // Increment head index. Off end of strip?
head = 0; // Yes, reset head index to start

}
if(++tail >= NUMPIXELS) tail = 0; // Increment, reset tail index
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

if(iii = 256)

Oh Oh

.

TomGeorge:
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Speaking of formatting to make things easier, why don't you use the chain icon on the toolbar to make that link clickable?

[url=http://forum.arduino.cc/index.php/topic,148850.0.html]http://forum.arduino.cc/index.php/topic,148850.0.html[/url]

does this:
http://forum.arduino.cc/index.php/topic,148850.0.html

I appreciate that you are trying to get people to use code tags, etc. But you're also setting a bad example of how to post URLs. Let's make it as easy as possible for people to read the "How to use this forum".

pert:
I appreciate that you are trying to get people to use code tags, etc. But you’re also setting a bad example of how to post URLs.

:slight_smile:

You use Serial.parseInt() which is a blocking function that can take up to 1 second (default value) to parse the incoming data.

If you have not configured Serial Monitor to send a e.g. linefeed, try that. Can you give examples (for e.g. red and rainon) of typical 'text' that you send from Serial Monitor.

You can read Serial Input Basics - updated to get ideas how to properly handle serial input.

And lastly, as you said, your naming of variables is bad.

Thank you sterretje!

pert:
Speaking of formatting to make things easier, why don't you use the chain icon on the toolbar to make that link clickable?

[url=http://forum.arduino.cc/index.php/topic,148850.0.html]http://forum.arduino.cc/index.php/topic,148850.0.html[/url]

does this:
http://forum.arduino.cc/index.php/topic,148850.0.html

I appreciate that you are trying to get people to use code tags, etc. But you're also setting a bad example of how to post URLs. Let's make it as easy as possible for people to read the "How to use this forum".

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Done... Tom...... :slight_smile: