debounce works on one function but not the other, same button for both.

Hey everyone! I am struggling and am in need of some expert advice…A little background, I am a mechanic by trade and hobby and can and have fixed everything from microwaves to small aircraft and almost everything in between. But this programming language is kicking my butt! I just can’t visualize it like I can most other things. Any way on to my problem.

I am building a push button start/stop with RFID “key” for my f150 Lightning, and eventually I want to add some other options too, but one thing at a time. I have watched so many tuturials that, I thought I was going blind, tried using the libraries and still struggled. What I want is to have the rfid chip in my pocket, get in the truck, the RFID goes “green” allowing the push button to start the truck, by holding down the button for 2 seconds, turn on the ignition relays and at 3 seconds engage the starter until I let off the button. Once the truck is started I want to run an input from my tach to disable the starter. If the RFID chip is out of range, then the push button will still turn on the ACC (like turning the key back) so I can listen to radio etc…

I have written the program and laid it out on my breadboard so that “inputPin1” (the push button) will toggle output “ledPin” (the ignition relay output) only if “inputPin2” (input signal from the RFID module) is “high.” And it turns on the “ledPin” just fine, but I want to be able to turn off “ledPin” regardless of what state “inputPin2” is in… I can also toggle the “accPin” (the output to control the ACC relay)
with the “inputPin1” if “inputPin2” is “low” but my debounce doesn’t work on that function, so the results are subpar, how do I debounce this function alone? Thanks EVeryone

int ledPin = 11;               // led in place of ignition relay for prototyping purposes only
int inputPin1 = 2;            // input from button to start the truck
int inputPin2 = 7;            // input from rfid relay to allow truck to start
int accPin = 12;              // accPin to turn on accesories. 

boolean lastButton = LOW;
boolean currentButton = LOW;
boolean currentButton2 = LOW;
boolean lastButton2 = LOW;
boolean ledOn = false;
boolean accOn = false;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin1, INPUT);
  pinMode(inputPin2, INPUT);
  pinMode(accPin, OUTPUT);
}

boolean debounce(boolean last)
{
  boolean currentButton = digitalRead(inputPin1);
  if (last != currentButton);
    {
    delay(5);
    currentButton = digitalRead(inputPin1);
      }
  return currentButton;
}
 boolean debounce2(boolean last2) 
 {
  boolean currentButton2 = digitalRead(inputPin1);
  if (last2 != currentButton2);
  {
    delay(5);
    currentButton = digitalRead(inputPin1);
  }
  return currentButton2;
 } 
void loop()
{
  currentButton = debounce(lastButton);
  if (digitalRead(inputPin1) == LOW && lastButton == HIGH && digitalRead(inputPin2) == HIGH && lastButton == HIGH){
   ledOn =! ledOn;
  }
   {
     lastButton = currentButton;
     digitalWrite(ledPin, ledOn);
     }
    {
      currentButton2 = debounce(lastButton2);
      if (digitalRead(ledPin) == LOW && lastButton2 == HIGH && digitalRead(inputPin2) == LOW){
        accOn =! accOn;
      }
      {
        lastButton2 = currentButton2;
        digitalWrite(accPin, accOn);
         }
}
   }

Oh and why does this text box shake up and down as you type making it a real pia to see what youre typing…

sketch_may04a_worksbutbounces_onebutton.ino (1.54 KB)

Here is one way to do it.

byte LEDpin[] = {
  4, 5}; // number of LEDs
byte ButtonPin[] = {
  2, 3}; //Number of buttons

int button[sizeof(ButtonPin)];
int last[sizeof(ButtonPin)];
int lastButtonState[sizeof(ButtonPin)];

long lastDebounceTime[sizeof(ButtonPin)];  
long debounceDelay = 50;

boolean latch[sizeof(ButtonPin)];

void setup() {
  for(int count = 0; count < sizeof(ButtonPin); count++) {
    pinMode(LEDpin[count], OUTPUT);
    pinMode(ButtonPin[count], INPUT);
  }
}

void loop() {
  for(int count = 0; count < sizeof(ButtonPin); count++) {
    button[count] = digitalRead(ButtonPin[count]);
    if ( button[count] != lastButtonState[count]) {
      lastDebounceTime[count] = millis();
      lastButtonState[count] = button[count];
    } 

    if ((millis() - lastDebounceTime[count]) > debounceDelay) {

      if (button[count] == HIGH && button[count] != last[count]) {
        latch[count] = !latch[count];
        latch[count] ? digitalWrite(LEDpin[count], HIGH): digitalWrite(LEDpin[count], LOW);
      }
      last[count] = button[count];
    }
  }
}

Thanks for the fast reply! Ill give that a shot ASAP!

I think I know why the second one doesn't work like the first one. You're missing a two. Standard cut and paste "missed one" error.

 boolean debounce2(boolean last2) 
 {
  boolean currentButton2 = digitalRead(inputPin1);
  if (last2 != currentButton2);
  {
    delay(5);
    currentButton/* You're missing a 2 here */ = digitalRead(inputPin1);
  }
  return currentButton2;
 }

The program works but not how I want it to. Ill elaborate more latere. Thanks for your time.

HazardsMind:
Here is one way to do it.

byte LEDpin[] = {

4, 5}; // number of LEDs
byte ButtonPin = {
  2, 3}; //Number of buttons

int button[sizeof(ButtonPin)];
int last[sizeof(ButtonPin)];
int lastButtonState[sizeof(ButtonPin)];

long lastDebounceTime[sizeof(ButtonPin)]; 
long debounceDelay = 50;

boolean latch[sizeof(ButtonPin)];

void setup() {
  for(int count = 0; count < sizeof(ButtonPin); count++) {
    pinMode(LEDpin[count], OUTPUT);
    pinMode(ButtonPin[count], INPUT);
  }
}

void loop() {
  for(int count = 0; count < sizeof(ButtonPin); count++) {
    button[count] = digitalRead(ButtonPin[count]);
    if ( button[count] != lastButtonState[count]) {
      lastDebounceTime[count] = millis();
      lastButtonState[count] = button[count];
    }

if ((millis() - lastDebounceTime[count]) > debounceDelay) {

if (button[count] == HIGH && button[count] != last[count]) {
        latch[count] = !latch[count];
        latch[count] ? digitalWrite(LEDpin[count], HIGH): digitalWrite(LEDpin[count], LOW);
      }
      last[count] = button[count];
    }
  }
}

Yep, missed the two, not sure how I missed it, lol I proofread that program looking for just such things about 10 times. But sadly it didn't take care of the bounce in one function and not the other.

TanHadron:
I think I know why the second one doesn't work like the first one. You're missing a two. Standard cut and paste "missed one" error.

 boolean debounce2(boolean last2) 

{
  boolean currentButton2 = digitalRead(inputPin1);
  if (last2 != currentButton2);
  {
    delay(5);
    currentButton/* You're missing a 2 here */ = digitalRead(inputPin1);
  }
  return currentButton2;
}

Any time you find yourself numbering variables to tell them apart you should consider whether you ought to be using arrays instead. Moving from simple variables to arrays introduces a little complexity but once you're familiar with it you will find it takes almost no extra effort, and saves from from a whole slew of copy/paste errors like this.