"Theatre-style crawling lights" effect stop my web server (arduino Yun)

Hi,

First, I am not a developper, and I am French so sorry for my English.

I am doing my first project with Arduino Yun Mini.

I try to put some leds on a trolley, and control these leds by wifi.

It works. The electronic part is ok, and the code also.

But I want to make a little modification, and I am not able to do it.

I want to use the "Theatre Style Crawling lights" effect, but it need to do it until there is a new ordre from the web interface.

I try to use "Do...While" and "While" instruction, but when I do than, my web server is dead.

Do you avec any idea?

My code:

/*
Arduino YUN Put-to-light

 * "/arduino/digital/1"     -> switch On BIN 1
 * "/arduino/digital/2"     -> switch On BIN 2
 * "/arduino/digital/3"     -> switch On BIN 3
 * "/arduino/digital/4"     -> switch On BIN 4
 * "/arduino/digital/5"     -> switch On BIN 5
 * "/arduino/digital/6"     -> switch On BIN 6
 * "/arduino/digital/7"     -> switch On BIN 7
 * "/arduino/digital/8"     -> switch On BIN 8
 * "/arduino/digital/9"     -> switch On BIN 9


 */

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN 6

int NewRequest;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino 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(60, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
YunServer server;

void setup() {
  // Bridge startup
  Bridge.begin();
  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
  strip.begin();
  
  theaterChase(0, strip.numPixels(),strip.Color(127, 127, 127), 50); // White
  blackout();
}

void loop() {
  // Get clients coming from server
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    // Process request
    process(client);

    // Close connection and free resources.
    client.stop();
  }

  delay(50); // Poll every 50ms
}

void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');

  // is "digital" command?
  if (command == "ptl") {
    ptlCommand(client);
  }
}

void ptlCommand(YunClient client) {
  int bin;

  // Read bin number
  bin = client.parseInt();

blackout();

    switch (bin) {
    case 1:
      theaterChase(0,10,strip.Color(0,255,0),50); //Bin 1 green
      break;
    case 2:
      theaterChase(12,18,strip.Color(255,0,0),50); //Bin 2 red
      break;
    case 3:
      theaterChase(20,26,strip.Color(0,0,255),50); //Bin 3 blue
      break;
    case 4:
     theaterChase(20,26,strip.Color(255,255,0),50); //Bin 4 yelow
     break;
    case 5:
     theaterChase(27,33,strip.Color(255,0,0),50); //Bin 5 red
     break;
    case 6:
     theaterChase(35,41,strip.Color(255,0,0),50); //Bin 6 red
     break;
    case 7:
      theaterChase(42,48,strip.Color(255,255,0),50); //Bin 7 yelow
      break;
    case 8:
      theaterChase(42,48,strip.Color(0,0,255),50); //Bin 8 Blue
      break;
    case 9:
      theaterChase(49,60,strip.Color(0,255,0),50); //Bin 9 green
      break;
    default:
      Lighton(0,strip.numPixels(),strip.Color(255,0,0),50); //Error
      break;
    }

  // Send feedback to client
  client.print(F("Bin "));
  client.print(bin);
}

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

void Lighton(int firstled, int lastled,uint32_t c, uint8_t wait)
{
      for (int i=firstled; i<lastled; i++) {
      strip.setPixelColor(i,c); 
      strip.show();
      }
}

//Theatre-style crawling lights.
void theaterChase(int firstled, int lastled,uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=firstled; i < lastled; i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  } 
}

I want to use the "Theatre Style Crawling lights" effect, but it need to do it until there is a new ordre from the web interface.

You need to separate the "use the data from the web" part of your code from the "get the data from the web" part of the code.

Getting the data should be in one function. Using the data should be in another function. The using the data function MUST NOT BE BLOCKING.

theaterChase() is currently a blocking function. It will iterate the inner loop 30 times, with a delay() at the end of each iteration.

You need to re-write it using a state machine, so that theaterChase() determines if it is time to do something (as in the blink without delay example) and, if it is, does it and sets the stage for the next time.

There is a lot of value in what @PaulS has said, but I think he has missed the most important thing.

You are using a Yun. It has two computers. Run the webserver in Python on the Linux side and run the crawling lights on the Arduino side.

...R

Hi,

Thanks for these responses...

I use the way that @PaulS has said, as I have no idea of Python.

And it works...

Thanks a lot

Bidulle:
I have no idea of Python.

I'm glad you have a solution.

If you plan to do more with your Yun I do recommend learning Python. You may be interested in develop on PC deploy on Yun

...R

Here are some bookmarks related to doing what you want to do.

Arduino 3rd Party Projects
http://codesnippets.altervista.org/documentation/yun/3rdParty-Projects/3rdParty-Projects.html