Hello everybody.
I'm doing a simple project and in it i have 2 push buttons.
I wanna create an array of ints which only consists of 0s and 1s.
And i want to get these 0s and 1s from these 2 push buttons.
For example the left button represents 0, and the right button represents 1.
And i press, left, right, right, left, right...
I get an array like this : {0, 1, 1, 0, 1, ...}
I'm having trouble with this state changing process.
I tried pulseIn but it misses some presses.
Please help me
Thank you in advance.
Hey, i don't have a code to be corrected, i want the whole code
And the wiring is pretty simple.
2 push buttons , each one with a leg into gnd and the other to two arduino pins
And in the code
Hey, i don't have a code to be corrected, i want the whole code
hey, I guess if you want a code - you should start to write it.
And the wiring is pretty simple.
2 push buttons , each one with a leg into gnd and the other to +5v
so your button will shortcut between gnd and +5. That's really very simple. Can you show a picture of your wiring?
If you don't understand our answers here: we are not here to do your homework.
Come up with a code & schematics and ask a specific question.
This is how this forum works.
If you want someone else to do your job, make an offer in the subforum Gigs & Collaboration and ask for payed help.
alireza_alavi2317:
And the wiring is pretty simple.
2 push buttons , each one with a leg into gnd and the other to +5v
Simple but completely wrong. That shorts out the power supply. And anyway there is no connection to the Arduino so how can you read the state of the buttons?
slipstick:
Simple but completely wrong. That shorts out the power supply. And anyway there is no connection to the Arduino so how can you read the state of the buttons?
Steve
I'm so sorry, of course that was a mistake, i did not mean +5v
Instead the buttons are each connected to an arudino pin
And as i said before ,this is just a part of a long project and i have done everything except this 'button reading' stuff that i left blank in that part of my code.
So i'm not really want you guys to do my job or anything like that.
I purely have a programming problem here so i just wanted to know a correct way of the thing that i wanted the buttons to do.
It's easier for the helpers if you can give a minimum sketch which shows what you need and where you struggle.
My next questions are:
how many button presses to you expect?
how large is your result array?
what should happen if you are at the end of the array/the maximum storing capacity of the array is reached?
anyhow. this is a working sketch with nonblocking debouncing of buttons:
/*
Debounce 2 Buttons
and fill an array
by noiasca
2020-02-27
https://forum.arduino.cc/index.php?topic=667200.0
*/
// constants won't change. They're used here to set pin numbers:
const byte button0Pin = A0; // the number of the pushbutton pin
const byte button1Pin = A1; // the number of the pushbutton pin
byte result[5];
const byte maxResut = sizeof(result);
byte index = 0; // current position to write
class Button
{
// returns HIGH if button was klick since last call - debounce
// the following variables are ONE BYTE only as we don't measure absolute running
byte buttonPin;
const byte debounceDelay = 50; // the debounce time; increase if the output flickers
bool lastButtonState = HIGH; // the previous reading from the input pin
byte lastDebounceTime = 0; // the last time the output pin was toggled - we check only ONE byte
public:
Button(byte attachTo) : buttonPin(attachTo) {}
void begin() {
pinMode(buttonPin, INPUT_PULLUP);
}
bool wasPressed() {
bool buttonState = LOW; // the current reading from the input pin
byte reading = !digitalRead(buttonPin); // if we are using INPUT_PULLUP we are checking invers to LOW Pin
if (((millis() & 0xFF ) - lastDebounceTime) > debounceDelay) // If the switch changed, AFTER any pressing or noise
{
if (reading != lastButtonState && lastButtonState == LOW) // If there was a change and and last state was LOW
{
buttonState = HIGH;
}
lastDebounceTime = millis() & 0xFF;
lastButtonState = reading;
}
return buttonState;
}
};
Button button0(button0Pin);
Button button1(button1Pin);
void setup() {
Serial.begin(115200);
button0.begin();
button1.begin();
}
void loop() {
if (button0.wasPressed())
{
Serial.println(F("0 pressed"));
result[index] = 0;
index++;
}
if (button1.wasPressed())
{
Serial.println(F("1 pressed"));
result[index] = 1;
index++;
}
if (index >= sizeof(result))
{
Serial.println(F("wrap around"));
Serial.print (F("result[]="));
for (auto &i : result) Serial.print(i); // print array
Serial.println();
index = 0;
memset(result, 0, sizeof(result)); // delete array
}
}
next step is to use an array of objects for the buttons and get rid of some code duplicates, but the concept should be clear. Basically it's a OOP approach of the IDE example 02. Digital | Debounce.