Code not working with Blynk

Hello everyone, I’m using mega 2560 and this code is not working. But I don’t have any error in compilation. Can someone tell me If I have done anything wrong? Is there something worng with Blynk initialisation? I have tried everything. I’m using serial connection. There are 4 buttons V1 to V4 and conditions are mentioned in those functions if none of those conditions satisy the function named normal should run in loop. FYI it’s a traffic signal project. The code executes everything until Blynk.begin(Serial, auth); is mentioned in setup and after that it doesn’t do anything. Please help.

#include <Arduino.h>
#include <LiquidCrystal.h>
#include <BlynkSimpleStream.h>
#include <FastLED.h>
#include <SoftwareSerial.h>

#define LED_TYPE WS2811
#define NUM_LEDS_PER_STRIP 3
#define COLOR_ORDER BGR

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
SoftwareSerial DebugSerial(2, 3); // RX, TX

CRGB L1[NUM_LEDS_PER_STRIP];   //L1A Stud Alert ///Lane stud alerts don't forget to update before code pivot... 
CRGB L2[NUM_LEDS_PER_STRIP];   //L2A Stud Alert
CRGB L3[NUM_LEDS_PER_STRIP];   //L3A Stud Alert
CRGB L4[NUM_LEDS_PER_STRIP];   //L4A Stud Alert

char auth[] = "bMLxyWboMZ9YS";


int buttonstate1=0; 

unsigned long delayStart = 0; // the time the delay started
unsigned long light_delay = 1000; // the time the  delay runs
unsigned long half_delay = 500; 
unsigned long studs =200; 
unsigned long screen_delay =2500; 

const int L1G=2; ////////////////L1-GREEN
const int L1O=3; ////////////////L1-ORANGE
const int L1R=4; ////////////////L1-RED

const int L2G=5;   ////////////////L2-GREEN
const int L2O=6;   ////////////////L2-ORANGE
const int L2R=7;   ////////////////L2-RED

const int L3G=14;   ////////////////L3-GREEN
const int L3O=15;   ////////////////L3-ORANGE
const int L3R=16;   ////////////////L3-RED

const int L4G=17;   ////////////////L4-GREEN
const int L4O=18;   ////////////////L4-ORANGE
const int L4R=19;   ////////////////L4-RED


const int L1A=22;   ////////////////L1-Stud Alert
const int L2A=24;   ////////////////L2-Stud Alert
const int L3A=26;   ////////////////L3-Stud Alert
const int L4A=28;   ////////////////L4-Stud Alert 


void setup()
{
  FastLED.addLeds<WS2811, 23, BRG>(L1, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2811, 24, BRG>(L2, NUM_LEDS_PER_STRIP);//CRGB::Black; Raipur test
  FastLED.addLeds<WS2811, 26, BRG>(L3, NUM_LEDS_PER_STRIP);//CRGB::Black; 
  FastLED.addLeds<WS2811, 28, BRG>(L4, NUM_LEDS_PER_STRIP);//CRGB::Black;

  FastLED.clear();
  FastLED.show(); 

lcd.begin(16, 2); 

 Serial.begin(9600);

pinMode(L1G, OUTPUT);
pinMode(L1O, OUTPUT);
pinMode(L1R, OUTPUT);

pinMode(L2G, OUTPUT);
pinMode(L2O, OUTPUT);
pinMode(L2R, OUTPUT);

pinMode(L3G, OUTPUT);
pinMode(L3O, OUTPUT);
pinMode(L3R, OUTPUT);

pinMode(L4G, OUTPUT);
pinMode(L4O, OUTPUT);
pinMode(L4R, OUTPUT);

pinMode(L1A, OUTPUT);
pinMode(L2A, OUTPUT);
pinMode(L3A, OUTPUT);
pinMode(L4A, OUTPUT);


delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= screen_delay) {
  Serial.println("Ready");
  
}

lcd.clear();

delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= screen_delay) {
  Serial.println("Check");
  
}


delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= half_delay) {
  Serial.println("Lane 1 blink");
  digitalWrite(L1G, HIGH);digitalWrite(L1O, HIGH);digitalWrite(L1R, HIGH);
}


delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= half_delay) {
  digitalWrite(L1G, LOW);digitalWrite(L1O, LOW);digitalWrite(L1R, LOW);

  digitalWrite(L2G, HIGH);digitalWrite(L2O, HIGH);digitalWrite(L2R, HIGH);
}

delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= half_delay) {
  digitalWrite(L1G, LOW);digitalWrite(L1O, LOW);digitalWrite(L1R, LOW);

  digitalWrite(L2G, HIGH);digitalWrite(L2O, HIGH);digitalWrite(L2R, HIGH);
}

delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= half_delay) {
  digitalWrite(L2G, LOW);digitalWrite(L2O, LOW);digitalWrite(L2R, LOW);

  digitalWrite(L3G, HIGH);digitalWrite(L3O, HIGH);digitalWrite(L3R, HIGH);    
}

delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= half_delay) {
  digitalWrite(L3G, LOW);digitalWrite(L3O, LOW);digitalWrite(L3R, LOW);

  digitalWrite(L4G, HIGH);digitalWrite(L4O, HIGH);digitalWrite(L4R, HIGH);   
}

digitalWrite(L4G, LOW);digitalWrite(L4O, LOW);digitalWrite(L4R, LOW);


Serial.println("Shuru");

delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= screen_delay) 
{
  Serial.println("Test");

  
}

delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= light_delay) 
{
  digitalWrite(L1G, HIGH);digitalWrite(L1O, HIGH);digitalWrite(L1R, HIGH);//delay(10);
  digitalWrite(L2G, HIGH);digitalWrite(L2O, HIGH);digitalWrite(L2R, HIGH);//delay(10);
  digitalWrite(L3G, HIGH);digitalWrite(L3O, HIGH);digitalWrite(L3R, HIGH);//delay(10);
  digitalWrite(L4G, HIGH);digitalWrite(L4O, HIGH);digitalWrite(L4R, HIGH);
}

delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= light_delay) 
{
  // turn them off before start the loop
  digitalWrite(L1G, LOW);digitalWrite(L1O, LOW);digitalWrite(L1R, LOW);//delay(10);
  digitalWrite(L2G, LOW);digitalWrite(L2O, LOW);digitalWrite(L2R, LOW);//delay(10);
  digitalWrite(L3G, LOW);digitalWrite(L3O, LOW);digitalWrite(L3R, LOW);//delay(10);
  digitalWrite(L4G, LOW);digitalWrite(L4O, LOW);digitalWrite(L4R, LOW);
}
lcd.clear();
lcd.setCursor(0,0);lcd.print("   NORMAL LANE  ");
lcd.setCursor(0,1);
lcd.print(" MOVEMENT ACTIVE  ");

Blynk.begin(Serial, auth);
}

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

  if(buttonstate1 == 1)
 {

   lcd.setCursor(0,0);lcd.print("FIRST LANE PASS");
  digitalWrite(L1R, LOW);digitalWrite(L1G, HIGH);digitalWrite(L2R, HIGH);digitalWrite(L3R, HIGH);digitalWrite(L4R, HIGH); Serial.print(" First Lane Active"); //digitalWrite(L1A, HIGH); 
  
delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
  L1[0] = CRGB::Red; FastLED.setBrightness(64 );
  FastLED.show(); 
  }
  delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
  L1[0] = CRGB::Blue; 
  FastLED.show(); 
 }
/*  L2[0] = CRGB::Yellow; 
  FastLED.show(); 
  delay(200);*/
  digitalWrite(L1G, LOW); //digitalWrite(L1A, LOW); 
  L1[0] = CRGB::Black; 
 
        FastLED.show(); 
      
  }

}

BLYNK_WRITE(V2) 
{
  buttonstate1 = param.asInt();

  if(buttonstate1 == 1)
 {

 lcd.setCursor(0,0);lcd.print("SECOND LANE PASS");
digitalWrite(L2R, LOW);digitalWrite(L2G, HIGH);digitalWrite(L1R, HIGH);digitalWrite(L3R, HIGH);digitalWrite(L4R, HIGH);  Serial.print(" Second Lane Active"); //digitalWrite(L2A, HIGH);
 
 delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
  L2[0] = CRGB::Red; FastLED.setBrightness(64 );
  FastLED.show(); 
  }

  delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
  L2[0] = CRGB::Blue; 
  FastLED.show(); 
  }
/*  L2[0] = CRGB::Yellow; 
  FastLED.show(); 
  delay(200);*/
  digitalWrite(L2G, LOW);//digitalWrite(L2A, LOW);
 L2[0] = CRGB::Black; 
        FastLED.show(); 
      
  }

}

BLYNK_WRITE(V3) 
{
  buttonstate1 = param.asInt();

  if(buttonstate1 == 1)
 {
    lcd.setCursor(0,0);lcd.print("THIRD LANE PASS");
    digitalWrite(L3R, LOW);digitalWrite(L3G, HIGH);digitalWrite(L2R, HIGH);digitalWrite(L1R, HIGH);digitalWrite(L4R, HIGH); Serial.print(" Third Lane Active");//digitalWrite(L3A, HIGH);
    
    delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
    L3[0] = CRGB::Red; FastLED.setBrightness(64 );
    FastLED.show(); 
    }

    delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
    L3[0] = CRGB::Blue; 
    FastLED.show(); 
  }
  /*  L3[0] = CRGB::Yellow; 
    FastLED.show(); 
    delay(200);*/ 
    digitalWrite(L3G, LOW);//digitalWrite(L3A, LOW);
    L3[0] = CRGB::Black; 
    FastLED.show();
      
  }

}

BLYNK_WRITE(V4) 
{
  buttonstate1 = param.asInt();

  if(buttonstate1 == 1)
 {
    lcd.setCursor(0,0);lcd.print("FOURTH LANE PASS");
    digitalWrite(L4R, LOW);digitalWrite(L4G, HIGH);digitalWrite(L2R, HIGH);digitalWrite(L3R, HIGH);digitalWrite(L1R, HIGH); Serial.print(" Fourth Lane Active");//digitalWrite(L4A, HIGH);
    
    delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
    L4[0] = CRGB::Red; FastLED.setBrightness(255 );
    FastLED.show(); 
  }
    
    delayStart = millis(); // ok we are stating 
while ((millis() - delayStart) <= 200) 
  {
    L4[0] = CRGB::Blue; 
    FastLED.show(); 
  }
  /* L4[0] = CRGB::Yellow; 
    FastLED.show(); 
    delay(200);*/
    digitalWrite(L4G, LOW);//digitalWrite(L4A, LOW);
    L4[0] = CRGB::Black; 
    FastLED.show();
      
  }

}


void normal()
{

  //1st lane
  delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= light_delay)
  {
    digitalWrite(L1R, LOW);digitalWrite(L1G, HIGH);digitalWrite(L2R, HIGH);digitalWrite(L3R, HIGH);digitalWrite(L4R, HIGH);

  }

   delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= half_delay)
  {
    digitalWrite(L1G, LOW);digitalWrite(L1O, HIGH);
  }

  digitalWrite(L1O, LOW);digitalWrite(L1R, HIGH);


  //2nd lane
  delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= light_delay)
  {
    digitalWrite(L2R, LOW);digitalWrite(L2G, HIGH);digitalWrite(L1R, HIGH);digitalWrite(L3R, HIGH);digitalWrite(L4R, HIGH);
  }

   delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= half_delay)
  {
    digitalWrite(L2G, LOW);digitalWrite(L2O, HIGH);
  }

  digitalWrite(L2O, LOW);digitalWrite(L2R, HIGH);


  //3nd lane
  delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= light_delay)
  {
    digitalWrite(L3R, LOW);digitalWrite(L3G, HIGH);digitalWrite(L2R, HIGH);digitalWrite(L1R, HIGH);digitalWrite(L4R, HIGH);
  }

   delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= half_delay)
  {
    digitalWrite(L3G, LOW);digitalWrite(L3O, HIGH);
  }

  digitalWrite(L3O, LOW);digitalWrite(L3R, HIGH);

  //4th lane
  delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= light_delay)
  {
    digitalWrite(L4R, LOW);digitalWrite(L4G, HIGH);digitalWrite(L2R, HIGH);digitalWrite(L3R, HIGH);digitalWrite(L1R, HIGH);
  }

   delayStart = millis(); // ok we are stating 
  while ((millis() - delayStart) <= half_delay)
  {
    digitalWrite(L4G, LOW);digitalWrite(L4O, HIGH);
  }

  digitalWrite(L4O, LOW);digitalWrite(L4R, HIGH);

}

void loop()
{

normal();
Blynk.run();

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


Your sketch compiled without any problem. this message is normal from fast LED library. and keep in mind keep your void loop() clean.
remove the serial from Blynk.begin

In file included from C:\Users\*****\Documents\Arduino\sketch_mar30a\sketch_mar30a.ino:4:0:
C:\Users\****\Documents\Arduino\libraries\FastLED/FastLED.h:14:21: note: #pragma message: FastLED version 3.003.002
 #    pragma message "FastLED version 3.003.002"
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~

Yes even I got this. But how to solve it?

I tried doing it… It doesn’t compile… Throws errors

When you run your original code (with Blynk.begin(Serial, auth)) what do you see in your CMD window where you are running the Blynk script?
Have you modified the Blynk script?

How are you connecting your debug serial pins to your PCV (via an FTDI?) and what are you seeing in the debug serial monitor?

What version of the Blynk library are you using?

Pete.

I’m using Blynk Library 0.6.7. I’m connecting via USB and run the bat file that is there in scripts folder. My code execute until the Blynk.begin(); and doesn’t execute anything outside setup thereafter.

That answers this question…

but what about the rest of my questions, and what is the relevance of the PIO screenshot?

You should probably be running library version 0.6.1

Pete.

Is this is what you are asking for?

That answers the CMD window question.

Are you going to drip-feed me the answers to my questions one at a time?

Pete.

I’m not sure about the rest. But I can tell that I can use serial monitor or the blynk script at a time and not simultaneously…

So you don’t know whether you’ve modified the Blynk script (the .bat file) or whether you own a FTDI adapter?

I assume that this isn’t the actual Auth token that you’re using when trying to connect, and that you’ve disguised the real one for security purposes?

Pete.

Yes I have done it to avoid revealing my auth token… I haven’t modified any bat file. And I don’t have FTDI adapter.

Now I have no idea what to do to make it work… I have posted the code here…

The single most useful tool in this situation is the output from the debug serial port.
You need to see these messages, whilst the device is attempting to connect, to understand what is happening. You need an FTDI adapter for this.

The one other thing you could try (once you’ve downgraded to 0.6.1) is to force the use of port 8080, in case your ISP is blocking port 80.
This requites you to edit the script file. If you do a little searching of this forum you’ll find instructions on how to do that.

Pete.

That’s definitely not an issue from ISP because the other code is working totally fibe… This is the code that’s working fine -


// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

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

const int Green=2;
const int Orange=3;
const int Red=4;

int buttonstate1=0;


void setup() 
{
pinMode(Green, OUTPUT); 
pinMode(Orange, OUTPUT); 
pinMode(Red, OUTPUT); 

  Serial.begin(9600);
  Blynk.begin(Serial, auth);

}

BLYNK_WRITE(V1)
{
  buttonstate1 = param.asInt();
  if(buttonstate1 == 1){
  digitalWrite(Green, HIGH);digitalWrite(Orange, HIGH);digitalWrite(Red, HIGH);
  }
  else{
    digitalWrite(Green, LOW);digitalWrite(Orange, LOW);digitalWrite(Red, LOW);
  }
}

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

just on and off the LED. But it’s not working in anything complex code than this…

Can someone help?

My advice is to give some thought to how your devices will connect to the Blynk server in the long term.
Clearly, using the USB serial connection via a computer which has to be running and have the command prompt open 24/7 isn’t the solution.

If your ultimate connection method will be hard-wired Ethernet then you don’t have many choices, and you’ll struggle to get a reliable long-term solution.

If your ultimate connection method will be WiFi then you have many more choices. You could go down the route of using an ESP-01 as a WiFi modem for an Arduino Uno/Mega, but I really wouldn’t recommend it.
Instead I’d suggest using an ESP8266 or ESP32 instead of the Arduino Uno/Mega, as they both have built-n WiFi capabilities, far more memory, and the ability to update them via OTA.

That way, instead of spending money on an FTDI adapter to allow you to see what messages your Mega is giving you about why your serial USB connection is failing, you’ll be able to spend that money on the device that you will ultimately use in your long-term project.

Pete.

1 Like

Yes… I’m planning the same. But are you sure this is not because of the code and it’s happening due to some external reasons?

What you describe sounds like an Auth code error, or an ISP issue, but you say it’s neither of these.
Beyond that it’s impossible to say without some serial debugging.

As far as your code is concerned, it’s a mess, but you already know that. We’ve told you several times about keeping your void loop clean by using timers, but you don’t appear to want to head that advice.

Pete.

I have tried everything… Also I’m not using delay anyways so I don’t see a problem. But I’ll try debugging and will let u know