Problem with sort of debounce on Analog


I need something to do the same thing as debouncing from an digital input but then on the analog.
If i press on de buttons it comes 5times or more on because human speed.
I will switches the lights with this program.

my code:

/*-----( Declare Constants and Pin Numbers )-----*/

#define keyboard_AnalogInput0 0
#define keyboard_AnalogInput1 1
#define keyboard_AnalogInput2 2

#define btnDouche 1
#define btnLavabo 2
#define btnVenti15 3
#define btnSauna 4
#define btnLicht 5
#define btnWC 6
#define btnAllesuit 7
#define btnVenti5 8
#define btnNONE0 10
#define btnNONE1 11
#define btnNONE2 12

/*-----( Declare Variables )-----*/

long lastToggledTime = 0;
long ToggleTime = 50;

int Licht = 70;
int LichtStatus = HIGH;
int Lavabo = 71;
int LavaboStatus = HIGH;
int Douche = 72;
int DoucheStatus = HIGH;
int lastDoucheStatus = LOW;
int WC = 73;
int WCStatus = HIGH;
int Venti = 74;
int VentiStatus = HIGH;
int Bezet = 75;
int BezetStatus = HIGH;
int Sauna = 76;
int SaunaStatus = HIGH;
int Allesuit;
int AllesuitStatus = LOW;

int adc_key_in0 = 0;
int buttonPressed0;
int adc_key_in1 = 0;
int buttonPressed1;
int adc_key_in2 = 0;
int buttonPressed2;

void setup()   /****** SETUP: RUNS ONCE ******/
  Serial.println("Sturing Badkamer");
  pinMode(Licht, OUTPUT);
  digitalWrite(Licht, HIGH);
  pinMode(Lavabo, OUTPUT);
  digitalWrite(Lavabo, HIGH);
  pinMode(Douche, OUTPUT);
  digitalWrite(Douche, HIGH);
  pinMode(WC, OUTPUT);
  digitalWrite(WC, HIGH);
  pinMode(Venti, OUTPUT);
  digitalWrite(Venti, OUTPUT);
  pinMode(Bezet, OUTPUT);
  digitalWrite(Bezet, OUTPUT);
  pinMode(Sauna, OUTPUT);
  digitalWrite(Sauna, HIGH);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
  buttonPressed0 = read_keyboard0();
  buttonPressed1 = read_keyboard1();
  buttonPressed2 = read_keyboard2();

  if (buttonPressed0 != btnNONE0 || buttonPressed1 != btnNONE1 || buttonPressed2 != btnNONE2)
    Serial.print("OK - That looks like you pressed ");
    Serial.println(buttonPressed0, DEC);
    Serial.print("OK - That looks like you pressed ");
    Serial.println(buttonPressed1, DEC); 
    Serial.print("OK - That looks like you pressed ");
    Serial.println(buttonPressed2, DEC); 

  if (buttonPressed0 == 1 || buttonPressed2 == 1){

  if (buttonPressed0 == 2){
       Serial.println("pressed btnLavabo");

  if (buttonPressed0 == 3){
       Serial.println("pressed btnVenti15");

  if (buttonPressed0 == 4 || buttonPressed1 == 4){
       Serial.println("pressed btnSauna");
    if (buttonPressed1 == 5){
        Serial.println("pressed btnLicht");

  if (buttonPressed1 == 6){
       Serial.println("pressed btnWC");

  if (buttonPressed1 == 7){
       Serial.println("pressed btnAllesuit");
  if (buttonPressed2 == 8){
       Serial.println("pressed btnVenti5");

}//--(end main loop )---

int read_keyboard0(){            // Read analog input 0    Viervoudige drukknop

  adc_key_in0 = analogRead(keyboard_AnalogInput0); 

  if (adc_key_in0 > 1000) return btnNONE0; 
  if (adc_key_in0 < 100) return btnDouche;
  if (adc_key_in0 < 380) return btnLavabo;
  if (adc_key_in0 < 555) return btnVenti15;
  if (adc_key_in0 < 800) return btnSauna;
  return btnNONE0;

int read_keyboard1(){            // Read analog input 1    Zesvoudige drukknop

  adc_key_in1 = analogRead(keyboard_AnalogInput1); 

  if (adc_key_in1 > 1000) return btnNONE1; 
  if (adc_key_in1 < 100) return btnLicht;
  if (adc_key_in1 < 380) return btnWC;
  if (adc_key_in1 < 555) return btnSauna;
  if (adc_key_in1 < 800) return btnAllesuit;
  return btnNONE1;

int read_keyboard2(){            // Read analog input 2    Zesvoudige drukknop

  adc_key_in2 = analogRead(keyboard_AnalogInput2); 

  if (adc_key_in2 > 1000) return btnNONE2; 
  if (adc_key_in2 < 500) return btnVenti5;
  if (adc_key_in2 < 1000) return btnDouche;
  return btnNONE2;

int KeuzeDouche(){
        Serial.println("pressed btnDouche");
        if (lastDoucheStatus != DoucheStatus){
          lastToggledTime = millis();
          Serial.println("controlled time");
        if ((millis() - lastToggledTime) > ToggleTime){
          lastDoucheStatus != DoucheStatus;


//*********( THE END )***********

I was trying with the mills but the last IF won’t work with me.

#define btnDouche 1

  pinMode(Douche, OUTPUT);
  digitalWrite(Douche, HIGH);

You don't say which Arduino that you are using, but on a Uno, digital pin 1 is the TX pin for hardware serial.

I am using the Mega with the A0, A1 and A2 as input and 70 till 76 for output.

kennyquintens: I am using the Mega with the A0, A1 and A2 as input and 70 till 76 for output.

The Mega has 54 digital pins and 16 dual purpose pins. The digital pin numbers for the dual purpose pins are 54 to 69. There are no pin numbers 70 to 76.

I'm sorry it's a copy of the Arduino MEGA, I have a chipKIT Max32 and that has 83 useable I/O's. But is this some problem for my code? I'm sorry for the bad info.

Friendly regards, Kenny.

Maybe the way debouncing is dealt with several things at a time will help

In simple terms, it waits a little time between successive reads of the button.


I'm sorry it's a copy of the Arduino MEGA, I have a chipKIT Max32

The ChipShit is NOT a copy of the Arduino Mega. Go ask your ChipShit questions on the ChipShit formum.

I'm sorry but the chipKit works with everything of the Arduino and with the same code, so I think its a copy of the Arduino, I was started with the UNO but I wasn't able to get the Arduino program working on my mac and it didn't have enough I/O for my project. Thats the reason I am using the ChipKit other wise I would like to use the Arduino. Hope that other people can help me.

Friendly regards, Kenny.

Oke, I'm switched back to the Arduino because the ChipKit isn't able to drive my relay board because it runs on 3V3 and it need 5V to turn the relays off. So if anyone can help me with the code, it would be helpful.