Double Pressing The Button

For some reason this code (which is probably messy as I am using it to learn) does not act in the way I would expect. The goal is to have a joystick button that when pressed sends a pulse on and off continuously. Effectively making a single press of the button into a rapid fire of sorts.

I know how to make it send a pulse and the code below works… BUT it doesn’t press the SAME button each time. It presses button 1, then a second later button 2 and so on. I need it to press the same button each time though. I believe it’s a problem with the digitalwrite line using index. Can anyone explain how to fix this issue?

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
  // Initialize Button Pins
  pinMode(3, INPUT_PULLUP);
    pinMode(4, INPUT_PULLUP);
      pinMode(5, INPUT_PULLUP);
        pinMode(6, INPUT_PULLUP);
          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);

  // Initialize Joystick Library
  Joystick.begin();
}

// Constant that maps the physical pin to the joystick button.
const int pinToButtonMap = 3;

// Last state of the button
int buttonState = HIGH ;
int lastButtonState = LOW ;
int index = 0 ;
int buttonPushCounter = 0 ;

void loop() {

  // Read pin values
  for (int index = 0; index < 13; index++)
  {
     if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
delay(1000); 
digitalWrite(index, LOW);    // unpress button by making the voltage LOW
delay(1000); 
      buttonPushCounter++;
Joystick.setButton(index, !digitalRead(index + pinToButtonMap));
    } else {
          if (buttonState == LOW) {
      // if the current state is LOW then the button went from on to off:
      //YYYYYYYYYYYYYYY turn light on here
buttonPushCounter++;
    } } } }
}

You're checking buttonState in various places, but after from your initialization of it, you never change it.

This code (which is probably messy as I am using it to learn)

Please, if you know it's messy then you should tidy it up. You are asking for help, help us to hep you by posting code that's not messy and thus easier to read. Thank you.

You have 2 seconds of delay inside your for loop, which means it will take 26 seconds to complete, is this what you want to happen?

You have 11 input pins defined but only 1 lastButtonState, you can't record the state of 11 inputs on 1 variable.

not sure where your code is reading the button and setting buttonState?

here's some code that generates butPresses if the but is held down after 1 sec

#define ButPin  A1
#define LedPin  10

// ---------------------------------------------------------
#define SIX_SEC     1500

void
loop (void)
{
    static unsigned long msecDly  = 0;
    static unsigned long msecLst  = 0;
           unsigned long msec     = millis ();

    int butPress = 0;

    if (LOW == digitalRead (ButPin))  {
        if (! msecLst)  {
            msecLst = msec;
            msecDly = 1000;
            butPress = 1;
        }
        else if (msec - msecLst > msecDly)  {
            msecLst = msec;
            msecDly = 100;
            butPress = 1;
        }
    }
    else
        msecLst = 0;

    if (butPress)  {
        butPress = 0;
        digitalWrite (LedPin, LOW);     // on
        delay (5);
        digitalWrite (LedPin, HIGH);     // off
    }
}

// ---------------------------------------------------------
void
setup (void)
{
    Serial.begin (115200);
    pinMode (ButPin, INPUT_PULLUP);
    pinMode (LedPin, OUTPUT);
    digitalWrite (LedPin, HIGH);
}

Sorry, this is getting painful!

Is this related to / the same problem as your inquiry in Project Guidance?

You have a toggle switch, and you want it to act like a toggle switch, flip it up, lights come on, flip it down, lights go off type of thing.

But your interface is expecting a pushbutton. Press/release, lights go on. Press/release (again, the same button) and the lights go off.

Is that correct? Your code posted is just cycling through the buttons and as wildbill points out you never change buttonState, so it doesn’t do anything. BTW the loop runs off the end of your switches, it should run to < 11, the number of switches, not 13, the number of the last switch. You are adding 3 (pinToButtonMap) to the index, so it should top out at 10 which would then be talking about 10 + 3, 13 the number of the last switch.

Assuming you write some code that actually follows a button and can see when it goes down (“becomes pressed”) AND when it goes up (“becomes unpressed”, or is released), you have to put, at each of those two points, a complete virtual press AND unpress of the joystick equivalent virtual button.

Below is the loop() in the State Change Detection example, stripped of everything I could strip. It looks at ONE button on i/o pin number buttonPin. It gooses joystick button number joystickButtonNumber. It’s your circuit, so you gotta supply those dets.

Where it prints “on” and “off” are where it is “detecting” the button going down and up respectively.

Along side the printing, I’ve placed what looks like your joystick library expects. As has been pointed out, there is no way to insure that your switch “up” will be equal to “on”, as there is no way to determine the original state, so your switch may be acting like it is upside down, or like three-way switches in your house - you toggle the switch from wherever it is to wherever it wasn’t, and the lights change. If they were out, the go on, &c.

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) {
      // if the current state is HIGH then the button went from off to on:

      Serial.println("on");

// "press" the joystick button for 10 milliseconds

       Joystick.setButton(joystickButtonNumber, HIGH);
       delay(10);
       Joystick.setButton(joystickButtonNumber, LOW);


    } else {
 
       Serial.println("off");

// "press" the joystick button for 10 milliseconds

       Joystick.setButton(joystickButtonNumber, HIGH);
       delay(10);
       Joystick.setButton(joystickButtonNumber, LOW);

    }

    delay(50);
  }

  // NOTE: save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}

Your original code suggests your joystick “buttons” are numbered from zero. So you have to figure out what joystickButtonNumber corresponds to what Arduino i/o pin you want your toggle switch on.

Or I have it all wrong and this was a waste of time. Oh well.

HTH

a7

No, I think you got it exactly right. I finally understand what a few lines do and mean now too. So that will help me in the future.

you have to put, at each of those two points, a complete virtual press AND unpress of the joystick equivalent virtual button.

I had a light go off in my head last night when I thought about that. Thanks for confirming it.

I'll see what happens later today when I try to clean it all up. Thanks again. I'll let you know how it goes.

Success! I now have it working thanks to all of the help here. Special thanks to alto777.