[SOLVED] Push Button Repeats

Sorry if asked before.

I am working on a project that has two buttons. I have tried Switch State (Edging), Bounce2, and delays while working on this project. I have a two little push button switch with 10K resistors and coded as INPUT_PULLUP on Pin 2 and 3. When a button is pushed it sends a command to the computer over serial. I am monitoring what is sent and it will send it twice with a delay, once on the down, and what seems to be one on the up, if press quickly. If I hold the button it continues to send every time with that delay.

If I use bounce2 method it send the command multiple times rapidly depending on the interval.
If I use the switch state method button 1 will send both command at the same time and button two stops working.

I have posted my code as a file and is currently the bounce2 method. Again sorry if this is posted before. I am new to Arduino and the terminology. If you could point the the correct direction much appreciated.

Photobooth2.ino (4.53 KB)

I have a two little push button switch with 10K resistors and coded as INPUT_PULLUP on Pin 2 and 3.

INPUT_PULLUP is used when you use the internal pullup resistor, NOT external resistors.

PaulS:
INPUT_PULLUP is used when you use the internal pullup resistor, NOT external resistors.

I have tried with both currently it is set for pullup. I still cannot get it to stop repeating

PaulS:
INPUT_PULLUP is used when you use the internal pullup resistor, NOT external resistors.

This advice is especially valid when the external resistors are pulling DOWN!

Delta_G:
How do you have it wired?

But that's not you're problem. Your problem is that you are reacting to the button being pressed instead of only reacting ot it becoming pressed. Once through the loop you check and the button is pressed to you takePic(). The next time through it is still HIGH so you're going to takePic() again. Instead, you should keep track of what the button is so you can only react when there is a change. Check out the state change example.

I have tried switch state but it worked for the bntStart but not bntColor. For some reason it ran both functions.

Delta_G:
And if we could see that code we might be able to help you fix it.

#include <Bounce2.h>

/* This is a button program for the Sparkbooth
 Software.  When a button is pushed it will send
 a key stroke to the PC via serial.write
 and start the software.  This is used in conjuction with ACC Key
 http://www.aacinstitute.org/downloads/aackeys/AACKeys.html for
 serial command input into the computer.
 
 Code has been parse together from examaples from Shifter and Arduino.cc
 */

/* The circuit:
 1 Arduino UNO R3
 1 USB A-B Cable
 2 10kohm Resistors
 20 330ohm Resistors
 2 10 Segment LED Bar
 2 Arcade buttons from sparkfun
 3 74HC595
 */

// Library used for the 74HC595 Shift Registers
#include <Shifter.h>

int SER_Pin = 8;   //pin 14 on the 75HC595
int RCLK_Pin = 11;  //pin 12 on the 75HC595
int SRCLK_Pin = 12; //pin 11 on the 75HC595

#define NUM_REGISTERS 2 //how many registers are in the chain  //Will be switch to three

// Constants won't change
//const int btnStart = 2; //Red Arcade button to start the program
//const int btnColor = 3; //Blue Arcade button to change the picture to Color
#define btnStart 2
#define btnColor 3
// Variables that will change
#define LEDPIN 13
//int LEDPIN = 13; //LED Test pin
int ButtonState[] = {0}; // Variable for the State of the buttons.  In and Array

//initaize shifter using the Shifter library
Shifter shifter(SER_Pin, RCLK_Pin, SRCLK_Pin, NUM_REGISTERS); 
Bounce debStart = Bounce();
Bounce debColor = Bounce();

void setup() {
  shifter.clear();            // clears of pins on shift regs
  shifter.setAll(LOW);        // sets all pins to LOW
  shifter.write();            // writes to the shift regs

  pinMode(btnStart, INPUT_PULLUP);   // Sets btnStart to INPUT Mode
  pinMode(btnColor, INPUT_PULLUP);   // Sets btnColor to INPUT Mode
  debStart.attach(btnStart);
  debColor.attach(btnColor);
  debStart.interval(50);
  debColor.interval(1);

  //  pinMode(btnBW, INPUT);    // Saved for later
  pinMode(LEDPIN, OUTPUT);    // Sets Pin 13 to LED Pin OUTPUT Mode
  pinMode(SER_Pin, OUTPUT);   // Sets Data Pin to OUTPUT Mode
  pinMode(RCLK_Pin, OUTPUT);  // Sets Latch Pin to OUTPUT Mode
  pinMode(SRCLK_Pin, OUTPUT); // Sets Clock Pin to OUT OUTPUT Mode
  Serial.begin(115200);         // Start the serial tranmission
}
void loop() {
  debStart.update();
  debColor.update();
  ButtonState[1] = debStart.read();
  ButtonState[2] = debColor.read();
  //ButtonState[1] = digitalRead(btnStart);    // Sets the button state for the two buttons
  //ButtonState[2] = digitalRead(btnColor);
  shifter.clear();                          // set all pins on the shift register chain to LOW
  shifter.write();                          // send changes to the chain and display them
  digitalWrite(LEDPIN, LOW);                 // Shuts the LED Status pin off


  // If the First button (Start) is pressed call function takePic
  if (ButtonState[1] == LOW) { // This button is always HIGH until pushed then LOW
     takePic();
  }
}

// Function takePic will send the keystroke 'SPACE' to the computer
void takePic() {
  digitalWrite(LEDPIN, HIGH);
  Serial.write(32);                    // ASCII for 'SPACE'
  digitalWrite(LEDPIN, LOW);
  // Signal();                            // Calls Function signal

}

// Function changeColor will send the keystroke 'CTRL+K' to the computer
void changeColor () {
  digitalWrite(LEDPIN, HIGH);
  // char KeyCommand = 77;
  //char KeyCommand[] = {27,44,44,'control',107,46};  
  char KeyCommand[] = {
    27,44,'c','o','m','b','i','n','e',44,'c','o','n','t','r','o','l',46,107,46    }; 
  //char KeyCommand[] = {27,44,68 6f 6c 64,44,'control',107,46};
  //char KeyCommand[] = {27,'hold,control,107.'};
  Serial.println(KeyCommand);
  digitalWrite(LEDPIN, LOW);
  //delay(250);
}

This is giving the best results right now. How ever I am still getting two pushes of the button on my serial monitor.

Thank you Delta_G;

I have gotten my Leonardo in for the keyboard emulation.

here is my new code and this gives me uk every time i press the button. The debounce function is now working as well

So what I am looking for is if button 1 is push it send the space command as a key, if button 2 is pushed it send ctrl + k to the computer.

I don't want any repeats or additional commands sent. This seems to work well until I put the second button on and it's code.

#include <Bounce2.h>

/* Circuit
Arduino Leonardo
Pushbutton Switch from sparkbooth https://www.sparkfun.com/products/9336
Pushbutton wired: COM = Pin3, NC = GND; NO = VCC (+5v)

// this constant won't change:
//const int buttonPin = 2;    // the pin that the pushbutton is attached to
//const int ledPin = 13;       // the pin that the LED is attached to
#define btnStart 2
#define btnColor 3
#define ledPin 13

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int btnTPSt = 0;             // current state of the button
int btnCCSt = 0;
int lastButtonState = 0;     // previous state of the button
int lastbtnCCSt = 0;

Bounce takeP = Bounce();
Bounce changeC = Bounce();


char ctrlKey = KEY_LEFT_GUI;

void setup() {
  pinMode(btnStart, INPUT);
  takeP.attach(btnStart);
  takeP.interval(5);
  pinMode(btnColor, INPUT);
  changeC.attach(btnColor);
  changeC.interval(5);

  pinMode(ledPin, OUTPUT);

}


void loop() {
  takeP.update();
  changeC.update();

  // read the pushbutton input pin:
  btnTPSt = takeP.read();
  btnCCSt = changeC.read();
  // compare the buttonState to its previous state
  if (btnTPSt != lastButtonState) {
    // if the state has changed, increment the counter
    if (btnTPSt == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      takePic();
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = btnTPSt;

  if (btnCCSt != lastbtnCCSt) {
    if (btnCCSt == HIGH) {
      changeColor();
    }
    else {
      Serial.println('off');
    }
  }

  lastbtnCCSt = btnCCSt;
}

void takePic() {
  Keyboard.write('u');
  delay(100);
}


void changeColor() {
  Keyboard.press(ctrlKey);
  Keyboard.print('k');
  delay(100);
  Keyboard.releaseAll();

}

SOLVED

I found that the code was off a little bit. I have gotten the repeats to stop with Software Debounce and proper Switch State code.

I have updated the code if anyone would like to see. Code is not neat and will still need work.

StartButton.ino (2.75 KB)