LDR and piezo Prob

Hello guys! I need help with this prob.
When using the cmd command, the piezo will not turn off when there is no light.
I wanted the LDR to be able to turn off and on from the app as well,it works, but it causes the issue i statted above when using the cmd.
If not running the cmd,it will run normally and work
Here’s my code

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

int lol;
int analogVal;
int ldrval = A0;
int piezo = 7;

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

void setup()
{
  SwSerial.begin(9600);
  Blynk.begin(auth);
  // Default baud rate is 9600. You could specify it like this:
  //Blynk.begin(auth, 57600);
}

void loop()
{ 
  Blynk.run();
  
  analogVal = analogRead(A0);
  if(analogVal>600) tone (7,262,250); delay (250);
  
  if(analogVal<600) noTone (7);

}

BLYNK_WRITE(A0)
{
}

First off, I’d suggest taking a look at the PushData Example in the Blynk library. It shows you the use of SimpleTimer. It’s a little library to make timed functions. Using code in the loop() part when running Blynk is not recommended because it gets executed thousands of times per second. Also, using delays in conjunction with Blynk (and foremost with ESP wifi chips) is also bound to get you into trouble.

Your logic may seem ok, but you have to rewrite your code a little different. Also, why the BLYNK_WRITE(A0)? I don’t really see the need of that :slight_smile:

1 Like

Will try it right now,Thx!!

This is how to use Simple Timer i pressume? I’m still learning coding XD

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "1075eabe10f14964b774c719c2510e34";
int analogVal;
int LDR = A0;
int piezo = 7;
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  analogVal= analogRead(LDR);
 if (analogVal>700)
 {
  tone(7,262);
  delay(250);
 }
 if (analogVal<700)
 {
  noTone(7);
 }
  Blynk.virtualWrite(V5, millis() / 1000);
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);
  pinMode(LDR,INPUT);
  pinMode(piezo,OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendUptime);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

After uploading this to the board, it will not start to ring unless i turn it on in the app( A good start).
But,still facing the same issue, still wont turn off when their is no light,

Very good! That’s exactly how it should work. This way Blynk can do it’s magic and you won’t flood the microcontroller with too much sensor readings. This can cause Blynk to disconnect and all sorts of other nasty things.

MCU’s have a really limited capacity in both memory and CPU capacity so you should always try to optimize where possible and really think about what you need and want.

Wao , you reply fast XD

I’m on my breakfast with a cup of coffee, so I do some “light” coding before going out, LOL :wink:

Heheheh,
Still problem remains T.T

Try this:

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  analogVal= analogRead(LDR);
 if (analogVal>700)
 {
  tone(7,262);
  delay(250);
 }
else    // Change this here :)
 {
  noTone(7);
 }
  Blynk.virtualWrite(V5, millis() / 1000);
}

Enjoy your breakfast thou :grinning:

Still not working thou
(Sry to bother you while you’re eating breakfast XD)

You can use Serial.println(analogVal); to see in the serial monitor what is the actual value of the LDR reading. It may give some insight as to what goes wrong. I use Serial.print (or Serial.println) a lot in the proces of coding and debugging.

Based on the Serial Monitor,it works.
It gives 700+ when there is light.
Below 700 when there is no light.

Well, at least that is ok, so we know that is not the problem. Does the code work without Blynk? That’s maybe worth giving a try.

Yeah,it actually does

Still, I want it to have the ability to be turned off and on without probs

@Nathaniel_Chai the other use of SimpleTimer with tone is to remove the 250ms delay.

SimpleTimer starts playing a tone.
SimpleTimer stops playing a tone 250ms later etc.

Which Arduino board are you using?

Uno

Actually, I have solved a lot of the issues my self.
I have changed my idea a bit, instead of turning off the LDR,I’ve set the code up to turn off the piezo instead.
Now, the problem is when turning on the speakers,it will start to ring when the lights of the room are off , but when turning on the lights again,the alarm wont ring again.
It only rings once and that’s it lul.I need it to run forever . Do you guys have any clue on how to solve it?
Here’s the code:

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "1075eabe10f14964b774c719c2510e34";
int LDR = A4;
int piezo = 2;
int analogVal;
int analogVal2;
int lol;
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void readSensors()
{
  analogVal = analogRead(A4);
  Blynk.virtualWrite(V5, analogVal);
  lol = digitalRead(2);
  if (lol == HIGH && analogVal>600) {tone(2,262);}
  if (lol == LOW && analogVal>600) {noTone(2));}
  if (lol == HIGH && analogVal<600) {noTone(2);}
  if (lol == LOW && analogVal<600) {noTone(2);}
}
void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   Blynk.virtualWrite(V0, millis()/ 1000);
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);
  pinMode(piezo,OUTPUT);
  // Setup a function to be called every second
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(1000L, readSensors);
}

void loop()
{
   Blynk.run();
  timer.run(); // Initiates SimpleTimer
  
  analogVal = analogRead(A4);
  lol = digitalRead(2);
  
}