#include "thingProperties.h"
#include <Arduino.h>
#define BUZZER_PIN 12
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
pinMode(BUZZER_PIN, OUTPUT);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
static unsigned long previousMillis = 0;
static int buzzerFrequency = 1000;
unsigned long currentMillis = millis();
if (Serial2.available()) {
String receivedData = Serial2.readString();
// Serial.println("Received: " + receivedData);
noTone(BUZZER_PIN);
previousMillis = 0;
buzzerFrequency = 1000;
} else {
// Serial.println("No signal received");
tone(BUZZER_PIN, buzzerFrequency);
if (currentMillis - previousMillis >= 300)
buzzerFrequency += 100;
previousMillis = currentMillis;
}
}
}
void onBuzzerChange() {
if (buzzer == true) {
digitalWrite(BUZZER_PIN, HIGH);
status_Buzzer = "on";
} else {
digitalWrite(BUZZER_PIN, LOW);
status_Buzzer = "off";
}
ArduinoCloud.update();
}
void onStatusBuzzerChange() {
if (status_Buzzer == "on") {
buzzer = true;
} else if (status_Buzzer == "off") {
buzzer = false; // Set nilai buzzer menjadi false
}
onBuzzerChange();
}
Hello,
we'll first help you fix your post...
do yourself a favour and please read How to get the best out of this forum and post accordingly (including code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).
sorry for that and thanks . I alredy change follow the guidelines . can you help me?
You did not.. the first post still looks ugly.
Click on the pencil below the post and add code tags where necessary.
only then you are more likely to get some interested readers. (the more structured information you also provide and document your testing and findings, the more likely you'll get help)
Thx
done
so how ? did you know how to fix it ?
sorry first post still totally ugly. Leaving this thread.
i'm sorry because take time to understand that . now i try the best
it's okey now?
i really need to fix this code for my final project . i hope you can help me
the loop() should include ArduinoCloud.update();
then explain in plain English what is supposed to happen
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
https://create.arduino.cc/cloud/things/79d64371-3f34-4dfb-a191-79acf669a0e5
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool buzzer;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <Arduino.h>
#define BUZZER_PIN 12
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
pinMode(BUZZER_PIN, OUTPUT);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
static unsigned long previousMillis = 0;
static int buzzerFrequency = 1000;
unsigned long currentMillis = millis();
if (Serial2.available()) {
String receivedData = Serial2.readString();
// Serial.println("Received: " + receivedData);
noTone(BUZZER_PIN);
previousMillis = 0;
buzzerFrequency = 1000;
} else {
// Serial.println("No signal received");
tone(BUZZER_PIN, buzzerFrequency);
if (currentMillis - previousMillis >= 300)
buzzerFrequency += 100;
previousMillis = currentMillis;
}
}
}
void onBuzzerChange() {
if (buzzer == true) {
digitalWrite(BUZZER_PIN, HIGH);
status_Buzzer = "on";
} else {
digitalWrite(BUZZER_PIN, LOW);
status_Buzzer = "off";
}
ArduinoCloud.update();
}
void onStatusBuzzerChange() {
if (status_Buzzer == "on") {
buzzer = true;
} else if (status_Buzzer == "off") {
buzzer = false; // Set nilai buzzer menjadi false
}
onBuzzerChange();
}
okey this what will happen in my code 1. When the hc-05 bluetooth module receiver does not receive a signal from the hc-05 transmitter, the buzzer will sound.
2. when the buzzer has sounded, the frequency of the buzzer will increase if the receiver has not yet received a signal from the transmitter.
3. The frequency will increase every 3 seconds.
4. Then, on the dashboard display, the buzzer can be turned off and monitor the status of the buzzer.
i don't know but when i upload , that what happen
- the buzzer still sound event hc-05 have send that data ( it because hc-05s already in pairing mode )
- the dashboard cannot control the buzzer and monitor the status of buzzer
what is part of the dashboard (the variable synced with the cloud)?
buzzer ? defined as a bool ?
that's it ?
Yes boolean
#include "thingProperties.h"
#include <Arduino.h>
#define BUZZER_PIN 12 // Pin to which the buzzer is connected
void setup() {
// Initialize serial communication and wait for the port to open:
Serial.begin(9600);
Serial2.begin(9600); // Initialize serial communication with HC-05
pinMode(BUZZER_PIN, OUTPUT); // Set the buzzer pin as output
// This delay allows time to wait for the Serial Monitor without blocking if none is encountered
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to get more information
about the status of the IoT Cloud connection and network, and errors.
Higher numbers will provide more detailed information.
Default is 0 (only errors).
Maximum is 4.
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
onBuzzerChange(); // Call the onBuzzerChange() function from within the loop
}
/*
Since Buzzer is a READ_WRITE variable, onBuzzerChange() will
be executed every time a new value is received from IoT Cloud.
*/
void onBuzzerChange() {
// Add your code here to respond to Buzzer changes
if (Serial2.available()) {
// If data is available from HC-05 (receiver receives signal)
String receivedData = Serial2.readString();
// Display the received data to the Serial Monitor
// Serial.println("Received: " + receivedData);
// Turn off the buzzer sound
noTone(BUZZER_PIN);
// Reset the timer and buzzer sound frequency
previousMillis = 0;
buzzerFrequency = 1000; // reset to original frequency
} else {
// If no data received (receiver doesn't receive signal)
//Serial.println("No signal received");
// Sound the buzzer with the stored frequency
tone(BUZZER_PIN, buzzerFrequency);
// Check if it has been 300 milliseconds since the buzzer was last sounded
if (currentMillis - previousMillis >= 300) {
// Increase the buzzer sound frequency
buzzerFrequency += 100; // example increase of 100Hz
previousMillis = currentMillis; // update the last time the buzzer sounded
}
}
}
It's true?
there were multiple questions... I don't appreciate one word answers... that's your project. Unless you invest some efforts into the conversation I'll go do something else..
what about status_Buzzer?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.