I am making a doorbell type device in an office(teacher's faculty) where when you are requesting to get in, you need to click first the button outside to see if the teacher is available, the led in each cubicle will then turn on to notify the teacher, and the rgb outside will turn yellow, indicating the request is pending. if the teacher is available, the teacher will press the button and the rgb light outside will turn green. if not there is a delay, and if the delay is met in this code (10 seconds), the request will automatically rejected, and the rgb outside will turn red. What I want is when I pressed the button the conditions will be executed instantly without any delay, instead it takes a second of holding a button before one of the conditions is executed. I don't know what is causing this.
p.s. I am a novice in programming, I apologize in advance if my coding is not meeting the standards.
Here is the code:
#include <Adafruit_NeoPixel.h>
#define numrgb 2
#define brightshit 255
const byte rgbpin = 9;
Adafruit_NeoPixel rgb = Adafruit_NeoPixel(numrgb, rgbpin);
//first teacher
const byte persprofbtn1 = 2;
const byte persprofbtn2 = 4;
const byte persprofled = 6;
//second teacher
const byte sekanprofbtn1 = 3;
const byte sekanprofbtn2 = 5;
const byte sekanprofled = 7;
const byte buzzerPin = 8;
unsigned long previousTime = 0;
// for the delay which will be used to reject
int timer1 = 0;
int timer2 = 0;
boolean state1 = LOW;
boolean state2 = LOW;
void setup() {
Serial.begin(9600);
pinMode(persprofbtn1, INPUT);
pinMode(persprofbtn2, INPUT);
pinMode(persprofled, OUTPUT);
pinMode(sekanprofbtn1, INPUT);
pinMode(sekanprofbtn2, INPUT);
pinMode(sekanprofled, OUTPUT);
rgb.begin();
}
void accepted(char profled, int pnt, int red, int green, int blue) {
digitalWrite(profled, LOW);
rgb.setPixelColor(pnt, rgb.Color(red, green, blue));
rgb.show();
tone(buzzerPin, 500);
delay(2000);
noTone(buzzerPin);
}
void rejected(char profled, int pnt, int red, int green, int blue) {
digitalWrite(profled, LOW);
rgb.setPixelColor(pnt, rgb.Color(red, green, blue));
rgb.show();
tone(buzzerPin, 50);
delay(2000);
noTone(buzzerPin);
}
void setLight(int pnt, int red, int green, int blue) {
rgb.setPixelColor(pnt, rgb.Color(red, green, blue));
rgb.show();
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - previousTime >= 1000) {
previousTime = currentTime;
//first teacher condition
if (digitalRead(persprofbtn1) == HIGH) {
state1 = HIGH;
} else if (digitalRead(persprofbtn2) == HIGH) {
state1 = LOW;
accepted(persprofled, 0, 0, 255, 0);
timer1 = 0;
setLight(0, 0, 0, 0);
} else if ((timer1 == 10) && (digitalRead(persprofled) == HIGH)) {
state1 = LOW;
rejected(persprofled, 0, 255, 0, 0);
timer1 = 0;
setLight(0, 0, 0, 0);
}
//second teacher condition
if (digitalRead(sekanprofbtn1) == HIGH) {
state2 = HIGH;
} else if (digitalRead(sekanprofbtn2) == HIGH) {
state2 = LOW;
accepted(sekanprofled, 1, 0, 255, 0);
timer2 = 0;
setLight(1, 0, 0, 0);
} else if ((timer2 == 10) && (digitalRead(sekanprofled) == HIGH)) {
state2 = LOW;
rejected(sekanprofled, 1, 255, 0, 0);
timer2 = 0;
setLight(1, 0, 0, 0);
}
//serves as a "switch" to proceed in first teacher conditions
if (state1 == HIGH) {
timer1 += 1;
if (digitalRead(persprofbtn1) == HIGH) {
digitalWrite(persprofled, HIGH);
setLight(0, 255, 255, 0);
}
}
//serves as a "switch" to proceed in second teacher conditions
if (state2 == HIGH) {
timer2 += 1;
if (digitalRead(sekanprofbtn1) == HIGH) {
digitalWrite(sekanprofled, HIGH);
setLight(1, 255, 255, 0);
}
}
}
}