Hi
I have created an alarm project but cant get ultrasonic sensor SR04 to read measurments.
If I put it without Blynk code, the sensors reads values correclty but when I put Blynk.begin(auth); into the setup the measurments show 0.
I searched the formus but couldn find any similar problems. I belive the problem is with the delay of the Echo HIGH.
Could somebody please help with checking the code?
Thanks a lot!!
#define BLYNK_PRINT Serial // Defines the object that is used for printing
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <WidgetRTC.h>
// You should get Auth Token in the Blynk App.
char auth[] = "";
#define W5100_CS 10
#define SDCARD_CS 4
// Set your LED and physical button pins here
const int relayPin = 8;
//Support for Blynk timer
BlynkTimer timer;
//Support for Blynk real time clock
WidgetRTC rtc;
// Attach virtual serial terminal to Virtual Pin V1
WidgetTerminal terminal(V0);
int trigPin = 9; // Trigger
int echoPin = 10; // Echo
int messagesent= 0;
int relayState = 1;
int val;
long duration, distance;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V1);
// Begin synchronizing time
rtc.begin();
}
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(year()) + "/" + month() + "/" + day();
terminal.print(currentDate);
terminal.print(" ");
terminal.print(currentTime);
terminal.print(" ");
}
void ultra()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
distance= duration*0.034/2;// Divide by 29.1 or multiply by 0.0343
Serial.println(duration);
Serial.println(distance);
}
void AlarmOFF()
{
digitalWrite(relayPin, HIGH);
Blynk.virtualWrite(V1, 1);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
relayState = param.asInt();
digitalWrite(relayPin, relayState);
if (relayState==0) {
timer.setTimeout(60000, AlarmOFF);
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
//set ULTRASONIC
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//set relay
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, relayState);
Blynk.begin(auth);
delay(5);
// Setup a function to be called every 100 ms
timer.setInterval (5000, ultra);
}
void loop()
{
Blynk.run();
timer.run();
}