Automatic fish feeder fails to work when rebooting

hi iam janardhan ,iam trying to do an automatic fish feeder using nodemcu and a servo with the help of blynk application.Everything is fine until there is a constant powersupply for the circuit,but when there is a powercut and when the board regain power ,the servo is reaching to 0 degrees at start which is actually resulting in flow of all the fish food.
the below is the code iam using for my project .Hope you suggest me some changes and solve this issue.



#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "GPONWIFI_7010";
char pass[] = "00000007B5";

Servo servo;

void cradle() {
//you begin your own personal code for servo here
  int pos;

  for (pos = 0; pos <= 40; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 40; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
//your personal code for servo should end here
}


BLYNK_WRITE(V3) 
{
  int pinValue = param.asInt();
  if (pinValue == 1) {    // if Button sends 1
    cradle();             // start the function cradle
    Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
    int pinValue = 0;  // Set V3 status to 0 to quit, unless button is still pushed (as per below)
    Blynk.syncVirtual(V3); // ...Then force BLYNK_WRITE(V3) function check of button status to determine if repeating or done.
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  servo.attach(4); //attaches servo to pin 4 (D2 on node mcu)
}

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

You obviously don’t understand how the BLYNK_WRITE(vPin) callback function works. It isn’t a looping function, it gets called once when the value of the widget attached to V3 changes, or when a Blynk.syncVirtual(vPin) command is executed.
Trying to force the code to loop by putting a Blynk.syncVirtual(vPin) command in your BLYNK_WRITE(vPin) callback isn’t the correct approach.
Also, this makes no sense:

This command does not change the status of the widget attached to V3, you would need to use a Blynk.virtualWrite(vPin, value) command to do that.

I suspect that the solution to your power interruption problem is to add a BLYNK_CONNECTED callback that contains code which will set your servo to the closed position.

Pete.

sorry i could’nt understand ,can you please elaborate more about the changes .

There is documentation and help topics to assist you in learning about Blynk code… and starting with a simpler project to start might help.

PS, please do not duplicate your issue into other’s topics, I have removed the duplicate.

iam sending a video so that you can have a look

Watching a video (which by the way isn’t accessible) won’t tell me anything I don’t already know - you’re using badly written code has no BLYNK_CONNECTED callback to handle resetting your servo when the device connects or reconnects to the Blynk server.

I’m assuming that you didn’t write this code yourself, otherwise you would have understood at least some of my comments. Is this a piece of code that you’ve picked-up from somewhere else?

Pete.

Pete.

Hi there jeevan,
Can you please answer some of my queries about your code so maybe I can be of some help:
1)Are you trying to activate your fish food cradle through Blynk app? (By pressing some button)
2)What is your fish food lock position (degree) of servo? (Degree of servo that food doesn’t drop)
3)What is your fish food drop position (degree) of servo?( Degree of servo that food will drop)