Control servo motor with specific time for fish feeder without using delay

halooo i need to solve a program using servo motor for fish feeder … it will turn 180 degree at specific time for fish feeder. as i know coding cant using delay because its will delay other program (in my case i have other sensor that reading and display for blynk… each sensor will send the data every 1s) if i put delay it will delay my other sensor to send data. pls pls pls help me to solved it

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>

Servo myservo;
unsigned long time;
int button;
int pos = 0;



char auth[] = "PeSX2GXaCKWn8T-o7moehrswfYN_DmdD"; //Auth Token in the Blynk App.
char ssid[] = "iPhone";// Your WiFi credentials
char pass[] = "unauni9495";// Set password to "" for open networks.

#include <SoftwareSerial.h>//Software Serial on Uno
SoftwareSerial EspSerial(2, 3); // RX, TX 2,3

#define ESP8266_BAUD 9600 // Your ESP8266 baud rate:

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

BLYNK_WRITE(V0)
{
  Blynk.run();
  button = param.asInt();
  if(button == 1)
  for(pos = 0; pos <= 180; pos +=1)
  time = millis();
}

void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  
  myservo.attach(7);
}

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

this is my coding and it cant run smoothly.

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h> 

char auth[] = "PeSX2GXaCKWn8T-o7moehrswfYN_DmdD"; //Auth Token in the Blynk App.
char ssid[] = "iPhone";// Your WiFi credentials
char pass[] = "unauni9495";// Set password to "" for open networks.

#include <SoftwareSerial.h>//Software Serial on Uno
SoftwareSerial EspSerial(2, 3); // RX, TX 2,3

#define ESP8266_BAUD 9600 // Your ESP8266 baud rate:

ESP8266 wifi(&EspSerial);

//--------------------------   pH sENSOR  ------------------------------//

#define SensorPin A0// The pH Sensor Analog A2 pin we're connected
unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;

int dosingPumpAc = 8;       // select the pin for the dosing pump high
int dosingPumpAl = 9;       // select the pin for the dosing pump low

//--------------------------   Water Level  ------------------------------//

int sensorPin = A3;      // input pin A1 for the water level sensor module
int sensorValue = 0;
int solenoidinPin = 4;   // select the pin for the solenoid valve in
int motorOut = 10;       // select the pin for the Motor valve out

//--------------------------   Servo Motor  ------------------------------//

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

long FISHFEEDER = 2000; // 12 hours between feeding
long endtime; 
long now;

BlynkTimer timer;

void sendSensor()
{
  


  //--------------------------   Water Level  ------------------------------//

  sensorValue = analogRead(sensorPin); // read the value from the sensor
  // send the message about water level to serial monitor
  
   Blynk.run();
    
    Blynk.virtualWrite(V10, sensorValue);
    
  if (sensorValue <= 0) {
    
     digitalWrite(solenoidinPin, HIGH); // turn the solenoid valve inlet on
     digitalWrite(motorOut,LOW);
  }

  else if (sensorValue > 0 && sensorValue <= 223) { 
     digitalWrite(solenoidinPin, LOW); // turn the solenoid valve inlet on
     digitalWrite(motorOut,LOW);
  }
  else if (sensorValue > 323) {
    //Blynk.notify("Warning water overflow");
    digitalWrite(solenoidinPin, LOW);   // turn the solenoid valve inlet off
    digitalWrite(motorOut,HIGH);
  }
  
  
  //--------------------------   pH sENSOR  ------------------------------//

  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;  //convert the millivolt into pH value
  Blynk.run();
  
  Serial.print("pH : ");
  Serial.println(phValue,2);
  
  Blynk.virtualWrite(V7, phValue);

  if(phValue<7)                             //Ph value below 5                       
  {
     digitalWrite(dosingPumpAc,LOW);        // turn on dosing pump for Alcali
     digitalWrite(dosingPumpAl,HIGH);       // turn on dosing pump for Acid
  }
  else if(phValue>7 && phValue <8)          //Ph value between 6 and 7
  {
     digitalWrite(dosingPumpAc,LOW);        // turn off dosing pump for Alcali
     digitalWrite(dosingPumpAl,LOW);        // turn on dosing pump for Acid
  }
  else if(phValue>8)                        //Ph value above 7  
  {
     digitalWrite(dosingPumpAc,HIGH);       // turn on dosing pump for Alcali
     digitalWrite(dosingPumpAl,LOW);        // turn off dosing pump for Acid
   
  }

   //--------------------------   Servo Motor  ------------------------------//

  now = millis();
  endtime = now + FISHFEEDER;
  
  while(now < endtime) {
   myservo.write(0);
   now = millis();   
  }
  

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






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

  EspSerial.begin(ESP8266_BAUD);// Set ESP8266 baud rate
  //delay(10);

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

  pinMode(sensorPin, INPUT);       // declare the sensorPin as an INPUT (WATER LEVEL )
  pinMode(solenoidinPin,OUTPUT);   // declare the solenoiindPin as an OUTPUT (WATER LEVEL )
  pinMode(motorOut,OUTPUT);        // declare the motor pump as an OUTPUT (WATER LEVEL )

  pinMode(SensorPin, INPUT);
  pinMode(dosingPumpAc, OUTPUT);   // declare the dosing pump high as an OUTPUT (Ph)
  pinMode(dosingPumpAl, OUTPUT);   // declare the dosing pump low as an OUTPUT (Ph)

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  
  myservo.write(0);
  
  timer.setInterval(1000L, sendSensor);
}

void loop()
{

    Blynk.run();
   
    timer.run();
}

pls help me i just got 1 week to present this project :frowning:

without diving too much into your code. You probably want to use the set.timeout command.

When the appropriate time comes, that is

you would write the servo to the 180 position, then say 5 seconds later just write it back to the 0 position.

For example:


if (timeToFeed == true)
{
servo.write(180);
timeToFeed = false;

timer.setTimeout(5000L, []() {  // Timed Lambda Function - run after 5 seconds
      servo.write(0);
   });
}
2 Likes

thank you for answering my question its really2 help but…

what if i want this servo to run after 12 hours for 3 seconds and repeat again for 12 hours later for 3 seconds ? basically i want to feed my fish for every 12 hours but the delay have blocked the other sensor reading.

I would say to use the RTC, and check the time. When the time of the day is reached, feed the fish.

something like (untested, just an example)…

#include <TimeLib.h>
#include <WidgetRTC.h>


int currentHour;
int currentMinute;
int feedHour1 = 10; //10AM
int feedHour2 = 22; //10PM

WidgetRTC rtc;

void feedFish()
{
  currentHour = hour();
  currentMinute = minute();

  if ((currentHour == feedHour1) || (currentHour == feedHour2))
   {
      if(currentMinute == 0)
      {
       servo.write(180);
       timer.setTimeout(3000L, []() {  // Timed Lambda Function - run after 3 seconds
       servo.write(0);
          });
      }
   }
}


void setup()
{
   
   //all your other stuff

  // Begin synchronizing time
  rtc.begin();

    setSyncInterval(300);

  timer.setInterval(60000L, feedFish);//check if time to feed every minute


}
3 Likes

halo why i try this coding its say have error at

35

Please post the full code, and a copy of the error.

1 Like

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Servo.h> 


char auth[] = "PeSX2GXaCKWn8T-o7moehrswfYN_DmdD"; //Auth Token in the Blynk App.
char ssid[] = "iPhone";// Your WiFi credentials
char pass[] = "unauni9495";// Set password to "" for open networks.

#include <SoftwareSerial.h>//Software Serial on Uno
SoftwareSerial EspSerial(2, 3); // RX, TX 2,3

#define ESP8266_BAUD 9600 // Your ESP8266 baud rate:

ESP8266 wifi(&EspSerial);
BlynkTimer timer;
Servo servo;
int currentHour;
int currentMinute;
int feedHour1 = 10; //10AM
int feedHour2 = 22; //10PM

WidgetRTC rtc;

void feedFish()
{
  currentHour = hour();
  currentMinute = minute();

  if(currentHour == feedHour1) || (currentHour == feedHour2)
   {
      if(currentMinute == 0)
      {
       servo.write(180);
       timer.setTimeout(3000L, []() {  // Timed Lambda Function - run after 3 seconds
       servo.write(0);
          });
      }
   }
}
void setup() {
  
  Serial.begin(9600);

  EspSerial.begin(ESP8266_BAUD);

  Blynk.begin(auth, wifi, ssid, pass);
  rtc.begin();

  setSyncInterval(300);

  timer.setInterval(60000L, feedFish);
  servo.attach(9);  // attaches the servo on pin 9 to the servo object 
  
 servo.write(0);
  
}

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

try wrapping the function in parentheses.

if ((currentHour == feedHour1) || (currentHour == feedHour2))
1 Like

thanks for helping and what if i want to test this coding ? for example i want to test for current time to make sure its function when time come

just change the variables. Re-load, and wait.

int feedHour1 = 10; //10AM; Time in 24 Hour Format
int feedHour2 = 22; //10PM; Time in 24 Hour Format

A slicker version would be to allow the user to select the hour, using a slider widget (easier), or if you want to get real fancy, using the time input widget (more advanced).

1 Like

hello, what if I want to move my servo every 40 seconds in 2 minutes?

hello, can i get the schematic circuit of the fish feeder ?? urgent please help 🥲

hello, can i get the schematic circuit of the fish feeder project ? Can you share it please

hi good day! can you help me code my project? i want to develop a automated fish feeder system. and is using a servomotor to dispense fish food, an ultrasonic sensor to detect if the container is about to empty and a nodemcu esp2866 as my microcontroller. i m also developing an application in android studio were the data’s from the devices can be viewed. hope you can help me . please DM me

hello! your code helps me alot. now what if, i want to edit the feedhour 1 and 2 via blynk? like i dont need to upload reupload the coding again?

//slider on pin V2
BLYNK_WRITE(V2) {
  feedhour1= param.asInt();
}