Variable not working with keyboard press.

Hi I am a beginner. I am having issues with a variable. I am using a momentary push button switch. When it is pushed I want an indicator light to turn on and it to execute a keyboard press. Then, when pressed again I want the indicator light to turn off and it to execute a different button press. It is executing the keyboard presses without me pressing the button and it is not turning the light on or off at all. What should I do to fix this? Should I be using a variable for this? Code is below! Thanks for your help?

boolean HarnessButtonbuttonPressed;
const int Harnesslight = 4;
const int HarnessButton = 3;
int harnessVariable = 0;
#include <Keyboard.h>
void setup()
{
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
    digitalWrite(HarnessButton, HIGH);
    }


void loop()
{if (digitalRead(HarnessButton) == HIGH) {
  HarnessButtonbuttonPressed = true;
 } else {
  HarnessButtonbuttonPressed = false;
 }


  
if (HarnessButtonbuttonPressed = true && harnessVariable == 0) {
    Keyboard.press(0xC9);
    Keyboard.release(0xC9);
    digitalWrite(Harnesslight, HIGH);
 
    harnessVariable = 1;
    
    }
if (HarnessButtonbuttonPressed = true && harnessVariable == 1) {
   Keyboard.press(0xCA);
    Keyboard.release(0xCA);
    digitalWrite(Harnesslight, LOW);

    harnessVariable = 0;
    }

}
if (HarnessButtonbuttonPressed = true && harnessVariable == 0) {
if (HarnessButtonbuttonPressed = true && harnessVariable == 1) {

Oops2.

And you should only perform your actions when a button becomes pressed not while it is pressed.

Take a look at the StateChangeDetection example in the IDE to see how to do this

A simple example

byte currentInputState = HIGH;
byte previousInputState = HIGH;
const byte inputPin = A3;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  pinMode(inputPin, INPUT_PULLUP);
}

void loop()
{
  currentInputState = digitalRead(inputPin);
  if (currentInputState != previousInputState)
  {
    if (currentInputState == LOW)
    {
      Serial.println("input pin changed from HIGH to LOW");
    }
    else
    {
      Serial.println("input pin changed from LOW to HIGH");
    }
    previousInputState = currentInputState;
  }
}

UKHeliBob:
Take a look at the StateChangeDetection example in the IDE to see how to do this

Before doing that, the OP could be inspired/encouraged to know the difference between == and = operators and their uses.

the OP could be inspired/encouraged to know the difference between == and = operators and their uses.

As previously noted in this thread

Mantra for the day: "I don't feed trolls."

@OP: good luck with your project.

UKHeliBob:
As previously noted in this thread

That is Post#1 with Oops2 -- an expression of cognitive level for a newbie?

Whandall:
Mantra for the day: "I don't feed trolls."

In Internet slang, a troll is a person who starts quarrels or upsets people on the Internet to distract and sow discord by posting inflammatory and digressive, extraneous, or off-topic messages in an ...

Hi I am a beginner. I am trying to use a variable to handle button presses. I have a bunch of momentary Push Button switches. What I am trying to do is when a button is pressed it executes a keyboard press and then turns on and indicator light. Then, when it is pressed again it turns off the indicator light and executes a different keyboard press. It is executing the keyboard presses at the same time without me pressing the button and it is not doing anything with the light. The code I have been trying is below. Should I be using a variable for this? If so, what can I do to fix this? If not, what should I use instead? I have tried State Change detection and I don't think it will work for me. If that is what I should be using can someone show me how it would work into my current code? Thanks for any help. My code is below.

boolean HarnessButtonbuttonPressed;
const int Harnesslight = 4;
const int HarnessButton = 3;
int harnessVariable = 0;
#include <Keyboard.h>
void setup()
{
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
    digitalWrite(HarnessButton, HIGH);
    }


void loop()
{if (digitalRead(HarnessButton) == HIGH) {
  HarnessButtonbuttonPressed = true;
 } else {
  HarnessButtonbuttonPressed = false;
 }


  
if (HarnessButtonbuttonPressed = true && harnessVariable == 0) {
    Keyboard.press(0xC9);
    Keyboard.release(0xC9);
    digitalWrite(Harnesslight, HIGH);
 
    harnessVariable = 1;
    
    }
if (HarnessButtonbuttonPressed = true && harnessVariable == 1) {
   Keyboard.press(0xCA);
    Keyboard.release(0xCA);
    digitalWrite(Harnesslight, LOW);

    harnessVariable = 0;
    }

Duplicate topics merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

My bad! Like I said I was a beginner. Sorry!

I have tried State Change detection and I don't think it will work for me.

Show us.


if (HarnessButtonbuttonPressed = true && harnessVariable == 1)

= true >>>>———> == true

“ Like I said I was a beginner”

You should be able to read posting guidelines.

So I changed the single equal sign to a double one. It half works. It isn't executing the keyboard presses at the same time and it is now executing it once the button is pressed. But it is only executing the second keyboard press and it still isn't working with the light. What should I do to fix this? Code is below! Thanks for any help you can provide!

boolean HarnessButtonbuttonPressed;
const int Harnesslight = 4;
const int HarnessButton = 3;
int harnessVariable = 0;
#include <Keyboard.h>
void setup()
{
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
    digitalWrite(HarnessButton, HIGH);
    }


void loop()
{
  if (digitalRead(HarnessButton) == HIGH) {
  HarnessButtonbuttonPressed = true;
 } else {
  HarnessButtonbuttonPressed = false;
 }


  
if (HarnessButtonbuttonPressed == true && harnessVariable == 0) {
    Keyboard.press(0xC9);
    Keyboard.release(0xC9);
    digitalWrite(Harnesslight, HIGH);
 
    harnessVariable = 1;
    
    }
if (HarnessButtonbuttonPressed == true && harnessVariable == 1) {
   Keyboard.press(0xCA);
    Keyboard.release(0xCA);
    digitalWrite(Harnesslight, LOW);

    harnessVariable = 0;
    }

}
}

Does that code actually compile ?
It seems to have an extra } at the end

Did you look at the ‘Change in state’ example in the IDE ?

Try this:

// Version   YY/MM/DD
//  1.01     20/03/23   Running code

#include <Keyboard.h>

#define PUSHED             HIGH

#define LEDon              HIGH  
#define LEDoff             LOW

const byte Harnesslight  = 4;
const byte HarnessButton = 3;
const byte heartBeatLED  = 13;

byte lastSwitchState;

//timing stuff
unsigned long heartBeatMillis;
unsigned long switchMillis;


//******************************************************************************
void setup()
{
  pinMode(heartBeatLED, OUTPUT);
  pinMode(Harnesslight, OUTPUT);

  pinMode(HarnessButton, INPUT_PULLUP);

} //END of setup()

//******************************************************************************
void loop()
{
  //***************************
  //toggle Heartbeat LED every 500ms
  if (millis() - heartBeatMillis > 500)
  {
    //restart timer
    heartBeatMillis = millis();

    //toggle LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //***************************
  //time to check the switches?
  if (millis() - switchMillis > 50)
  {
    //restart timer
    switchMillis = millis();

    checkSwitches();
  }


} //END of loop()

//******************************************************************************
void checkSwitches()
{
  byte switchState;

  //*******************************************
  switchState = digitalRead(HarnessButton);

  //***********************************
  if (lastSwitchState != switchState)
  {
    //update to the new state
    lastSwitchState = switchState;

    //*****************
    //was it pushed/closed
    if (switchState == PUSHED)
    {
      Keyboard.press(0xC9);
      Keyboard.release(0xC9);
      
      digitalWrite(Harnesslight, LEDon);
    }

    //*****************
    //Must be released/opened
    else
    {
      Keyboard.press(0xCA);
      Keyboard.release(0xCA);
      
      digitalWrite(Harnesslight, LEDoff);
    }
    
  }

} //END of checkSwitches()

Yes, that code compiles. I tried the sketch that larryd gave me. It half works. It works for the '0xC9' button press but it doesn't work with the second. It executes the second press at the same time as the first when the button is pressed for a second time. The light also half works. It stays on until it is pressed and then turns off when pressed but, when I let off the button it turns back on. I am trying to get the light stay on when it is pressed and then have it go off when it is pressed again and and stay off until the button is pressed again and then the light turn back on and have that repeat. Thanks for your guys help on this. One last thing, do you guys use the Arduino programming software or Studio Visual Code. I think Studio Visual Code might work better for this because this code is just a very small portion of the total code that I have. I still have a ton more to do that revolves around me getting this button working. My code is below. Thanks again for all of your help!

boolean HarnessButtonbuttonPressed;
const int Harnesslight = 4;
const int HarnessButton = 3;
int harnessVariable = 0;
#include <Keyboard.h>
void setup()
{
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
    digitalWrite(HarnessButton, HIGH);
    }


void loop()
{
  if (digitalRead(HarnessButton) == HIGH) {
  HarnessButtonbuttonPressed = true;
 } else {
  HarnessButtonbuttonPressed = false;
 }


  
if (HarnessButtonbuttonPressed == true && harnessVariable == 0) {
    Keyboard.press(0xC9);
    Keyboard.release(0xC9);
    digitalWrite(Harnesslight, HIGH);
 
    harnessVariable = 1;
    
    }
if (HarnessButtonbuttonPressed == true && harnessVariable == 1) {
   Keyboard.press(0xCA);
    Keyboard.release(0xCA);
    digitalWrite(Harnesslight, LOW);

    harnessVariable = 0;
    }

}
}

Yes, that code compiles.

I bet it doesn't. As I said, it has an extra } at the end, as does the code in reply #18 which also does not compile