Hello,
I'm currently doing a project with a sensor. I want it so that it does if a certain amount of time pass by and the amount is still the same, the a buzzer would go off to indicate that the amount is still the same. Please help.
Hello,
I'm currently doing a project with a sensor. I want it so that it does if a certain amount of time pass by and the amount is still the same, the a buzzer would go off to indicate that the amount is still the same. Please help.
Check the how to use millis() sticky.
How can I Store Time and Value?
unsigned long current_time = millis(); //store the current time
rimuru13:
How can I Store Time and Value?
Variables. Basic programming. If you don't know what I mean then you need to stop and go through some of the basic tutorials on coding before you try to go any further.
Don't know how to do the output if the value doesn't change within 3000 ms the buzzer would go off. Here's the code I've done.
const int buttonPin = 4;
const int buzzer = 13;
unsigned long previousMillis = 0;
const long interval = 3000;
int previousCounter = 0;
int counter = 0;
void setup() {
Serial.begin(19200);
pinMode(buttonPin,OUTPUT);
pinMode(buzzer,OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if(digitalRead(buttonPin)==HIGH){
counter++;
previousCounter = counter;
noTone(buzzer);
}}
if((currentMillis - previousMillis <= interval)&&(previousCounter==counter)){
tone(buzzer,1500);
}
Serial.println(counter);
}
I don't know what the counter stuff is, that was not in the original description.
The problem lies in the statement "value doesn't change". I'm defining it one way you will likely define it another.
I define it as follows:
1a. Wait for time to elapse then
1b. If old == new then use digitalWrite to sound the alarm
Therefore the program is:
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
newValue = sensorRead; // don't know what the sensor is
if (oldValue == newValue)
{
oldvalue = newValue;
digitalWrite(buzzer, HIGH);
}
else
{ digitalWrite(buzzer, LOW); }
}
the counter is incrementing the value as i push the button. So in the place of a sensor I used button first that when i pushed a button ,it would change the current value incrementing it. So now my problem is that when I dont push the button and enough time elapsed the buzzer would buzz. Thank you for everyone's help.
I tried to put it in my codes but it doesn't work. Here is my code.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
//PINS
const int buttonPin = 4;
const int relayPin = 8;
const int pulsePin = 11;
const int buzzer = 13;
//BOOLEAN
bool flg = 0;
//INTEGER
volatile int lcdCounter = 0;
volatile int counter = 0;
int bill = 0;
//BUZZER
unsigned long previousMillis = 0;
const long interval = 3000;
int oldValue = 0;
int newValue = 0;
void setup() {
Serial.begin(19200);
pinMode(buttonPin,OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(relayPin,OUTPUT);
pinMode(pulsePin,INPUT);
digitalWrite(relayPin,HIGH);
lcd.init();
lcd.backlight();
lcd.clear();
}
void(*resetFunc)(void) = 0;
void loop() {
if(Serial.available()){
bill = Serial.parseInt();
lcd.clear();
notPushed();
pulseCount();
Serial.println(bill);
digitalWrite(relayPin,HIGH);
counter = 0;
bill = 0;
Serial.println("Thank You for using our service!");
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Thank You!!! ");
delay(5000);
resetFunc();
lcd.clear();
}
else{
digitalWrite(relayPin,HIGH);
lcd.setCursor(15,0);
lcd.scrollDisplayLeft();
lcd.print("Insert Bill ");
delay(250);
}
}
void notPushed(){
while(digitalRead(buttonPin) ==LOW){
lcd.noAutoscroll();
lcd.setCursor(1,0);
lcd.print("Amount Entered: ");
lcd.setCursor(5,1);
lcd.print(bill);
}
lcd.clear();
}
void pulseCount(){
lcdCounter = bill;
digitalWrite(relayPin,LOW);
while(counter<bill){
if(digitalRead(pulsePin) == HIGH){
flg = 0;
}
else if(flg == 0){
flg = 1;
lcdCounter--;
counter++;
notEnough();
}
Serial.print("The count is ");
Serial.println(counter);
lcdCountdown();
}
}
void lcdCountdown(){
lcd.setCursor(1,0);
lcd.print("Amount Left: ");
if(lcdCounter == 9){
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Amount Left: ");
lcd.setCursor(5,1);
lcd.print(lcdCounter);
}
else
{
lcd.setCursor(5,1);
lcd.print(lcdCounter);
}
}
void notEnough(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
newValue = counter;
if(oldValue == newValue){
oldValue = newValue;
tone(buzzer,1000);
digitalWrite(relayPin,HIGH);
}
// else{
// noTone(buzzer);
// resetFunc();
// }
}
}
You are adding a lot of code before you get the simple piece done. Get back to working on this one piece. That will help you determine that you have not stored any initial value for oldValue or newValue. The code provided was a concept, not necessarily directly usable in your sketch. You will need to adapt it to fit your plan and needs.