# define alarmPin 7
# define buzzerPin 6
void setup() {
Serial.begin(115200);
Serial.println("hello there!\n");
pinMode(alarmPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
check_alarm_condition(!digitalRead(alarmPin));
delay(500);
}
# define ALARM 5000
void check_alarm_condition(int alarm_condition) {
static unsigned long start_time;
static int alarm_raised;
unsigned long current_time;
if (alarm_condition) {
if (start_time == 0) {
start_time = millis(); // Initialize start_time if it has not been set yet
}
current_time = millis();
if ((current_time - start_time) > ALARM && !alarm_raised) { // 60000 milliseconds is equal to 10 minutes
printf("Alarm condition has been true for 10 minutes\n");
turnOnBuzzer(); // Turn on the buzzer
alarm_raised = 1; // Set the flag to indicate that the alarm has been raised
}
} else {
start_time = 0; // Reset start_time if the alarm condition is no longer true
alarm_raised = 0; // Reset the flag if the alarm condition is no longer true
turnOffBuzzer(); // Turn off the buzzer
}
}
// # include <stdint.h>
void checkAlarmCondition(int alarmCondition)
{
static uint32_t startTime;
static int alarmRaised;
uint32_t currentTime;
if (alarmCondition && startTime == 0)
startTime = millis(); // Initialize startTime if it has not been set yet
currentTime = millis();
if ((currentTime - startTime) > 3000) { // 60000 milliseconds is equal to 10 minutes
if (!alarmRaised) {
Serial.print("Alarm condition has been true for 10 minutes\n");
turnOnBuzzer(); // Turn on the buzzer
alarmRaised = 1; // Set the flag to indicate that the alarm has been raised
}
}
else {
startTime = 0; // Reset startTime if the alarm condition is no longer true
alarmRaised = 0; // Reset the flag if the alarm condition is no longer true
turnOffBuzzer(); // Turn off the buzzer
}
}
void turnOffBuzzer()
{
Serial.print(" and OFF\n");
digitalWrite(buzzerPin, LOW);
}
void turnOnBuzzer()
{
Serial.print("Alarm is ON.\n");
digitalWrite(buzzerPin, HIGH);
}
long in;
long out;
long myMap(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void loop0() {
in = 40000L;
Serial.print("total seconds in ");
Serial.println(in);
out = myMap( in, 0, 86400, 0, 65535);
Serial.print("after map 1 ");
Serial.println(out);
out = myMap(out, 0, 65535, 0, 86400);
Serial.print("after map 2 ");
Serial.println(out);
for (;;); // or just put the code in setup();
}