BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Having trouble with the coding

#9
0 Likes

#10

@Gunner
you beat me :smile:

2 Likes

#11

Even the timer has been inserted but still has no output on the blnyk apps

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


int GreenMe  =4 ;                                               
int YellowMe =3 ;                                               
int RedMe    =2 ;                                               

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%";

                                                                 
int GreenNo  =10;                                               
int YellowNo =9;                                                                                          
int RedNo    =8;                                               

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 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(1200L, USMe);
 timer.setInterval(1200L, USNo);
 
}

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");
  Serial.println(" ");                              
  delay(1000);
                                                 

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


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");
  Serial.println(" ");                              
  delay(1000);
  

  
  if (targetDUS2 < 10)                              
  { 
    Serial.println(GarbageCaps3no);                  
    analogWrite(RedNo, 250);                      
    delay(1000);                                     
    analogWrite(RedNo, 0);                                  
    delay(100);
  }
  
  if (targetDUS2 >= 11 && targetDUS2 <= 20)           
  {
    Serial.println(GarbageCaps2no);                                                               
    analogWrite(YellowNo, 250);                             
    delay(1000);                                                
    analogWrite(YellowNo, 0);                               
    delay(100); 
  }
  
  if (targetDUS2 >20)                                       
  {     
    Serial.print(GarbageCaps1no);                              
    analogWrite(GreenNo, 250);                              
    delay(1000);                                                
    analogWrite(GreenNo, 0);                                
    delay(100);
  } 
}

void loop() 
{
  Blynk.run();
  timer.run();
  Blynk.virtualWrite(V10, targetDUS1);
  Blynk.virtualWrite(V12, targetDUS2);
}
0 Likes

#12

You have delays everywhere and Blynk virtual writes in your void loop!!

The “keep your void loop clean“ article has been linked twice in this thread, but you don’t seem to have understood any of it.

Pete.

0 Likes

#15

even with all the delay is erased and the Blynk Virtual write is placed under the void of USMe and USNo but still, no output on the blynk apps and my LED is not working/blinking without the delay


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


int GreenMe  =4 ;                                               
int YellowMe =3 ;                                               
int RedMe    =2 ;                                               

int EchoMe   =22 ;                                              
int TrigMe   =23 ;                                               
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%";

                                                                 
int GreenNo  =10;                                               
int YellowNo =9;                                                                                          
int RedNo    =8;                                               

int EchoNo   =26;                                                
int TrigNo   =27;                                                 
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 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(1000L, USNo);
 
 
} 

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(RedMe, 0);                                
   
  }
  
  if (targetDUS1 >= 11 && targetDUS1 <= 20)            
  {
    Serial.println(GarbageCaps2me);                                                             
    analogWrite(YellowMe, 250);                            
                                                
    analogWrite(YellowMe, 0);                            

  }
  
  if (targetDUS1 >20)                                    
  {  
    Serial.print(GarbageCaps1me);                           
    analogWrite(GreenMe, 250);                            
                                                 
    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(RedNo, 0);                                  

  }
  
  if (targetDUS2 >= 11 && targetDUS2 <= 20)           
  {
    Serial.println(GarbageCaps2no);                                                               
    analogWrite(YellowNo, 250);                             
                                  
    analogWrite(YellowNo, 0);                               
     
  }
  
  if (targetDUS2 >20)                                       
  {     
    Serial.print(GarbageCaps1no);                              
    analogWrite(GreenNo, 250);                              
                                                   
    analogWrite(GreenNo, 0);                                
 
  } 
}

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

#16

Research a lots of coding and the way it works on the community such as




but still got the same results with no output on the blynk. if i erase all the delay for the LED, then how should i make the LED blinking. and I use 2 Gauge on the Blnyk Apps for Metal (V10) and Non Metal (V12) both is PUSH. My connection is USB type

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


int GreenMe  =4 ;                                               
int YellowMe =3 ;                                               
int RedMe    =2 ;                                               

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%";

                                                                 
int GreenNo  =10;                                               
int YellowNo =9;                                                                                          
int RedNo    =8;                                               

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 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(1000L, USNo);
 
} 

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(RedMe, 0);                                
   
  }
  
  if (targetDUS1 >= 11 && targetDUS1 <= 20)            
  {
    Serial.println(GarbageCaps2me);                                                             
    analogWrite(YellowMe, 250);                            
                                                
    analogWrite(YellowMe, 0);                            

  }
  
  if (targetDUS1 >20)                                    
  {  
    Serial.print(GarbageCaps1me);                           
    analogWrite(GreenMe, 250);                            
                                                 
    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(RedNo, 0);                                  

  }
  
  if (targetDUS2 >= 11 && targetDUS2 <= 20)           
  {
    Serial.println(GarbageCaps2no);                                                               
    analogWrite(YellowNo, 250);                             
                                  
    analogWrite(YellowNo, 0);                               
     
  }
  
  if (targetDUS2 >20)                                       
  {     
    Serial.print(GarbageCaps1no);                              
    analogWrite(GreenNo, 250);                              
                                                   
    analogWrite(GreenNo, 0);                                
 
  } 
}

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

#17

same timer, never works :stuck_out_tongue:

0 Likes

#18

You use a timer, or a non-blocking utility like ticker, or you use a millis function and check how much time in millis has passed.

Pete.

0 Likes

#19

meaning?

0 Likes

#20

meaning that you can’t do two calls at the same time
try that

timer.setInterval(1000L, USMe);
timer.setInterval(1500L, USNo);
0 Likes

#21

the millis function seems complicated to understand and to apply on my coding based on youtube on how to do the coding and is it the same as delayMicroseconds(x)?

0 Likes

#22

as @Gunner always says :stuck_out_tongue: ,
you have to learn blynk before coding anything.
try basic blynk sketches to understand blynk timers,
there is a lot of examples on this community.

0 Likes

#23

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.

0 Likes

#24

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();
}
0 Likes

#25

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

#26

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();
}
0 Likes

#27

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

https://examples.blynk.cc/?board=Arduino%20Mega%202560&shield=Serial%20or%20USB&example=GettingStarted%2FBlynkBlink

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

0 Likes

#28

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();
}
0 Likes

#29

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:

0 Likes

#30

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

0 Likes