Control servoMG996 problem

I dont know why my device (servo, buzzer) do it twice althought i only press button in blynk app once . But only get 1 notification , 1 email.

This is my code :

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#include "music.h"

Servo myservo;

int speakerPin = 15;//Chân được nối với loa hoặc buzzer
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4 
};

int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 
};


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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "carrotne";
char pass[] = "12341234";

void setup()
{
   pinMode(15, OUTPUT);//buzzer
   for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);  
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);      
    noTone(speakerPin);
   }
  // Debug console
 
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

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


BLYNK_WRITE( V1 )
{   for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);  
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);      
    noTone(speakerPin);
   }   
 
    myservo.attach(13);
    myservo.write(0);
    delay(1000);
    myservo.write(180);
    delay(1000);
    myservo.detach();
    Blynk.notify("Your pet has been fed");
    Blynk.email("zzz@gmail.com","Done","Success")
}

Please tell me how to repair it. Sorry bad english !

I’ve re-formatted your code, by adding three backticks at the top and bottom, like this:
```

Please do this yourself in future, otherwise your unformatted code will be deleted.
When you press a button widget it sends two values. By default these at a 1 when the button is pressed and a 0 when it is released.

If you only want the code to run once, say ion the button press, then you need to test what value has been sent from the button.

Pete.

Hello Pete,
I only set output = V1 in my Blynk app. Value default is 0 ->1.
So what should i do to know what values has been sent?
Thanks you so much!

Carrot

BLYNK_WRITE(V1)
As I said before, you nee to test to see what the value is that’s com entombed the button widget. Here’s the simplest way:

{
 if (param.asInt());
  {
    //your code to run when the value from the widget ==1
  }
}

The line that says if (param.asInt()); is getting the value from the switch widget as an integer and testing to see if it’s true (1). This could be written this way as well:

  int pinvalue = param.asInt();
  if(pinvalue==1)
  {

  }

By the way, those delays are bad programming practice when using Blynk and will probably lead to disconnections. You should use timeout timers instead. Search the forum for info on how to do that.

Pete.

Now i can fix it . Thanks for your helping Pete!
Carrot

1 Like

I use this code to make new button V0. To run as many times as i like

BLYNK_WRITE( V0 )    {  
int pinvalue = param.asInt();
  for(int i=0;i<pinvalue;i++){

and use Timer button V0 , 0 - 2.
When i test with button , servo run 2nd time.
When i use timer button, servo run only once time.

If you want to control the number of times it runs then use something like a slider or step widget to set the number of times. Assign the incoming value to a GLOBAL variable and use that in your for loop.

Pete.