hello,
i made my project on arduino mega 2560 and connect to blink server through Ethernet Shield.
when i am trying to run my project then project works properly but giving problem in digital pins i don’t understand why this happening. But when i run individual code like only for push button switches then it works ok but not working when i run the my main sketches and it happening only with buttons. can any one tell me how to over come this problem.
please reply…
lets see what i done in my project i used some sensor such as
1.dht11 - for temp and humidity measurement
2.PIR - for security purpose
3. Ultrasonic HC-SR04- For well water level detection and calculating how much water present in well in litters and percentage as well.
4. soil moisture sensor- for measurement of soil moisture in farm
5. 4 channel relay driver- out of this 2 used for driving ac motor and 2 for switching lights
my code you can see here…
#define BLYNK_DEBUG // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
SimpleTimer timer;
char auth[] = "8b91df7e49e041398125c7bb34ee4236"; //project tocken
WidgetLED low_lev(0),med_lev(1),high_lev(2),no_lev(4),auto_led(7),man_led(8),sec_led(9),err_led(11);
WidgetLCD lcd(4);
const int pingPin = 7;
const int inPin = 6;
const int security_pin = 9;
const int buzzer_pin = 3;
const int motor1_pin = 22;
const int motor2_pin = 24;
const int light_pin = 26;
const int mode = 52;
const int inchconv = 147, cmconv = 59, fixed = 120, radius = 33;
int indec, cmdec,actual,actual_ft,limit,water_litter,val,val1,val2;
int sec_state = 0, mode_state=0;
float ltr1,water_per;
long duration, inches, cm, volume;
String hight,hight_A,hight_W,water_L,litter_val;
#define DHTPIN 8 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// a function to be executed periodically
void repeatMe() {
Serial.print("Uptime (s): ");
Serial.println(millis() / 1000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / inchconv;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / cmconv;
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
dht.begin();
pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT);
pinMode(motor1_pin, OUTPUT);
pinMode(motor2_pin, OUTPUT);
pinMode(security_pin, INPUT);
pinMode(mode, INPUT);
while (Blynk.connect() == false)
{
// Wait until connected
}
low_lev.off();
med_lev.off();
high_lev.off();
no_lev.off();
sec_led.off();
auto_led.off();
man_led.off();
err_led.off();
digitalWrite(buzzer_pin, LOW);
Blynk.notify("CONNECTED TO AGRICULTURE AUTOMATION SYSTEM");
delay(5000);
lcd.clear();
lcd.print(2, 0, "WEL-COME TO");
lcd.print(0, 1, "AGRICULTUR AUTO SYS.");
delay(5000);
lcd.clear();
lcd.print(3, 0, "SYSTEM IS");
lcd.print(2, 1, "INITIAZING..");
delay(3000);
lcd.clear();
lcd.print(3, 0, "SYSTEM IS");
lcd.print(5, 1, "ONLINE");
delay(2500);
lcd.clear();
lcd.print(2, 0, "SELECT MODE");
lcd.print(0, 1, "1.AUTO");
lcd.print(11, 1, "2.MAN");
delay(2500);
lcd.clear();
lcd.print(0, 0, "SELECTED MODE");
lcd.print(0, 1, "1.AUTOMATION");
delay(2500);
lcd.clear();
}
void cal_water()
{
// Send a short LOW followed by HIGH pulse to Trigger input:0
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH);
inches = microsecondsToInches(duration);
indec = ((duration - inches * inchconv) * 10 / inchconv);
cm = microsecondsToCentimeters(duration);
cmdec = (duration - cm * cmconv) * 10 / cmconv;
actual= fixed - inches;
actual_ft=actual/12;
volume=3.14*radius*radius*actual;
ltr1=volume/61.0237;//in foot
hight = String(inches) + "." + String(indec);
hight_A = String(volume) + "." + String(indec);
hight_W = String(actual_ft) + "." + String(indec);
water_L = String(ltr1) + "." + String(indec);
water_per=(ltr1/6724)*100;
lcd.print(0,0,"WATER LTR:");
lcd.print(10,0, ltr1);
lcd.print(14,0, " L");
lcd.print(0,1,"WATER LEV:");
lcd.print(10,1, water_per);
lcd.print(15,1, "%");
//Automation is on
auto_led.on();
if(water_per < 15)
{
Blynk.notify("WATER LEVEL IS LOW");
timer.setInterval(1000, repeatMe);
}
if(water_per > 15 && water_per < 25)
{
no_lev.on();
}
if (water_per > 15 && water_per <33.33 )
{
low_lev.off();
med_lev.off();
high_lev.off();
no_lev.off();
digitalWrite(motor1_pin, LOW);
digitalWrite(motor2_pin, LOW);
}
if (water_per > 33.33 && water_per < 66.66 )
{
low_lev.on();
med_lev.on();
high_lev.off();
no_lev.off();
if(val<650)
{
digitalWrite(motor1_pin, HIGH);
digitalWrite(motor2_pin, HIGH);
timer.setInterval(1000, repeatMe);;
}
if(val>649)
{
digitalWrite(motor1_pin, LOW);
digitalWrite(motor2_pin, LOW);
timer.setInterval(1000, repeatMe);
}
}
if (water_per > 66.66 )
{
low_lev.on();
med_lev.on();
high_lev.on();
no_lev.off();
if(val<650)
{
digitalWrite(motor1_pin, HIGH);
digitalWrite(motor2_pin, HIGH);
timer.setInterval(1000, repeatMe);
}
if(val>649)
{
digitalWrite(motor1_pin, LOW);
digitalWrite(motor2_pin, LOW);
timer.setInterval(1000, repeatMe);
}
}
}
void cal_temp()
{
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println("Failed to read from DHT sensor!");
err_led.on();
return;
}
Blynk.virtualWrite(V5, t);
Blynk.virtualWrite(V6, h);
timer.setInterval(1000, repeatMe);
}
void security()
{
sec_state = digitalRead(security_pin);
if (sec_state == HIGH)
{
sec_led.on();
digitalWrite(buzzer_pin, HIGH);
Blynk.notify("security alart....!");
}
else
{
sec_led.off();
digitalWrite(buzzer_pin, LOW);
}
}
void moisture_cal()
{
val1=analogRead(A0);
val2=analogRead(A1);
val=(val1+val2)/2;
timer.setInterval(1000, repeatMe);
Serial.print("Moisture Sensor1 Value:");
Serial.println(val1);
Serial.print("Moisture Sensor2 Value:");
Serial.println(val2);
Serial.print("Moisture average Value:");
Serial.println(val);
Blynk.virtualWrite(V10, val);
}
void check_mode()
{
if(mode_state == HIGH)
{
lcd.clear();
lcd.print(0, 0, "SELECTED MODE");
lcd.print(0, 1, "2.MANUAL");
delay(2500);
while(1)
{
Blynk.run();
timer.run();
security();
}
}
else
{
//
}
}
void loop()
{
Blynk.run();
timer.run();
moisture_cal();
cal_water();
cal_temp();
security();
timer.setInterval(1000, repeatMe);
}