een buttonstate lezen na bounce gebruik

Arduino specifieke Software
Berichten: 7
Geregistreerd: 22 Mei 2020, 12:45

een buttonstate lezen na bounce gebruik

Berichtdoor R.Rijkes » 19 Feb 2022, 14:38

Hoi!

Al een tijdje bezig met een joystick project met gebruik van een Teensy2++.
Het ding heeft een mini analoge stick/ 32 buttons en dit staat op een slider (als gashendel).
Hij doet 't prima!
Maarrrr,
Als extra feature zou het mooi zijn als er bij bepaalde buttonpresses, automatische knop en stick handelingen worden uitgevoerd.
"Probleem" is alleen dat ik "bounce.h" in de code gebruik met risingedge en fallingedge, dus kan niet een button lezen met digitalRead.

Hoe doe ik dat?

de code nu: (okay, beetje langdradig zonder array's enzo, slordig maar hij doet 't...)

/* Buttons to USB Joystick Example

You must select Joystick from the "Tools > USB Type" menu

*/

#include <Bounce.h>

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.

int bd = 10; // bd = 10 ms debounce time which is appropriate for most mechanical pushbuttons
Bounce button0 = Bounce(0, bd); Bounce button1 = Bounce(1, bd);
Bounce button2 = Bounce(2, bd); Bounce button3 = Bounce(3, bd);
Bounce button4 = Bounce(4, bd); Bounce button5 = Bounce(5, bd);
Bounce button6 = Bounce(6, bd); Bounce button7 = Bounce(7, bd);
Bounce button8 = Bounce(8, bd); Bounce button9 = Bounce(9, bd);
Bounce button10 = Bounce(10, bd); Bounce button11 = Bounce(11, bd);
Bounce button12 = Bounce(12, bd); Bounce button13 = Bounce(13, bd);
Bounce button14 = Bounce(14, bd); Bounce button15 = Bounce(15, bd);
Bounce button16 = Bounce(16, bd); Bounce button17 = Bounce(17, bd);
Bounce button18 = Bounce(18, bd); Bounce button19 = Bounce(19, bd);
Bounce button20 = Bounce(20, bd); Bounce button21 = Bounce(21, bd);
Bounce button22 = Bounce(22, bd); Bounce button23 = Bounce(23, bd);
Bounce button24 = Bounce(24, bd); Bounce button25 = Bounce(25, bd);
Bounce button26 = Bounce(26, bd); Bounce button27 = Bounce(27, bd);
Bounce button28 = Bounce(28, bd); Bounce button29 = Bounce(29, bd);
Bounce button30 = Bounce(30, bd);

void setup() {
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!
pinMode(0, INPUT_PULLUP); pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP); pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP); pinMode(5, INPUT_PULLUP);
// 6 niet led gedoe
pinMode(7, INPUT_PULLUP); pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP); pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP); pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP); pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP); pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP); pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP); pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP); pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP); pinMode(24, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP); pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP); pinMode(28, INPUT_PULLUP);
pinMode(29, INPUT_PULLUP); pinMode(30, INPUT_PULLUP);
}

void loop() { // read analog inputs and set X-Y position
//met Deadzone voor de analoge stick
int xValue;
int yValue;
int deadZone = 20;
xValue = analogRead(1);
yValue = analogRead(0);
if ((xValue > (512 - deadZone)) && (xValue < (512 + deadZone))) xValue = 512;
if ((yValue > (512 - deadZone)) && (yValue < (512 + deadZone))) yValue = 512;
Joystick.X(xValue);
Joystick.Y(yValue);

int slide;//gashendel:
int deadzoneSlide = 20;
int slidemap;//achteruit is een kwart, vooruit 3 kwart op volledig bereik
slide = analogRead(2);
if (slide <= 255) slidemap = map (slide,0,255,0,512);
if (slide > 255) slidemap = map (slide,256,1023,513,1023);
if ((slidemap > (512 - deadzoneSlide)) && (slidemap < (512 + deadzoneSlide)))slidemap = 512;
Joystick.sliderLeft(slidemap);

// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update(); button1.update();
button2.update(); button3.update();
button4.update(); button5.update();
//button6.update(); 6 niet, led gedoe
button7.update(); button8.update();
button9.update(); button10.update();
button11.update(); button12.update();
button13.update(); button14.update();
button15.update(); button16.update();
button17.update(); button18.update();
button19.update(); button20.update();
button21.update(); button22.update();
button23.update(); button24.update();
button25.update(); button26.update();
button27.update(); button28.update();
button29.update(); button30.update();

// Check each button for "falling" edge.
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)


if (button0.fallingEdge()) {Joystick.button(1, 1); }
if (button1.fallingEdge()) {Joystick.button(2, 1); }
if (button2.fallingEdge()) {Joystick.button(3, 1); }
if (button3.fallingEdge()) {Joystick.button(4, 1); }
if (button4.fallingEdge()) {Joystick.button(5, 1); }
if (button5.fallingEdge()) {Joystick.button(6, 1); }
//geen 6
if (button7.fallingEdge()) {Joystick.button(7, 1); }
if (button8.fallingEdge()) {Joystick.button(8, 1); }
if (button9.fallingEdge()) {Joystick.button(9, 1); }
if (button10.fallingEdge()) {Joystick.button(10, 1); }
if (button11.fallingEdge()) {Joystick.button(11, 1); }
if (button12.fallingEdge()) {Joystick.button(12, 1); }
if (button13.fallingEdge()) {Joystick.button(13, 1); }
if (button14.fallingEdge()) {Joystick.button(14, 1); }
if (button15.fallingEdge()) {Joystick.button(15, 1); }
if (button16.fallingEdge()) {Joystick.button(16, 1); }
if (button17.fallingEdge()) {Joystick.button(17, 1); }
if (button18.fallingEdge()) {Joystick.button(18, 1); }
if (button19.fallingEdge()) {Joystick.button(19, 1); }
if (button20.fallingEdge()) {Joystick.button(20, 1); }
if (button21.fallingEdge()) {Joystick.button(21, 1); }
if (button22.fallingEdge()) {Joystick.button(22, 1); }
if (button23.fallingEdge()) {Joystick.button(23, 1); }
if (button24.fallingEdge()) {Joystick.button(24, 1); }
if (button25.fallingEdge()) {Joystick.button(25, 1); }
if (button26.fallingEdge()) {Joystick.button(26, 1); }
if (button27.fallingEdge()) {Joystick.button(27, 1); }
if (button28.fallingEdge()) {Joystick.button(28, 1); }
if (button29.fallingEdge()) {Joystick.button(29, 1); }
if (button30.fallingEdge()) {Joystick.button(30, 1); }

// Check each button for "rising" edge
// Update the Joystick buttons only upon changes.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)

if (button0.risingEdge()) {Joystick.button(1, 0); }
if (button1.risingEdge()) {Joystick.button(2, 0); }
if (button2.risingEdge()) {Joystick.button(3, 0); }
if (button3.risingEdge()) {Joystick.button(4, 0); }
if (button4.risingEdge()) {Joystick.button(5, 0); }
if (button5.risingEdge()) {Joystick.button(6, 0); }
//weer geen 6
if (button7.risingEdge()) {Joystick.button(7, 0); }
if (button8.risingEdge()) {Joystick.button(8, 0); }
if (button9.risingEdge()) {Joystick.button(9, 0); }
if (button10.risingEdge()) {Joystick.button(10, 0); }
if (button11.risingEdge()) {Joystick.button(11, 0); }
if (button12.risingEdge()) {Joystick.button(12, 0); }
if (button13.risingEdge()) {Joystick.button(13, 0); }
if (button14.risingEdge()) {Joystick.button(14, 0); }
if (button15.risingEdge()) {Joystick.button(15, 0); }
if (button16.risingEdge()) {Joystick.button(16, 0); }
if (button17.risingEdge()) {Joystick.button(17, 0); }
if (button18.risingEdge()) {Joystick.button(18, 0); }
if (button19.risingEdge()) {Joystick.button(19, 0); }
if (button20.risingEdge()) {Joystick.button(20, 0); }
if (button21.risingEdge()) {Joystick.button(21, 0); }
if (button22.risingEdge()) {Joystick.button(22, 0); }
if (button23.risingEdge()) {Joystick.button(23, 0); }
if (button24.risingEdge()) {Joystick.button(24, 0); }
if (button25.risingEdge()) {Joystick.button(25, 0); }
if (button26.risingEdge()) {Joystick.button(26, 0); }
if (button27.risingEdge()) {Joystick.button(27, 0); }
if (button28.risingEdge()) {Joystick.button(28, 0); }
if (button29.risingEdge()) {Joystick.button(29, 0); }
if (button30.risingEdge()) {Joystick.button(30, 0); }
}

Advertisement

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: een buttonstate lezen na bounce gebruik

Berichtdoor shooter » 21 Feb 2022, 10:51

en waarom zou dat niet kunnen want een digitalread is een normale functie, en bounce is daar een uitbreiding op.
kijk maar in de bounce programma hoe dat gedaan wordt. en je kunt dus altijd je eigen functie erbij schrijven dus if read then sequence.
paul deelen
shooter@home.nl

Berichten: 7
Geregistreerd: 22 Mei 2020, 12:45

Re: een buttonstate lezen na bounce gebruik

Berichtdoor R.Rijkes » 18 Apr 2022, 16:09

Sorry voor de late reactie!
Wist niet dat je dat er ook gewoon langs kan gebruiken.
TNX Paul

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Google Adsense [Bot] en 18 gasten