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

How to modify regular code to blynk..this is mycode

#define buz 9
#define pulsePin A4
#define led 10
long sumExpect=0; //running sum of 64 sums 
long ignor=0;   //number of ignored sums
long diff=0;        //difference between sum and avgsum
long pTime=0;
long buzPeriod=0; 
void setup() 
{
  Serial.begin(9600);
  pinMode(pulsePin, OUTPUT); 
  digitalWrite(pulsePin, LOW);
  pinMode(capPin, INPUT);  
  pinMode(buz, OUTPUT);
  digitalWrite(buz, LOW);
  pinMode(led, OUTPUT);
}
void loop() 
{
  int minval=1023;
  int maxval=0;
  long unsigned int sum=0;
  for (int i=0; i<256; i++)
  {
    //reset the capacitor
    pinMode(capPin,OUTPUT);
    digitalWrite(capPin,LOW);
    delayMicroseconds(20);
    pinMode(capPin,INPUT);
    applyPulses();
    int val = analogRead(capPin); //takes 13x8=104 microseconds
    minval = min(val,minval);
    maxval = max(val,maxval);
    sum+=val;
long unsigned int cTime=millis();
    char buzState=0;
    if (cTime<pTime+10)
    {
      if (diff>0)
        buzState=1;
      else if(diff<0)
        buzState=2;
    }
    if (cTime>pTime+buzPeriod)
    {
      if (diff>0)
      buzState=1;
      else if (diff<0)
      buzState=2;
      pTime=cTime;   
    }
    if (buzPeriod>300)
    buzState=0;

    if (buzState==0)
    {
      digitalWrite(led, LOW);
      noTone(buz);
    }  
    else if (buzState==1)
    {
      tone(buz,2000);
      digitalWrite(led, HIGH);
    }
    
    else if (buzState==2)
    {
      tone(buz,500);
      digitalWrite(led, HIGH);
     Serial.println("Alert Metal Detected");
    }
  }
subtract minimum and maximum value to remove spikes
  sum-=minval; 
  sum-=maxval;
   if (sumExpect==0) 
  sumExpect=sum<<6; //set sumExpect to expected value
  long int avgsum=(sumExpect+32)>>6; 
  diff=sum-avgsum;
  if (abs(diff)<avgsum>>10)
  {
    sumExpect=sumExpect+sum-avgsum;
    ignor=0;
  } 
  else 
    ignor++;
  if (ignor>64)
  { 
    sumExpect=sum<<6;
    ignor=0;
  }
  if (diff==0) 
    buzPeriod=1000000;
  else 
  buzPeriod=avgsum/(2*abs(diff));    
}
void applyPulses()
{
    for (int i=0;i<3;i++) 
    {
      digitalWrite(pulsePin,HIGH); //take 3.5 uS
      delayMicroseconds(3);
      digitalWrite(pulsePin,LOW);  //take 3.5 uS
      delayMicroseconds(3);
    }
}

@Kiran_Malganvi please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

please help me …i have to submit the proj in school

Start by reading this:

Then at the Sketch Builder examples here:
https://examples.blynk.cc/
for whatever type of board and connection method you are planning g to use.

Pete.

then where to add loop thing… sir

Maybe you should have paid more attention in school!

Pete.

actually im doing it on my own we have not learnt about this…its unknow for us…thought to learn something different so i tried to do this

That’s fair enough. Is the code that you posted written by you, or have you acquired it from somewhere else?

Pete.

i got it in github…im came to know little bit …but im getting confused with blynk thats it

Your question about:

tells me that either you didn’t read the “Keep your void loop() clean” document properly, or you don’t understand the basic structure of a C++ sketch and the role of the void setup() and void loop() functions and that reading the “Keep your void loop() clean” document therefore didn’t make any sense.

I’d suggest that either you re-read the “Keep your void loop() clean” document and ask specific questions that are relevant to what it says, or you learn how a C++ sketch is structured.

What hardware are you planning to run this on, and what connection method are you planning to use?

Pete.

oh yes i just did not go through the void loop i got it…but not good in prog i can do it…thank you