Creating a loop within Void Loop

Hey all,

I am working on trying to get a subset of my code to loop within Void Loop. The code works as is, but having this particular part of the code within Void Loop causes the LED Strip to move more slowly than it would without this particular part of the code crunching its own numbers. Let me show you what I mean.

#include <FastLED.h>
#include <NewPing.h>
#define DATA_PIN 7
#define NUM_LEDS 151
#define BRIGHTNESS 96
#define LED_TYPE WS2813
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

CRGBPalette16 currentPalette( CRGB::Black);

CRGBPalette16 targetPalette( PartyColors_p );
const int numReadings = 20 ;
int readings [ numReadings ] ;
int readIndex = 0;
int total = 0;
int average = 0;
int newValue = 0;
int lastpixel = 0;

#define UPDATES_PER_SECOND 100
#define EP1 12 // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define TP1 13
#define TP2 11
#define EP2 10
#define MAX_DISTANCE 64 // Maximum distance we want to ping for (in centimeters). Maximum sensor dista
#define SONAR_NUM 2
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(11, 10, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(13, 2, MAX_DISTANCE),
};

void setup() {
delay( 3000 ); // power-up safety delay
Serial.begin(115200);
Serial.println("resetting");
LEDS.addLeds<WS2813,DATA_PIN,RGB>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);
for(int thisReading = 0 ; thisReading < numReadings ; thisReading++)
{ readings [ thisReading ] = 0 ; }
int proxcont;
proxcont = sonar[1].ping_cm();
int colorchange;
colorchange = sonar[0].ping_cm();

int brightsense = 160 - 2.5 * proxcont;
if(proxcont <= 0 ) {
brightsense = 240; }
if(proxcont >= 64 ) {
brightsense = 240; }

FastLED.setBrightness(140);

}

void loop()
{
ChangePalettePeriodically();

uint8_t maxChanges = 48;
nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);

static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);

FastLED.show();
FastLED.delay(1 / UPDATES_PER_SECOND);

for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
delay(1); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print(i);
Serial.print("=");
Serial.print(sonar*.ping_cm());*

  • Serial.print("cm ");*
  • }*
  • Serial.println();*
  • delay(1);*

}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{

  • uint8_t brightness = 255;*

  • for( int i = 0; i < NUM_LEDS; i++) {*
    leds = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);
    * colorIndex += 3;*
    * }*
    }
    void ChangePalettePeriodically()
    {
    int colorchange;
    colorchange = sonar[0].ping_cm();
    uint8_t secondHand;
    secondHand = colorchange;
    * CRGB p = CHSV( HUE_PURPLE, 255, 255);
    CRGB g = CHSV( HUE_GREEN, 255, 255);
    _
    CRGB b = CRGB::Black;_
    _
    CRGB w = CRGB::White;_
    _
    if( secondHand == 0) { targetPalette = CRGBPalette16( b,b,b,w, b,b,b,w, b,b,b,w, b,b,b,w); }_
    _
    if( secondHand == 10) { targetPalette = CRGBPalette16( b,b,b,w, b,b,b,w, b,b,b,w, b,b,b,w); }_
    _
    if( secondHand == 20) { targetPalette = CRGBPalette16( b,b,b,w, b,b,b,w, b,b,b,w, b,b,b,w); }_
    if( secondHand == 30) { targetPalette = LavaColors_p; }
    if( secondHand == 40) { targetPalette = LavaColors_p; }
    if( secondHand == 50) { targetPalette = LavaColors_p; }
    _
    }_
    I need this portion of my code to run without interrupting the ChangePalettePeriodically() portion of the code because it causes the LED Strip to pause (milliseconds) each time that the for statement executes and slows down the lights on the strip:
    for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    _
    delay(1); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings._
    _
    Serial.print(i);_
    _
    Serial.print("=");_
    Serial.print(sonar.ping_cm());
    _ Serial.print("cm ");
    }
    Serial.println();
    delay(1);*
    I was able to "fix" this problem by setting the both delays in the for statement to "1", but I figure that there is a better way to solve this._

The first thing to do is to read this before posting a programming question then Auto Format your code in the IDE and post it using code tags to prevent it being mangled because it contains HTML commands such as [­i] which turns following text into italics