I need help for my code

Hi,

I need to know if i can loop my effect when i press on bottom number 1 in mode bluetooth apps ?
Because when i press it, my code is only 1 turn and stops on the color and you have to press the button to redo the effect, so what to do?

my code :

#include <Adafruit_NeoPixel.h>
char data = 0;
#define PIN 6
#define NUM_LEDS 30
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
Serial.begin(9600);
strip.setBrightness(50);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}

// *** REPLACE FROM HERE ***
void loop() {
// ---> here we call the effect function <---
if(Serial.available() > 0)
{ data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");

if(data == '1')
RunningLights(0xff,0xff,0xff, 50);
}
}

// ---> here we define the effect function <---
void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
int Position=0;

for(int j=0; j<NUM_LEDS*2; j++)
{
Position++; // = 0; //Position + Rate;
for(int i=0; i<NUM_LEDS; i++) {
// sine wave, 3 offset waves make a rainbow!
//float level = sin(i+Position) * 127 + 128;
//setPixel(i,level,0,0);
//float level = sin(i+Position) * 127 + 128;
setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
((sin(i+Position) * 127 + 128)/255)*green,
((sin(i+Position) * 127 + 128)/255)*blue);
}
showStrip();
delay(WaveDelay);
}
}

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}

enum Modes {Idle, RunningLightsMode} Mode = Idle;

void loop()
{
  if (Serial.available() > 0)
  {
    data = Serial.read(); //Read the incoming data & store into data
    Serial.print(data);   //Print Value inside data in Serial monitor
    Serial.print("\n");

    switch (data)
    {
      case '1':
        Mode = RunningLightsMode;
        break
    }
  }

  // ---> here we call the effect function <---
  switch (Mode)
  {
    case Idle;
      // Do nothing when idle.
      break;

    case RunningLightsMode;
      RunningLights(0xff, 0xff, 0xff, 50);
      break;
  }
}

Hi Johnwassern thanks but error : 'RunningLightsMode' redeclared as different kind of symbol

helpmeplz41:
Hi Johnwassern thanks but error : 'RunningLightsMode' redeclared as different kind of symbol

So fix it. If you expect anyone to help you, post the COMPLETE output from the compiler. It told you EXACTLY which line has the problem. And, post the code YOU compiled. John did not post complete code, so it looks like YOU introduced the error.

helpmeplz41:
Hi Johnwassern thanks but error : 'RunningLightsMode' redeclared as different kind of symbol

The code I posted had three minor typos: a missing ';' after one "break", and ';' in place of ':' after two 'case' clauses. When I put my code in place of your loop() function and tried to verify, the typos were each pointed out in turn. After fixing those three typos the full sketch compiled without error or warning.

It sounds like you did not correctly insert the code I provided in place of your loop() function. Try again.

Hi,

Ok thanks i test that ^^

Thank you very much johnwasser, it works fine ^^.


Now i want add 3 bottom on my apps bluetooth on Appinventor :

Bottom 1 = green led effect runninglights

Bottom 2 = red led effect runninglights

Bottom 3 = orange led effect runninglights

Bottom 4 = OFF so black led


My code but i have error :

exit status 1
'RunningLights' was not declared in this scope


Code:

#include <Adafruit_NeoPixel.h>
char data = 0; //Variable for storing received data
#define PIN 6
#define NUM_LEDS 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup(){
Serial.begin(9600);
strip.begin();
strip.setBrightness(50);
strip.show(); // Initialize all pixels to 'off'
}

enum Modes {Idle, RunningLightsMode} Mode = Idle;

void loop()
{
if (Serial.available() > 0)
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");

switch (data)
{
case '1':
Mode = RunningLightsMode;
break;
}
}

// ---> here we call the effect function <---
switch (Mode)
{
case Idle:
// Do nothing when idle.
break;

case RunningLightsMode:
RunningLights(0,250,0, 50); //green
break;
}

switch (data)
{
case '2':
Mode = RunningLightsMode;
break;
}
}

// ---> here we call the effect function <---
switch (Mode)
{
case Idle:
// Do nothing when idle.
break;

case RunningLightsMode:
RunningLights(150, 0, 0, 50); //red
break;
}

switch (data)
{
case '3':
Mode = RunningLightsMode;
break;
}
}

// ---> here we call the effect function <---
switch (Mode)
{
case Idle:
// Do nothing when idle.
break;

case RunningLightsMode:
RunningLights(250, 120, 0, 50); //orange
break;
}

switch (data)
{
case '4':
Mode = RunningLightsMode;
break;
}
}

// ---> here we call the effect function <---
switch (Mode)
{
case Idle:
// Do nothing when idle.
break;

case RunningLightsMode:
RunningLights(); //off
break;
}
}

void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
int Position=0;

for(int j=0; j<NUM_LEDS*2; j++)
{
Position++; // = 0; //Position + Rate;
for(int i=0; i<NUM_LEDS; i++) {
// sine wave, 3 offset waves make a rainbow!
//float level = sin(i+Position) * 127 + 128;
//setPixel(i,level,0,0);
//float level = sin(i+Position) * 127 + 128;
setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
((sin(i+Position) * 127 + 128)/255)*green,
((sin(i+Position) * 127 + 128)/255)*blue);
}

showStrip();
delay(WaveDelay);
}
}

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}

It's good, i have find ^^, thanks very much johnwasser ^^

but does

      showStrip();
      delay(WaveDelay);
  }

within runningLights(),
assure you of a constantly responsive program ?

Don't use a 'switch' statement when you are only going to compare against one value. Use a switch statement when you are going to compare against a bunch of values. Also, don't use the same value for every Mode. Use a different value for every Mode so you can tell them apart.

#include <Adafruit_NeoPixel.h>
char data = 0;            //Variable for storing received data
#define PIN 6
#define NUM_LEDS 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  Serial.begin(9600);
  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
}

enum Modes {Idle, GreenMode, RedMode, OrangeMode, BlackMode} Mode = Idle;

void loop()
{
  if (Serial.available() > 0)
  {
    data = Serial.read(); //Read the incoming data & store into data
    Serial.print(data);   //Print Value inside data in Serial monitor
    Serial.print("\n");

    switch (data)
    {
      case '1':
        Mode = RedMode;
        break;

      case '2':
        Mode = GreenMode;
        break;

      case '3':
        Mode = OrangeMode;
        break;

      case '4':
        Mode = BlackMode;
        break;
    }
  }

  // ---> here we call the effect function <---
  switch (Mode)
  {
    case Idle:
      // Do nothing when idle.
      break;

    case GreenMode:
      RunningLights(0, 250, 0, 50); //green
      break;

    case RedMode:
      RunningLights(150, 0, 0, 50); //red
      break;

    case OrangeMode:
      RunningLights(250, 120, 0, 50); //orange
      break;

    case BlackMode:
      RunningLights(0, 0, 0, 50); //off
      break;
  }
}

void RunningLights(byte red, byte green, byte blue, int WaveDelay)
{
  int Position = 0;

  for (int j = 0; j < NUM_LEDS * 2; j++)
  {
    Position++; // = 0; //Position + Rate;
    for (int i = 0; i < NUM_LEDS; i++)
    {
      // sine wave, 3 offset waves make a rainbow!
      //float level = sin(i+Position) * 127 + 128;
      //setPixel(i,level,0,0);
      //float level = sin(i+Position) * 127 + 128;
      setPixel(i, ((sin(i + Position) * 127 + 128) / 255)*red,
               ((sin(i + Position) * 127 + 128) / 255)*green,
               ((sin(i + Position) * 127 + 128) / 255)*blue);
    }

    showStrip();
    delay(WaveDelay);
  }
}

void showStrip()
{
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue)
{
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue)
{
  for (int i = 0; i < NUM_LEDS; i++ )
  {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

Hi,

Thanks, i have do the same thing but i add in enum : RunningLightsMode1, RunningLightsMode2, RunningLightsMode3, RunningLightsMode4

But Now, i want no delay between each mode because i have delays to 2 and 3 secondes between each.

I don't know how reduce the delay or erase this delay.

Thanks if you know how fix it

Deva_Rishi:
but does

     showStrip();

delay(WaveDelay);
 }



within runningLights(),
assure you of a constantly responsive program ?

Hi,

No, I have delay between each mode and i want no delay between each mode, my code :

#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal.h>
char data = 0; //Variable for storing received data
#define PIN 6
#define NUM_LEDS 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
Serial.begin(9600);
lcd.begin(16, 2);

strip.begin();
strip.setBrightness(50);
strip.show(); // Initialize all pixels to 'off'
}

enum Modes {Idle, RunningLightsMode, RunningLightsMode2, RunningLightsMode3, RunningLightsMode4} Mode = Idle;

void loop()
{

if (Serial.available() > 0)
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");

switch (data)
{
case '1':
Mode = RunningLightsMode;
lcd.clear();
lcd.print("OPEN");
break;

case '2':
Mode = RunningLightsMode2;
lcd.clear();
lcd.print("CLOSE");
break;

case '3':
Mode = RunningLightsMode3;
lcd.clear();
lcd.print("BUSY");
break;

case '0':
Mode = RunningLightsMode4;
lcd.clear();
lcd.print("OFF");
break;
}
}

// ---> here we call the effect function <---
switch (Mode)
{
case Idle:
// Do nothing when idle.
break;

case RunningLightsMode:
RunningLights(0, 250, 0, 50);
break;

case RunningLightsMode2:
RunningLights(150, 0, 0, 50);
break;

case RunningLightsMode3:
RunningLights(250, 100, 0, 50);
break;

case RunningLightsMode4:
RunningLights(0, 0, 0, 0);
break;
}
}

void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
int Position=0;

for(int j=0; j<NUM_LEDS*2; j++)
{
Position++; // = 0; //Position + Rate;
for(int i=0; i<NUM_LEDS; i++) {
// sine wave, 3 offset waves make a rainbow!
//float level = sin(i+Position) * 127 + 128;
//setPixel(i,level,0,0);
//float level = sin(i+Position) * 127 + 128;
setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
((sin(i+Position) * 127 + 128)/255)*green,
((sin(i+Position) * 127 + 128)/255)*blue);
}

showStrip();
delay(WaveDelay);
}
}

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}

No, I have delay between each mode and i want no delay between each mode, my code :

Since your code for the pattern is of a blocking nature, you will always have to wait until the pattern is complete (although you might be able to do something by redefining the interrupt vector that is triggered when a byte is received on the UART, but i doubt if that is the easiest and best solution) So i suggest you changing the RunningLights() function in such a way that it takes a step every time it is called. (or calculates it's position depending on elapsed time)
Also while you are at it, simplify your code by making a decision on the Neopixel/FastLED (i prefer Neopixel it has better error checking) and you could change the calculation for the rainbow into a linear 16-bit integer calculation, it will look just as good (if not better) and is a lot easier and faster.

Ah actually you could even get away with changing your RunningLights() like this :

void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
  int Position=0;
  
  for(int j=0; j<NUM_LEDS*2; j++)
  {
      Position++; // = 0; //Position + Rate;
      for(int i=0; i<NUM_LEDS; i++) {
        // sine wave, 3 offset waves make a rainbow!
        //float level = sin(i+Position) * 127 + 128;
        //setPixel(i,level,0,0);
        //float level = sin(i+Position) * 127 + 128;
        setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
                   ((sin(i+Position) * 127 + 128)/255)*green,
                   ((sin(i+Position) * 127 + 128)/255)*blue);
      }
      
      showStrip();
      if (Serial.available()) return;  // it's not pretty but it will sort of do what you want
      delay(WaveDelay);
  }
}

It will also exit the the function when incorrect data has been received, but it will exit.

Deva_Rishi:
Ah actually you could even get away with changing your RunningLights() like this :

void RunningLights(byte red, byte green, byte blue, int WaveDelay) {

int Position=0;
 
  for(int j=0; j<NUM_LEDS*2; j++)
  {
      Position++; // = 0; //Position + Rate;
      for(int i=0; i<NUM_LEDS; i++) {
        // sine wave, 3 offset waves make a rainbow!
        //float level = sin(i+Position) * 127 + 128;
        //setPixel(i,level,0,0);
        //float level = sin(i+Position) * 127 + 128;
        setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
                  ((sin(i+Position) * 127 + 128)/255)*green,
                  ((sin(i+Position) * 127 + 128)/255)*blue);
      }
     
      showStrip();
      if (Serial.available()) return;  // it's not pretty but it will sort of do what you want
      delay(WaveDelay);
  }
}


It will also exit the the function when incorrect data has been received, but it will exit.

Hi,

Thanks, it works very well.