Menu - Code example doesn't loop?

I am new to Blynk and am writing a sketch to control neopixel strips with a menu system. I followed the code example but my sketch kicks out after each selection instead of staying in a loop. I know I am missing something simple, I just cant see it. The code isn’t long as most of the menu options have not been filled out yet. Thanks in advance for any help. - Jeff

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * 
 *
 **************************************************************
 * 
 * Pylon Controller for ESP 8266
 *
 *
 **************************************************************/

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <SimpleTimer.h>
#include <ESP8266WebServer.h>

#define PIN 15 // GPIO pin
#define NUMPIXELS 60 // Number of pixels in strip
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

SimpleTimer timer;
SimpleTimer quickTimer;

char auth[] = "TOKEN";  // Auth Token from the Blynk App.

// Setup
void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "PWORD");
  strip.begin();
}

// Menu Section
  BLYNK_WRITE(V1) {
  switch (param.asInt())
  {
    case 1: { // Solid Red
      colorWipe(strip.Color(255, 0, 0), 0);
      break;
    }
    case 2: { // Solid Green
       colorWipe(strip.Color(0, 255, 0), 0);
       break;
    }
    case 3: { // Solid Blue
       colorWipe(strip.Color(0, 0, 255), 0);
      break;
    }
    case 4: { // Solid Yellow
       colorWipe(strip.Color(255, 255, 0), 0);
      break;
    }
    case 5: { // Solid Magents
       colorWipe(strip.Color(255, 0, 255), 0);
      break;
    }
    case 6: { // Solid Cyan
       colorWipe(strip.Color(0, 255, 255), 0);
      break;
    }
    case 7: { // Flashing Red
      Serial.println("Item 7 selected");
      break;
    }
    case 8: { // Flashing Green
      Serial.println("Item 8 selected");
      break;
    }
    case 9: { // Flashing Blue
      Serial.println("Item 9 selected");
      break;
    }
    case 10: { // Flashing Yellow
      Serial.println("Item 10 selected");
      break;
    }
    case 11: { // Flashing Magenta
      Serial.println("Item 11 selected");
      break;
    }
    case 12: { // Flashing Cyan
      Serial.println("Item 12 selected");
      break;
    }
    case 13: { // Stripe Red
      colorWipe(strip.Color(255, 0, 0), 50);
      break;
    }
    case 14: { // Stripe Green
      colorWipe(strip.Color(0, 255, 0), 50);
      break;
    }
    case 15: { // Stripe Blue
      colorWipe(strip.Color(0, 0, 255), 50);
      break;
    }
    case 16: { // Stripe Yellow
      colorWipe(strip.Color(255, 255, 0), 50);
      break;
    }
    case 17: { // Stripe Magenta
      colorWipe(strip.Color(255, 0, 255), 50);
      break;
    }
    case 18: { // Stripe Cyan
      colorWipe(strip.Color(0, 255, 255), 50);
      break;
    }
    case 19: { // Rainbow
      rainbow(20);
      break;
    }
    case 20: { // Moving Rainbow
      rainbowCycle(20);
      break;
    }
    case 21: { // Fire
      Serial.println("Item 21 selected");
      break;
    }
    case 22: { // Snow
      Serial.println("Item 22 selected");
      break;
    }
    case 23: { // Checker
      Serial.println("Item 23 selected");
      break;
    }
    case 24: { // unused 1
      Serial.println("Item 24 selected");
      break;
    }
    case 25: { // unused 25
      colorWipe(strip.Color(255, 0, 0), 50);
      break;
    }
    default: {
      Serial.println("Unknown item selected");
    }
  }
}

// Main Loop
void loop() {
  timer.run();
  quickTimer.run();
  Blynk.run();
}

// Color Wheel Subroutine
// Input a value 0 to 255 to get a color value. The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

// ColorWipe Subroutine: Fill the dots bottom to top with a color
void colorWipe(uint32_t c, uint8_t wait) {
 for(uint16_t i=strip.numPixels(); i>=0; i--) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

// Rainbow Subroutine: Create Rainbow from top to bottom
void rainbow(uint8_t) {
  uint16_t i, j;
  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
  }
}

// Moving Rainbow Subroutine: Cycle Rainbow from top to bottom
void rainbowCycle(uint8_t) {
  uint16_t i, j;
  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
  }
}

@jparisse format your sketch, study SimpleTimer (missing setInterval and only one instance of timer required). If you are still having problems explain what “sketch kicks out” means (in detail).

Thank you Costas…

I formatted the sketch. I am not clear on Timer yet, I just know that Blynk suggest not to use Delay and to include Timer instead. I commented out references to running timer until I learn more.

The sketch runs until one chooses a menu item. If the user selects a menu item, that item is executed and then the program stops running.

I wish to have the the program continue to run so that the user can select other menu items (neopixel scripts) without the program stopping.

Thanks again… I know how difficult it is to answer newbie questions… I promise to get smarter faster…

Jeff

/**************************************************************
   Blynk is a platform with iOS and Android apps to control
   Arduino, Raspberry Pi and the likes over the Internet.
   You can easily build graphic interfaces for all your
   projects by simply dragging and dropping widgets.

     Downloads, docs, tutorials: http://www.blynk.cc
     Blynk community:            http://community.blynk.cc
     Social networks:            http://www.fb.com/blynkapp
                                 http://twitter.com/blynk_app

   Blynk library is licensed under MIT license


 **************************************************************

   Pylon Controller for ESP 8266 - Jeff W. Parisse


 **************************************************************/

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <SimpleTimer.h>
#include <ESP8266WebServer.h>

#define PIN 15 // GPIO pin
#define NUMPIXELS 60 // Number of pixels in strip
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

SimpleTimer timer;

char auth[] = "TOKEN";  // Auth Token from the Blynk App.

// Setup
void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "PWORDl");
  strip.begin();
}

// Menu Section
BLYNK_WRITE(V1) {
  switch (param.asInt())
  {
    case 1: { // Solid Red
        colorWipe(strip.Color(255, 0, 0), 0);
        break;
      }
    case 2: { // Solid Green
        colorWipe(strip.Color(0, 255, 0), 0);
        break;
      }
    case 3: { // Solid Blue
        colorWipe(strip.Color(0, 0, 255), 0);
        break;
      }
    case 4: { // Solid Yellow
        colorWipe(strip.Color(255, 255, 0), 0);
        break;
      }
    case 5: { // Solid Magents
        colorWipe(strip.Color(255, 0, 255), 0);
        break;
      }
    case 6: { // Solid Cyan
        colorWipe(strip.Color(0, 255, 255), 0);
        break;
      }
    case 7: { // Flashing Red
        Serial.println("Item 7 selected");
        break;
      }
    case 8: { // Flashing Green
        Serial.println("Item 8 selected");
        break;
      }
    case 9: { // Flashing Blue
        Serial.println("Item 9 selected");
        break;
      }
    case 10: { // Flashing Yellow
        Serial.println("Item 10 selected");
        break;
      }
    case 11: { // Flashing Magenta
        Serial.println("Item 11 selected");
        break;
      }
    case 12: { // Flashing Cyan
        Serial.println("Item 12 selected");
        break;
      }
    case 13: { // Stripe Red
        colorWipe(strip.Color(255, 0, 0), 50);
        break;
      }
    case 14: { // Stripe Green
        colorWipe(strip.Color(0, 255, 0), 50);
        break;
      }
    case 15: { // Stripe Blue
        colorWipe(strip.Color(0, 0, 255), 50);
        break;
      }
    case 16: { // Stripe Yellow
        colorWipe(strip.Color(255, 255, 0), 50);
        break;
      }
    case 17: { // Stripe Magenta
        colorWipe(strip.Color(255, 0, 255), 50);
        break;
      }
    case 18: { // Stripe Cyan
        colorWipe(strip.Color(0, 255, 255), 50);
        break;
      }
    case 19: { // Rainbow
        rainbow(20);
        break;
      }
    case 20: { // Moving Rainbow
        rainbowCycle(20);
        break;
      }
    case 21: { // Fire
        Serial.println("Item 21 selected");
        break;
      }
    case 22: { // Snow
        Serial.println("Item 22 selected");
        break;
      }
    case 23: { // Checker
        Serial.println("Item 23 selected");
        break;
      }
    case 24: { // unused 1
        Serial.println("Item 24 selected");
        break;
      }
    case 25: { // unused 25
        colorWipe(strip.Color(255, 0, 0), 50);
        break;
      }
    default: {
        Serial.println("Unknown item selected");
      }
  }
}

// Main Loop
void loop() {
//  timer.run();
  Blynk.run();
}

// Color Wheel Subroutine
// Input a value 0 to 255 to get a color value. The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

// ColorWipe Subroutine: Fill the dots bottom to top with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = strip.numPixels(); i >= 0; i--) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

// Rainbow Subroutine: Create Rainbow from top to bottom
void rainbow(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
  }
}

// Moving Rainbow Subroutine: Cycle Rainbow from top to bottom
void rainbowCycle(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
  }
}

The posting of your sketch is not formatted.

Hey I’d recommend ditching the Neopixel library and going with the FastLED library.

I’ve built something similar if you want to try it then modify it to work for you.

Thanks Jamin… Once I get this working, I will look into the FastLED library as you suggest. In the meantime, do you see anything in the code that would cause the action described?

Costas,

It seems that cut and paste removes formatting. I will look into forum behavor and see if I can find out how to upload a sketch with the formatting intact.

Jeff

thanks Jamin !!! That was easy… reformatted as requested…

1 Like

Don’t worry, everyone has issues with their first code post :wink:

I don’t see the setup for the timers? Not that you’re using or need them but worth noting.

quickTimer.setInterval(3000L, thefuncton);
timer.setInterval(3000L, thefuncton);

Otherwise this is my attempt (bare in mind that I am at work and am writing this without compile/testing in IDE):

#define BLYNK_PRINT Serial // this should be first
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
//#include <SimpleTimer.h> // dont need this
//#include <ESP8266WebServer.h> // or this

#define PIN 15 // GPIO pin
#define NUMPIXELS 60 // Number of pixels in strip

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

char auth[] = "TOKEN";  // Auth Token from the Blynk App.

int varMenuSelection;

// Setup
void setup() {
   varMenuSelection = 1; // maybe set your boot up colour or use blynk.sync(pin) to sync the last used setting
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "PWORDl");
  strip.begin();
}

// Menu Section

BLYNK_WRITE(V1) {
   varMenuSelection = param.asInt(); // set global var that is called in loop by doTheSwitch();
   Serial.print("Item ");
   Serial.print(varMenuSelection );
   Serial.println(" selected");
}

// Dont serial.print in doTheSwitch as it will loop the print output
void doTheSwitch(){
  switch (varMenuSelection)
  {
    case 1: { colorWipe(strip.Color(255, 0, 0), 0); break; } // Solid Red
    case 2: { colorWipe(strip.Color(0, 255, 0), 0); break; } // Solid Green
    case 3: { colorWipe(strip.Color(0, 0, 255), 0); break; } // Solid Blue
    case 4: { colorWipe(strip.Color(255, 255, 0), 0); break; } // Solid Yellow
    case 5: { colorWipe(strip.Color(255, 0, 255), 0); break; } // Solid Magents
    case 6: { colorWipe(strip.Color(0, 255, 255), 0); break; } // Solid Cyan
    case 7: { break; } // Flashing Red
    case 8: { break; } // Flashing Green
    case 9: { break; } // Flashing Blue
    case 10: { break; } // Flashing Yellow
    case 11: { break; } // Flashing Magenta
    case 12: { break; } // Flashing Cyan
    case 13: { colorWipe(strip.Color(255, 0, 0), 50); break; } // Stripe Red
    case 14: { colorWipe(strip.Color(0, 255, 0), 50); break; } // Stripe Green
    case 15: { colorWipe(strip.Color(0, 0, 255), 50); break; } // Stripe Blue
    case 16: { colorWipe(strip.Color(255, 255, 0), 50); break; } // Stripe Yellow
    case 17: { colorWipe(strip.Color(255, 0, 255), 50); break; } // Stripe Magenta
    case 18: { colorWipe(strip.Color(0, 255, 255), 50); break; } // Stripe Cyan
    case 19: { rainbow(20); break; } // Rainbow
    case 20: { rainbowCycle(20); break; } // Moving Rainbow
    case 21: { break; } // Fire
    case 22: { break; } // Snow
    case 23: { break; } // Checker
    case 24: { break; } // unused 1
    case 25: { colorWipe(strip.Color(255, 0, 0), 50); break; } // unused 25
  }
}

// Main Loop
void loop() {
  Blynk.run();
  doTheSwitch(); 
  strip.show(); // be sure to constantly SHOW the selected colour after each loop
}

// Color Wheel Subroutine
// Input a value 0 to 255 to get a color value. The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

// ColorWipe Subroutine: Fill the dots bottom to top with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = strip.numPixels(); i >= 0; i--) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

// Rainbow Subroutine: Create Rainbow from top to bottom
void rainbow(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
  }
}

// Moving Rainbow Subroutine: Cycle Rainbow from top to bottom
void rainbowCycle(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
  }
}

Thanks Jamin! How did you know this was my first code post? :grin:

I will pour over what you posted after the house gets quiet… Thanks for the help…

Also, I looked over what you have going on with your posted FastLED project and it looks similar to what I wish to accomplish. Once I have enough experience in Blynk to be useful, I’ll pop over and see what you’ve got going on. Thanks again… I will report back here with my success.

Jeff

1 Like
// Lamp Controller for ESP 8266

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>

#define PIN 15 // GPIO pin
#define NUMPIXELS 60 // Number of pixels in strip
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "TOKEN";

int varMenuSelection = 1;

void setup()
{
  int varMenuSelection = 1; // maybe set your boot up colour or use blynk.sync(pin) to sync the last used setting
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "PWORD");
  pixels.begin();
}

BLYNK_WRITE(V3) {
  varMenuSelection = param.asInt(); // set global var that is called in loop by doTheSwitch();
}

void doTheSwitch() {
  switch (varMenuSelection)
  {
    case 1: {
        colorWipe(pixels.Color(255, 0, 0), 0);  // Solid Red
        break;
      }
    case 2: {
        colorWipe(pixels.Color(0, 255, 0), 0);  // Solid Green
        break;
      }
    case 3: {
        colorWipe(pixels.Color(0, 0, 255), 0);  // Solid Blue
        break;
      }
    case 4: {
        colorWipe(pixels.Color(255, 255, 0), 0);  // Solid Yellow
        break;
      }
    case 5: {
        colorWipe(pixels.Color(255, 0, 255), 0);  // Solid Magents
        break;
      }
    case 6: {
        colorWipe(pixels.Color(0, 255, 255), 0);  // Solid Cyan
        break;
      }
    case 7: {
        break;  // Flashing Red
      }
    case 8: {
        break;  // Flashing Green
      }
    case 9: {
        break;  // Flashing Blue
      }
    case 10: {
        break;  // Flashing Yellow
      }
    case 11: {
        break;  // Flashing Magenta
      }
    case 12: {
        break;  // Flashing Cyan
      }
    case 13: {
        colorWipe(pixels.Color(255, 0, 0), 50);  // Stripe Red
        break;
      }
    case 14: {
        colorWipe(pixels.Color(0, 255, 0), 50);  // Stripe Green
        break;
      }
    case 15: {
        colorWipe(pixels.Color(0, 0, 255), 50);  // Stripe Blue
        break;
      }
    case 16: {
        colorWipe(pixels.Color(255, 255, 0), 50);  // Stripe Yellow
        break;
      }
    case 17: {
        colorWipe(pixels.Color(255, 0, 255), 50);  // Stripe Magenta
        break;
      }
    case 18: {
        colorWipe(pixels.Color(0, 255, 255), 50);  // Stripe Cyan
        break;
      }
    case 19: {
        rainbow(20);  // Rainbow
        break;
      }
    case 20: {
        rainbowCycle(20);  // Moving Rainbow
        break;
      }
    case 21: {
        break;  // Fire
      }
    case 22: {
        break;  // Snow
      }
    case 23: {
        break;  // Checker
      }
    case 24: {
        break;  // unused 1
      }
    case 25: {
        colorWipe(pixels.Color(255, 0, 0), 50);  // unused 25
        break;
      }
  }
}

void loop()
{
  Blynk.run();
  doTheSwitch();
  pixels.show(); // be sure to constantly SHOW the selected colour after each loop
}


// Color Wheel Subroutine
// Input a value 0 to 255 to get a color value. The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

// ColorWipe Subroutine: Fill the dots bottom to top with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = pixels.numPixels(); i >= 0; i--) {
    pixels.setPixelColor(i, c);
    pixels.show();
    delay(wait);
  }
}

// Rainbow Subroutine: Create Rainbow from top to bottom
void rainbow(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256; j++) {
    for (i = 0; i < pixels.numPixels(); i++) {
      pixels.setPixelColor(i, Wheel((i + j) & 255));
    }
    pixels.show();
  }
}

// Moving Rainbow Subroutine: Cycle Rainbow from top to bottom
void rainbowCycle(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < pixels.numPixels(); i++) {
      pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
    }
    pixels.show();
  }
}

Jamin… so I looked over your code… read up on BLYNK_WRITE() and virtual write and attempted to understand how the menu is implemented but I just cant seem to get the sketch to work (it compiles). I get no action from the hardware.

I can get a bunch of things working in Blynk including a manual controller using the Zebra and virtual pins. This code however is getting me down… I cant seem to make it work…

Do you or anybody else reading this thread know what I am missing? Thanks.

Jeff

Compile it, upload it to the hardware and check the Serial monitor to see if it’s connecting fine to Blynk.
Load up the app and create a slider with a range of 1-25 on Virtual Port 3
Run the project and slider the sider… it should start changing colours?
If no joy or responce from the hardware… then we will have to set up a small test to see if the Virtual port wrote correctly.

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>

#define PIN 15 // GPIO pin
#define NUMPIXELS 60 // Number of pixels in strip
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "TOKEN";

void setup()
{
Serial.begin(9600);
 Blynk.begin(auth, "SSID", "PWORD");
 pixels.begin();
}

BLYNK_WRITE(V1) // Widget WRITEs to Virtual Pin V1
{

int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
Serial.println(R);
Serial.println(G);
Serial.println(B);
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(R,G,B)); // Moderately bright green color.

pixels.show(); // This sends the updated pixel color to the hardware.

}
}

void loop()
{
Blynk.run();
}

The code above connects and runs with the zebra widget. With the exact same credentials, the code in the post above this one does not run. Even a widget connected to GIOPs doesn’t work (run, execute, etc.). The serial monitor doesn’t display anything probably because there are no serial print commands… Or is the WiFi handshake just supposed to show up… What is the best way to monitor the connection handshake?

Jeff

If you define the blynk_serial, yes, it always should output Serial info when Blynk is connecting (assuming you initialized the serial port :wink: )

Hmmm…

I am getting garbage on the Arduino IDE Serial Monitor as if there is a baud mismatch. I reason that this is the serial output to which you refer. This is the same serial connection used to upload code at 115kbaud and setting that upload value to 9600 (just as a check) didn’t make a difference. I checked in Windows Device Manager and COM8 seems OK reporting 9600 N81, etc…

Something is not right… I have used this port, cable etc. (in fact, I build the above project at Adafruit IO just to make sure it works) and I can communicate while uploading. Is there another serial port that I should be monitoring? I would still like to debug this Blynk project since their phone interface is so pretty.

In the meantime, I am going to work off Blynk’s published menu sample code and slowly add code until it breaks.

Thanks for the input…

Jeff

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "TOKEN";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "PWORD");
}

BLYNK_WRITE(V1) {
  switch (param.asInt())
  {
    case 1: { // Item 1
      Serial.println("Item 1 selected");
      break;
    }
    case 2: { // Item 2
      Serial.println("Item 2 selected");
      break;
    }
    case 3: { // Item 3
      Serial.println("Item 3 selected");
      break;
    }
    default: {
      Serial.println("Unknown item selected");
    }
  }
}

void loop()
{
  Blynk.run();
}

Finally, I have at least some good news to report. I got the above sketch to work (also meaning that the serial monitor works) so I have at least a working menu framework. As mentioned, I will slowly add code till it breaks. :wink:

Jeff

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#include <SPI.h>

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define PIN 15 // GPIO pin
#define NUMPIXELS 60 // Number of pixels in strip

int varMenu = 1;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "TOKEN";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "PWORD");
  pixels.begin();
}

BLYNK_WRITE(V0) // Widget WRITEs to Virtual Pin V0
{
  int R = param[0].asInt();
  int G = param[1].asInt();
  int B = param[2].asInt();
  Serial.println(R);
  Serial.println(G);
  Serial.println(B);
  for (int i = 0; i < NUMPIXELS; i++) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(R, G, B)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.
  }
}


BLYNK_WRITE(V1) {
  varMenu = param.asInt();

  switch (varMenu)
  {
    case 1: { // Item 1
        Serial.println("Item 1 selected");
        colorWipe(pixels.Color(255, 0, 0), 50);  // Stripe Red
        break;
      }
    case 2: { // Item 2
        Serial.println("Item 2 selected");
        colorWipe(pixels.Color(0, 0, 255), 50);  // Stripe Blue
        break;
      }
    case 3: { // Item 3
        Serial.println("Item 3 selected");
        rainbow(20);  // Rainbow
        break;
      }
    default: {
        Serial.println("Unknown item selected");
      }
  }
}
  

void loop()
{
  Blynk.run();
  pixels.show();
}

// Color Wheel Subroutine
// Input a value 0 to 255 to get a color value. The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

// ColorWipe Subroutine: Fill the dots bottom to top with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = pixels.numPixels(); i >= 0; i--) {
    pixels.setPixelColor(i, c);
    pixels.show();
    delay(wait);
  }
}

// Rainbow Subroutine: Create Rainbow from top to bottom
void rainbow(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256; j++) {
    for (i = 0; i < pixels.numPixels(); i++) {
      pixels.setPixelColor(i, Wheel((i + j) & 255));
    }
    pixels.show();
  }
}

// Moving Rainbow Subroutine: Cycle Rainbow from top to bottom
void rainbowCycle(uint8_t) {
  uint16_t i, j;
  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < pixels.numPixels(); i++) {
      pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
    }
    pixels.show();
  }
}

Well this is as close as I can get. The Sketch works until the user selects a menu item. The correct subroutine runs once and then the sketch stops running (ie. widgets that work stop working). I tried to make a menu selection subroutine as Ben Jammin did but placing the subroutine call in the loop section kills the sketch entirely (ie. widgets wont work at all).

Thanks in advance…

Jeff

This is really bad idea, You are killing your connection to the server because the loop runs thousands of times per second. Try and add the pixels.show() to the case statement instead of the loop :slight_smile:

I removed that line from the loop.

Any ideas how to make the menu call subroutines more than once? The sketch still bombs out once a menu selection is made and run once.

Thanks.

Jeff

@jparisse I don’t think the Menu is a problem, it is probably the functions you are calling in the Menu.

As a test change the function calls to something like:

Serial.println(“function X called”);

If this identifies the problem you need to fix your functions i.e if they “hog” the processor the ESP will crash because it has functions of it’s own that it must carry out at intervals (WiFi etc) and you will be timed out from the Blynk server.