So I tried some code. But whenever I connect ground and Pin 9 it just runs through the Layers without stopping. (so my serial Log just runs "Test1Test2Test3Test4")
What am I doing wrong? How do I get it to just see it as the button was pressed once.
#include <Bounce2.h>
Bounce debouncer = Bounce();
int counter = 0;
const int buttonPin = 9;
int buttonState = 0;
void setup() {
pinMode(1, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
debouncer.attach(buttonPin);
debouncer.interval(5);
}
void loop() {
{
// Update the debouncer
debouncer.update();
// Get the update value
int value = debouncer.read();
if (value == LOW) {
counter++;
digitalWrite(buttonPin, HIGH);
//Reset count if over max mode number
if (counter == 4)
{
counter = 1;
}else {digitalWrite(buttonPin, HIGH);
}
}
}
switch (counter) {
case 1:
Serial.print("Test1");
break;
case 2:
Serial.print("Test2");
break;
case 3:
Serial.print("Test3");
break;
case 4:
Serial.print("Test4");
break;
}
}
// read the pushbutton input pin:
buttonState = digitalRead(value);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
And it helped, that it didn't run through the loop anymore, but it still is giving me "Test1Test1Test1" in the serial monitor.
Is there something else I am missing?
I am connecting ground and Pin 9 with a simple tactile switch.
you should post your complete sketch in a new posting. Exactly that sketch that shows the behaviour you have described. In most cases it is very hard to assume what you have coded that it does not yet work as you want it.
You can post code by using this method that adds the code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
#include "Keyboard.h"
int counter = 0;
const int buttonPin = 9;
const int button1 = 2;
const int button2 = 3;
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(button1, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
counter++;
//Reset count if over max mode number
if (counter == 4)
{
counter = 1;
}
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
switch (counter) {
case 1:
Serial.print("Test1");
if (digitalRead(button1) == HIGH) {
Keyboard.press(99);
}
break;
case 2:
Serial.print("Test2");
if (digitalRead(button1) == HIGH) {
Keyboard.press(100);
}
break;
case 3:
Serial.print("Test3");
if (digitalRead(button1) == HIGH) {
Keyboard.press(97);
}
break;
case 4:
Serial.print("Test4");
if (digitalRead(button1) == HIGH) {
Keyboard.press(55);
}
break;
}
}
}
I'm sorry, I had deleted the Serial.print for a test.
I copied the wrong version. I am just trying to understand why I get a Loop instead of a single button push.
Here is my current Code again.
#include "Keyboard.h"
int counter = 0;
const int buttonPin = 9;
const int button1 = 2;
const int button2 = 3;
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(button1, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
counter++;
//Reset count if over max mode number
if (counter == 4)
{
counter = 1;
}
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
switch (counter) {
case 1:
Serial.print("Test1");
if (digitalRead(button1) == HIGH) {
Keyboard.press(99);
}
break;
case 2:
Serial.print("Test2");
if (digitalRead(button1) == HIGH) {
Keyboard.press(100);
}
break;
case 3:
Serial.print("Test3");
if (digitalRead(button1) == HIGH) {
Keyboard.press(97);
}
break;
case 4:
Serial.print("Test4");
if (digitalRead(button1) == HIGH) {
Keyboard.press(55);
}
break;
}
}
}
This part of your code does the state-change-detection
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
counter++;
//Reset count if over max mode number
if (counter == 4)
{
counter = 1;
}
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
but the part below that does the serial print is executed independent from the if-condition
if (buttonState != lastButtonState) {
and this is the reason why you get a repeated printed test1 test1 test1
If it shall be printed only one time you have either to put the whole switch-case-structure inside the if-condition
or to use another flag-variable something like
boolean textPrinted;
changing textPrinted from false to true inside the if-condition that checks for (textPrinted == false)
locks itselfs and becomes reset if a new button-press occured.