debouncing on the code

i got ghost outputs on my project manual alarm system. do i need to debounce my pushbuttons? i got 15 pushbuttons which makes confusion on the code. here is the code.

int bp1=22;
int bp2=23;
int bp3=24;
int bp4=26;
int bp5=52;
int bp6=28;
int bp7=32;
int bp8=31;
int bp9=30;
int bp10=36;
int bp11=35;
int bp12=34;
int bp13=40;
int bp14=39;
int bp15=38;
int led1=25;
int led2=29;
int led3=33;
int led4=37;
int led5=41;
int ledfire=49;
int ledint=51;
int ledamb=47;
int ledcoe=46;
int ledcme=43;
int ledcnahs=44;
int ledcba=42;
int ledced=45;
int buzz1= 50;
int a = 0;
int b = 0;
int c= 0;
int d= 0;
int e= 0;
int f= 0;
int g= 0;
int h= 0;
int i= 0;
int j= 0;
int k= 0;
int l= 0;
int m= 0;
int n= 0;
int o= 0;

void setup() {

pinMode(bp1,INPUT);
pinMode(bp2,INPUT);
pinMode(bp3,INPUT);
pinMode(bp4,INPUT);
pinMode(bp5,INPUT);
pinMode(bp6,INPUT);
pinMode(bp7,INPUT);
pinMode(bp8,INPUT);
pinMode(bp9,INPUT);
pinMode(bp10,INPUT);
pinMode(bp11,INPUT);
pinMode(bp12,INPUT);
pinMode(bp13,INPUT);
pinMode(bp14,INPUT);
pinMode(bp15,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(ledfire,OUTPUT);
pinMode(ledint,OUTPUT);
pinMode(ledamb,OUTPUT);
pinMode(ledcoe,OUTPUT);
pinMode(ledced,OUTPUT);
pinMode(ledcnahs,OUTPUT);
pinMode(ledcme,OUTPUT);
pinMode(ledcba,OUTPUT);
pinMode(buzz1,OUTPUT);

Serial.begin(9600);
Serial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(500);

// put your setup code here, to run once:

}

void loop() {
a=digitalRead(bp1);
b=digitalRead(bp2);
c=digitalRead(bp3);
d=digitalRead(bp4);
e=digitalRead(bp5);
f=digitalRead(bp6);
g=digitalRead(bp7);
h=digitalRead(bp8);
i=digitalRead(bp9);
j=digitalRead(bp10);
k=digitalRead(bp11);
l=digitalRead(bp12);
m=digitalRead(bp13);
n=digitalRead(bp14);
o=digitalRead(bp15);

if(a==HIGH)
{

digitalWrite(ledfire,HIGH);
digitalWrite(ledcoe,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led1,HIGH);

delay(10000);

digitalWrite(ledfire,LOW);
digitalWrite(ledcoe,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led1,LOW);

}
if(b==HIGH)
{

digitalWrite(ledint,HIGH);
digitalWrite(ledcoe,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led1,HIGH);

delay(10000);
digitalWrite(ledint,LOW);
digitalWrite(ledcoe,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led1,LOW);

}

if(c==HIGH)
{

digitalWrite(ledamb,HIGH);
digitalWrite(ledcoe,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led1,HIGH);

delay(10000);
digitalWrite(ledamb,LOW);
digitalWrite(ledcoe,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led1,LOW);

}
if(d==HIGH)
{

digitalWrite(ledfire,HIGH);
digitalWrite(ledced,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led2,HIGH);

delay(10000);

digitalWrite(ledfire,LOW);
digitalWrite(ledced,LOW);
digitalWrite(led2,LOW);
digitalWrite(buzz1,LOW);

}
if(e==HIGH)
{

digitalWrite(ledint,HIGH);
digitalWrite(ledced,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led2,HIGH);

delay(10000);
digitalWrite(ledint,LOW);
digitalWrite(ledced,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led2,LOW);

}
if(f==HIGH)
{

digitalWrite(ledamb,HIGH);
digitalWrite(ledced,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led2,HIGH);

delay(10000);
digitalWrite(ledamb,LOW);
digitalWrite(ledced,LOW);
digitalWrite(led2,LOW);
digitalWrite(buzz1,LOW);

}
if(g==HIGH)
{

digitalWrite(ledfire,HIGH);
digitalWrite(ledcnahs,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led3,HIGH);

delay(10000);
digitalWrite(ledfire,LOW);
digitalWrite(ledcnahs,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led3,LOW);

}
if(h==HIGH)
{

digitalWrite(ledint,HIGH);
digitalWrite(ledcnahs,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led3,HIGH);

delay(10000);
digitalWrite(ledint,LOW);
digitalWrite(ledcnahs,LOW);
digitalWrite(led3,LOW);
digitalWrite(buzz1,LOW);

}
if(i==HIGH)
{

digitalWrite(ledamb,HIGH);
digitalWrite(ledcnahs,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led3,HIGH);

delay(10000);
digitalWrite(ledamb,LOW);
digitalWrite(ledcnahs,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led3,LOW);

}
if(j==HIGH)
{

digitalWrite(ledfire,HIGH);
digitalWrite(ledcme,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led4,HIGH);

delay(10000);
digitalWrite(ledfire,LOW);
digitalWrite(ledcme,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led4,LOW);

}
if(k==HIGH)
{

digitalWrite(ledint,HIGH);
digitalWrite(ledcme,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led4,HIGH);

delay(10000);
digitalWrite(ledint,LOW);
digitalWrite(ledcme,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led4,LOW);

}
if(l==HIGH)
{

digitalWrite(ledamb,HIGH);
digitalWrite(ledcme,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led4,HIGH);

delay(10000);
digitalWrite(ledamb,LOW);
digitalWrite(ledcme,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led4,LOW);

}
if(m==HIGH)
{

digitalWrite(ledfire,HIGH);
digitalWrite(ledcba,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led5,HIGH);

delay(10000);
digitalWrite(ledfire,LOW);
digitalWrite(ledcba,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led5,LOW);

}

if(n==HIGH)
{

digitalWrite(ledint,HIGH);
digitalWrite(ledcba,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led5,HIGH);

delay(10000);
digitalWrite(ledint,LOW);
digitalWrite(ledcba,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led5,LOW);

}

if(o==HIGH)
{

digitalWrite(ledamb,HIGH);
digitalWrite(ledcba,HIGH);
digitalWrite(buzz1,HIGH);
digitalWrite(led5,HIGH);

delay(10000);

digitalWrite(ledamb,LOW);
digitalWrite(ledcba,LOW);
digitalWrite(buzz1,LOW);
digitalWrite(led5,LOW);

}

// put your main code here, to run repeatedly:

}

Welcome to the forum. Please read How to use the forum and edit you post accordingly.

With that much delay() blocking everything, I doubt it. You can probably empty the whole room before the alarm goes off.

And have you heard of arrays?
And have you heard of arrays?
And have you heard of arrays?
And have you heard of arrays?
And have you heard of arrays?
And have you heard of arrays?*

And variable names are there to help YOU. 'bp4' is almost as useful as just using '26'. Or 'a', what the heck is 'a'???? It's wayyyyyy better to spend a second longer on tying a longer variable name than to short the crap out of it and make everything unreadable.

  • Is funny if you do :wink:

thanks for the comment. the program works fine yet sometimes i get ghost inputs. how do i resolve this? sorry im just new in arduino :smiley:

here is the diagram of my work. i excluded the gsm module functionality on the program.

fdasfsf.png

What was hard about:

septillion:
Welcome to the forum. Please read How to use the forum and edit you post accordingly.

?????

I assume long wires?

Use lower value pull up resistors and filter the input. You can use some debounce routines to do so but with all the delay()'s it will NOT be easy to implement it.

And the unreadable image:
fdasfsf.png
Note, that's not a schematic, that's a Fritzing breadboard mess.

dont have any idea about filtering the input. maybe enlighten me more. :smiley:

A library like Bounce2 can do all the heavy lifting for you. But like I said, with all the delay()'s and no array's it's a complete nightmare to implement it. I would say now you have bugs it's a good time to start V2.0.0 of the software from the ground up :wink:

i am using dupont jumper wires. and how do I get rid of the delays? does that affect the system?

I just can't read the Fritzing mess. Do you have resistors on the buttons? (If so, using INPUT_PULLUP can be used so you don't need them ;))Do you have long wires?

And yes, just removing them will change the program of course. But you can replace them with waits based on millis(). See Blink without delay. It's like when making breakfast. Instead of putting a pan of the stove, do nothing, put egg in pan, do nothing, take egg out of the pan, turn on the oven, do nothing, place bread in the oven, do nohing, etc. That's not how you make breakfast (I hope). Instead of doing nothing while you're egg is boiling you can fix other things. Simply remember when you put the egg into the water and glance at the clock while you're doing other stuff to see if you need to take the eggs out.

yup. i do have 10k resistor connected to ground.. so to sum it up, i need to study about arrays, millis, and bounce2 is that right?

why is it long wires affects the system? I am making a prototype and later will implement it and may use long wires. how will i deal with long wires then?

If you can, avoid them! if you can't, be sure to have at least a lower value pull up (start with 1k) and apply filtering. This can be in hardware or software.

In hardware something like

                      VCC
                       ┬
                       │
       long wire       1k
 ┌─────────────────────┴───100k───┬───Input
 │                                │
 │                              100nF
  \                               │
 │                                │
 ┴                                ┴
GND                              GND

would be pretty efficient I think. Also adds protection to the inputs.

You do not need 15 inputs to read 15 buttons - you need a couple of shift registers. A couple of $5 shift registers means you can save far more than that on the processor.

Debounce is irrelevant in this sketch - any “bouncing” occurs during the 10 second delays. Everyone else is probably right about the spurious inputs: it’s the electronics, not the code.

Speaking of code, replace all of these:

if(o==HIGH)
  {
   
    digitalWrite(ledamb,HIGH);
    digitalWrite(ledcba,HIGH);
    digitalWrite(buzz1,HIGH);
    digitalWrite(led5,HIGH);
   
    delay(10000);
   
    digitalWrite(ledamb,LOW);
    digitalWrite(ledcba,LOW);
    digitalWrite(buzz1,LOW);
    digitalWrite(led5,LOW);
   
    }

with:

  if(o==HIGH)
    doAlarm(ledamb,ledcba,buzz1,led5);

and add a function

void doAlarm(int ledX, int ledY, int buzzer, int ledZ) {
  digitalWrite(ledX,HIGH);
  digitalWrite(ledY,HIGH);
  digitalWrite(buzzer,HIGH);
  digitalWrite(ledZ,HIGH);
   
  delay(10000);
   
  digitalWrite(ledX,LOW);
  digitalWrite(ledY,LOW);
  digitalWrite(buzzer,LOW);
  digitalWrite(ledZ,LOW);
}

It’s still very horrible, but way better than what you have.

Thank you for that PaulMurray. I got my alarm system working with bounce2 library. The ghost inputs has gone.
Septillon, is there a formula to get the capacitance with respect to the distance of long wire? If this is implemented, the wires could reach up to more or less 300 meters for a single pushbutton input.

veraniumhydroxide:
Septillon, is there a formula to get the capacitance with respect to the distance of long wire? If this is implemented, the wires could reach up to more or less 300 meters for a single pushbutton input.

You may want to consider another way to get the inputs to the Arduino. 600 meters for a 5VDC signal is a *long * way. Lots of opportunities for electrical interference and attenuation. Perhaps RS-485 or a fiber optic link to the remote switches would be better.

YMMV

shall i still use capacitors for ripples if I use RS-485 or a fiber optic link?

Indeed, 600m is ridiculous long. Just have a local uC and let it send the data over RS485. Or, it it's a single buton you can connect it straight to a RS485 driver. RS485 is made for long distance so the drivers will do all the work. To power the driver, send a higher voltage over the line. I would say 12V+ and have a local regulator on the other side.

Thank you septillion. No idea what is RS485. Gotta study that how to use. Delay is ok in the system since it is nearly possible for two occuring alarms simultaneously.