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