Having trouble with the coding

I wouldn’t reccomend using millis personally, unless it’s an approach that you’re familiar with and is your regular method of doing a timeout.

Blynk timer makes most sense, as you are using it already.

Pete.

So i decided not to blink the LED and instead of using millis function or delay, i changed it into for and else. but the output in the blynk apps still nothing happens.

#define BLYNK_PRINT Serial1
#include <BlynkSimpleStream.h>
BlynkTimer timer;
char auth[] = "cabb6d95d9804956b25f9e4553fc10b8";

// Metal 
int GreenMe  =2 ;                                               
int YellowMe =3 ;                                               
int RedMe    =4;                                               
int EchoMe   =38 ;                                              
int TrigMe   =39 ;                                               
float pingUS1   ;                                              
float targetDUS1;                                                                                                            
String GarbageCaps1me="The Garbage Capacity for Metal is 0%";   
String GarbageCaps2me="The Garbage Capacity for Metal is 50%"; 
String GarbageCaps3me="The Garbage Capacity is for Metal 100%";

// Non - Metal                                                        
int GreenNo  =8;                                               
int YellowNo =9;                                                                                          
int RedNo    =10;                                               
int EchoNo   =44;                                                
int TrigNo   =45;                                                 
float pingUS2;                                       
float targetDUS2;                                                                                                           
String GarbageCaps1no="The Garbage Capacity for Non-Metal is 0%";
String GarbageCaps2no="The Garbage Capacity is Non-Metal 50%";  
String GarbageCaps3no="The Garbage Capacity is Non-Metal 100%"; 

float speedOfSound=776.5;                                       
String GarbageLCD="Garbage Capacity";      

void USMe()
{
  digitalWrite(TrigMe, LOW);                                  
  delayMicroseconds(100);                                       
  digitalWrite(TrigMe, HIGH);                               
  delayMicroseconds(20);                                       
  digitalWrite(TrigMe, LOW);                                  
  
  pingUS1 = pulseIn(EchoMe, HIGH);                          
  pingUS1 = pingUS1/1000000;                                 
  pingUS1 = pingUS1/3600;                                     
  targetDUS1 = speedOfSound*pingUS1;                    
  targetDUS1 = targetDUS1/2;                            
  targetDUS1 = targetDUS1*63360;                       
  targetDUS1 = targetDUS1*2.54;                         
  Serial.print("The distance to the trash is: ");             
  Serial.print(targetDUS1);                                
  Serial.println(" centimeters");
  Blynk.virtualWrite(V10, targetDUS1);                                           

  if (targetDUS1 < 10)                                     
  { 
    Serial.println(GarbageCaps3me);                           
    analogWrite(RedMe, 250);                                                              
    analogWrite(YellowMe, 0);
    analogWrite(GreenMe, 0); 
  }
  else
  {                                               
    analogWrite(RedMe, 0);                                  
  }
  
  if (targetDUS1 >= 11 && targetDUS1 <= 20)            
  {
    Serial.println(GarbageCaps2me);                                                             
    analogWrite(YellowMe, 250);                            
    analogWrite(RedMe, 0);   
    analogWrite(GreenMe, 0); 
  }
  else
  {                                      
    analogWrite(YellowMe, 0);                            
  }
  
  if (targetDUS1 >20)                                    
  {  
    Serial.print(GarbageCaps1me);                           
    analogWrite(GreenMe, 250);
    analogWrite(RedMe, 0); 
    analogWrite(YellowMe, 0);                           
  } 
  else 
  {                           
    analogWrite(GreenMe, 0);                                 
  }
}


void USNo()
{
  digitalWrite(TrigNo, LOW);                             
  delayMicroseconds(100);                                 
  digitalWrite(TrigNo, HIGH);                             
  delayMicroseconds(20);                                     
  digitalWrite(TrigNo, LOW);                             
  
  pingUS2 = pulseIn(EchoNo, HIGH);                           
  pingUS2 = pingUS2/1000000;                                 
  pingUS2 = pingUS2/3600;                                 
  targetDUS2 = speedOfSound*pingUS2;                   
  targetDUS2 = targetDUS2/2;                         
  targetDUS2 = targetDUS2*63360;                      
  targetDUS2 = targetDUS2*2.54;                         
  Serial.print("The distance to the trash is: ");             
  Serial.print(targetDUS2);                                
  Serial.println(" centimeters");
  Blynk.virtualWrite(V12, targetDUS2);
  
  if (targetDUS2 < 10)                              
  { 
    Serial.println(GarbageCaps3no);                  
    analogWrite(RedNo, 250);                      
    analogWrite(YellowNo, 0);
    analogWrite(GreenNo, 0);
  }
  else
  {                              
    analogWrite(RedNo, 0);                                  
  }
  
  if (targetDUS2 >= 11 && targetDUS2 <= 20)           
  {
    Serial.println(GarbageCaps2no);                                                               
    analogWrite(YellowNo, 250);  
    analogWrite(RedNo, 0);                      
    analogWrite(GreenNo, 0);                           
  }
  else 
  {                   
    analogWrite(YellowNo, 0);                                
  }
  
  if (targetDUS2 >20)                                       
  {     
    Serial.print(GarbageCaps1no);                              
    analogWrite(GreenNo, 250);
    analogWrite(YellowNo, 0);  
    analogWrite(RedNo, 0); 
  }
  else {                                                                           
    analogWrite(GreenNo, 0);                                
  } 
}

void setup() 
{
 Serial.begin(9600);
 Serial1.begin(9600);
 Blynk.begin(Serial, auth);
 pinMode(EchoMe, INPUT);
 pinMode(TrigMe, OUTPUT);
 pinMode(EchoNo, INPUT);
 pinMode(TrigNo, OUTPUT);
 timer.setInterval(1000L, USMe);
 timer.setInterval(1500L, USNo);
} 

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

I’ve never used the serial connection method, but I don’t think you can direct your Blynk debug output to the same UART that you’re using for your Blynk connection.
You also need to have the macro running on an open command prompt window on your PC.

Pete.

2 Likes

If i delete the #define BLYNK_PRINT Serial1 and the Serial in the Blynk.begin(Serial, auth); it will say (no matching function for call to ‘BlynkStream::begin(char [33])’)


Im not using any ESP8266 module or wifi, just a direct connection from USB to arduino and i follow the examples.blynk.cc as shown in the previous coding with the #define BLYNK_PRINT

#include <SoftwareSerial.h>
#include <BlynkSimpleStream.h>
BlynkTimer timer;
char auth[] = "cabb6d95d9804956b25f9e4553fc10b8";

// Metal 
int GreenMe  =5 ;                                               
int YellowMe =6 ;                                               
int RedMe    =7;                                               
int EchoMe   =38 ;                                              
int TrigMe   =39 ;                                               
float pingUS1   ;                                              
float targetDUS1;  
float targetDUS3;                                                                                                              
String GarbageCaps1me="The Garbage Capacity for Metal is 0%";   
String GarbageCaps2me="The Garbage Capacity for Metal is 50%"; 
String GarbageCaps3me="The Garbage Capacity is for Metal 100%";

// Non - Metal                                                        
int GreenNo  =8;                                               
int YellowNo =9;                                                                                          
int RedNo    =10;                                               
int EchoNo   =44;                                                
int TrigNo   =45;                                                 
float pingUS2;                                       
float targetDUS2;  
float targetDUS4;                                                                                                          
String GarbageCaps1no="The Garbage Capacity for Non-Metal is 0%";
String GarbageCaps2no="The Garbage Capacity is Non-Metal 50%";  
String GarbageCaps3no="The Garbage Capacity is Non-Metal 100%"; 

float speedOfSound=776.5;                                       
String GarbageLCD="Garbage Capacity";      

void USMe()
{
  digitalWrite(TrigMe, LOW);                                  
  delayMicroseconds(100);                                       
  digitalWrite(TrigMe, HIGH);                               
  delayMicroseconds(20);                                       
  digitalWrite(TrigMe, LOW);                                  
  
  pingUS1 = pulseIn(EchoMe, HIGH);                          
  pingUS1 = pingUS1/1000000;                                 
  pingUS1 = pingUS1/3600;                                     
  targetDUS1 = speedOfSound*pingUS1;                    
  targetDUS1 = targetDUS1/2;                            
  targetDUS1 = targetDUS1*63360;                       
  targetDUS3 = targetDUS1*2.54;                         
  Serial.print("The distance to the trash is: ");             
  Serial.print(targetDUS3);                                
  Serial.println(" centimeters");
  Blynk.virtualWrite(V10, targetDUS3);                                           

  if (targetDUS3 < 10)                                     
  { 
    Serial.println(GarbageCaps3me);                           
    analogWrite(RedMe, 250);                                                              
    analogWrite(YellowMe, 0);
    analogWrite(GreenMe, 0); 
  }
  else
  {                                               
    analogWrite(RedMe, 0);                                  
  }
  
  if (targetDUS3 >= 11 && targetDUS3 <= 20)            
  {
    Serial.println(GarbageCaps2me);                                                             
    analogWrite(YellowMe, 250);                            
    analogWrite(RedMe, 0);   
    analogWrite(GreenMe, 0); 
  }
  else
  {                                      
    analogWrite(YellowMe, 0);                            
  }
  
  if (targetDUS3 >20)                                    
  {  
    Serial.print(GarbageCaps1me);                           
    analogWrite(GreenMe, 250);
    analogWrite(RedMe, 0); 
    analogWrite(YellowMe, 0);                           
  } 
  else 
  {                           
    analogWrite(GreenMe, 0);                                 
  }
}


void USNo()
{
  digitalWrite(TrigNo, LOW);                             
  delayMicroseconds(100);                                 
  digitalWrite(TrigNo, HIGH);                             
  delayMicroseconds(20);                                     
  digitalWrite(TrigNo, LOW);                             
  
  pingUS2 = pulseIn(EchoNo, HIGH);                           
  pingUS2 = pingUS2/1000000;                                 
  pingUS2 = pingUS2/3600;                                 
  targetDUS2 = speedOfSound*pingUS2;                   
  targetDUS2 = targetDUS2/2;                         
  targetDUS2 = targetDUS2*63360;                      
  targetDUS4 = targetDUS2*2.54;                         
  Serial.print("The distance to the trash is: ");             
  Serial.print(targetDUS4);                                
  Serial.println(" centimeters");
  Blynk.virtualWrite(V12, targetDUS4);
  
  if (targetDUS4 < 10)                              
  { 
    Serial.println(GarbageCaps3no);                  
    analogWrite(RedNo, 250);                      
    analogWrite(YellowNo, 0);
    analogWrite(GreenNo, 0);
  }
  else
  {                              
    analogWrite(RedNo, 0);                                  
  }
  
  if (targetDUS4 >= 11 && targetDUS4 <= 20)           
  {
    Serial.println(GarbageCaps2no);                                                               
    analogWrite(YellowNo, 250);  
    analogWrite(RedNo, 0);                      
    analogWrite(GreenNo, 0);                           
  }
  else 
  {                   
    analogWrite(YellowNo, 0);                                
  }
  
  if (targetDUS4 >20)                                       
  {     
    Serial.print(GarbageCaps1no);                              
    analogWrite(GreenNo, 250);
    analogWrite(YellowNo, 0);  
    analogWrite(RedNo, 0); 
  }
  else {                                                                           
    analogWrite(GreenNo, 0);                                
  } 
}

void setup() 
{
 Serial.begin(9600);
 Blynk.begin(auth);
 pinMode(EchoMe, INPUT);
 pinMode(TrigMe, OUTPUT);
 pinMode(EchoNo, INPUT);
 pinMode(TrigNo, OUTPUT);
 timer.setInterval(700L, USMe);
 timer.setInterval(1000L, USNo);
} 

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

Can’t get much simpler then this Blynk Blink example…

And the documentation

http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/arduino/usb-serial

The Blynk Server connection is via the USB Link over Serial (the primary USB/programming port), whereas any (optional) desired debugging or serial prints MUST go over Serial1 using a TTL-USB adapter and another type of serial monitor/terminal program like termite. You can NOT share the Link and Debug/print serial ports.

This whole “Arduino over USB link” process has also been heavily covered throughout this forum and there is really no need for yet more individual topics about it… please search for keywords like USB, LINK, Arduino MEGA, UNO, etc

As I search/learning at the forum for the USB and Arduino Mega connection and the output still not appearing on the blynk apps.I came across a discussion about [SOLVED] Device offline (UNO using USB-link) and i follow the step.

and another one i follow the https://examples.blynk.cc/?board=Arduino%20Mega%202560&shield=Serial%20or%20USB&example=GettingStarted%2FBlynkBlink. and try and error with the codding. All is connected but only the output is not appeared

The serial is for TTL USB adapter, correct me if im wrong, that i need to comment out the Serial because of a direct connection and follow the http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/arduino/usb-serial for the bat

BlynkTimer timer;
char auth[] = "cabb6d95d9804956b25f9e4553fc10b8";

// Metal 
int GreenMe  =5 ;                                               
int YellowMe =6 ;                                               
int RedMe    =7;                                               
int EchoMe   =38 ;                                              
int TrigMe   =39 ;                                               
float pingUS1   ;                                              
float targetDUS1;  
float targetDUS3;                                                                                                              
String GarbageCaps1me="The Garbage Capacity for Metal is 0%";   
String GarbageCaps2me="The Garbage Capacity for Metal is 50%"; 
String GarbageCaps3me="The Garbage Capacity is for Metal 100%";

// Non - Metal                                                        
int GreenNo  =8;                                               
int YellowNo =9;                                                                                          
int RedNo    =10;                                               
int EchoNo   =44;                                                
int TrigNo   =45;                                                 
float pingUS2;                                       
float targetDUS2;  
float targetDUS4;                                                                                                          
String GarbageCaps1no="The Garbage Capacity for Non-Metal is 0%";
String GarbageCaps2no="The Garbage Capacity is Non-Metal 50%";  
String GarbageCaps3no="The Garbage Capacity is Non-Metal 100%"; 

float speedOfSound=776.5;                                       
String GarbageLCD="Garbage Capacity";      

void USMe()
{
  digitalWrite(TrigMe, LOW);                                  
  delayMicroseconds(100);                                       
  digitalWrite(TrigMe, HIGH);                               
  delayMicroseconds(20);                                       
  digitalWrite(TrigMe, LOW);                                  
  
  pingUS1 = pulseIn(EchoMe, HIGH);                          
  pingUS1 = pingUS1/1000000;                                 
  pingUS1 = pingUS1/3600;                                     
  targetDUS1 = speedOfSound*pingUS1;                    
  targetDUS1 = targetDUS1/2;                            
  targetDUS1 = targetDUS1*63360;                       
  targetDUS3 = targetDUS1*2.54;                         
  Serial.print("The distance to the trash is: ");             
  Serial.print(targetDUS3);                                
  Serial.println(" centimeters");
  Blynk.virtualWrite(V48, targetDUS3);                                           

  if (targetDUS3 < 10)                                     
  { 
    Serial.println(GarbageCaps3me);                           
    analogWrite(RedMe, 250);                                                              
    analogWrite(YellowMe, 0);
    analogWrite(GreenMe, 0); 
  }
  else
  {                                               
    analogWrite(RedMe, 0);                                  
  }
  
  if (targetDUS3 >= 11 && targetDUS3 <= 20)            
  {
    Serial.println(GarbageCaps2me);                                                             
    analogWrite(YellowMe, 250);                            
    analogWrite(RedMe, 0);   
    analogWrite(GreenMe, 0); 
  }
  else
  {                                      
    analogWrite(YellowMe, 0);                            
  }
  
  if (targetDUS3 >20)                                    
  {  
    Serial.print(GarbageCaps1me);                           
    analogWrite(GreenMe, 250);
    analogWrite(RedMe, 0); 
    analogWrite(YellowMe, 0);                           
  } 
  else 
  {                           
    analogWrite(GreenMe, 0);                                 
  }
}


void USNo()
{
  digitalWrite(TrigNo, LOW);                             
  delayMicroseconds(100);                                 
  digitalWrite(TrigNo, HIGH);                             
  delayMicroseconds(20);                                     
  digitalWrite(TrigNo, LOW);                             
  
  pingUS2 = pulseIn(EchoNo, HIGH);                           
  pingUS2 = pingUS2/1000000;                                 
  pingUS2 = pingUS2/3600;                                 
  targetDUS2 = speedOfSound*pingUS2;                   
  targetDUS2 = targetDUS2/2;                         
  targetDUS2 = targetDUS2*63360;                      
  targetDUS4 = targetDUS2*2.54;                         
  Serial.print("The distance to the trash is: ");             
  Serial.print(targetDUS4);                                
  Serial.println(" centimeters");
  Blynk.virtualWrite(V49, targetDUS4);
  
  if (targetDUS4 < 10)                              
  { 
    Serial.println(GarbageCaps3no);                  
    analogWrite(RedNo, 250);                      
    analogWrite(YellowNo, 0);
    analogWrite(GreenNo, 0);
  }
  else
  {                              
    analogWrite(RedNo, 0);                                  
  }
  
  if (targetDUS4 >= 11 && targetDUS4 <= 20)           
  {
    Serial.println(GarbageCaps2no);                                                               
    analogWrite(YellowNo, 250);  
    analogWrite(RedNo, 0);                      
    analogWrite(GreenNo, 0);                           
  }
  else 
  {                   
    analogWrite(YellowNo, 0);                                
  }
  
  if (targetDUS4 >20)                                       
  {     
    Serial.print(GarbageCaps1no);                              
    analogWrite(GreenNo, 250);
    analogWrite(YellowNo, 0);  
    analogWrite(RedNo, 0); 
  }
  else {                                                                           
    analogWrite(GreenNo, 0);                                
  } 
}

void setup() 
{
 Serial.begin(9600);
 Blynk.begin(Serial, auth);
 pinMode(EchoMe, INPUT);
 pinMode(TrigMe, OUTPUT);
 pinMode(EchoNo, INPUT);
 pinMode(TrigNo, OUTPUT);
 timer.setInterval(300L, USMe);
 timer.setInterval(500L, USNo);
} 

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

Basicly Serial is a one-thing-at-a-time conduit… when using the USB-link, you typically alternate the devices primary USB port (in you case COM5 in the PC and Serial in the sketch) between uploading code (USB-link script NOT running) and connecting to the server (USB-link script running).

For anything else (Debugging, serial printing, etc.), you need to use an alternate Serial port on your device (SoftSerial, or 2nd Hardware Serial if available - like Serial1, Serial2, Serial3 on MEGA), and an alternate Serial monitor type program on the PC (NOT the IDE Serial Monitor)… period… no sharing :stuck_out_tongue:

Hahahahaha thanks for the info mate and its nearly done and it suddenly appeared

but the level i set the minimum to from 30 to 0 but it appeared on the top of the level is 2.5K value, how do i change that? and it is a bit laggy or slow responsive time

I believe the MIN/MAX setting in a widget is more for visual scaling… they will not really prevent the widget from showing the actual input value. If you what to constrain your input data in the sketch to the ranges you wish to display on the widgets, use the map() command

That could simply be due to the timing between your serial BAUD and your set feed rates in your timers… trying to send too-much-to-fast may trigger a governor type limitation in the Blynk Library.

Thanks its already works perfectly, but one last question if i want to add 2 LCD with I2C, it doesnt effects the blynks right?

and thanks again to @Gunner, @PeteKnight and @Blynk_Coeur for assisting me on this project, so much appreciated

1 Like

I guess it depends on how you code it’s use… but basically, no problem.

I have used a single 20x4 i2c with one of my projects… still wishing I could have the equivalent in a widget :stuck_out_tongue_winking_eye:

1 Like

Thanks mate @Gunner for the help, much appreciated