So I made a Handbrake, and everything works. In fact, I will pull it right now. pppppppppppppppppppppppppppppppppppppppp. The reason I made so many p's pop up is because I need to pull the handbrake for longer periods of time in some games and on some corners, and It stutters even like this. I want to somehow make the code so it is able to stay fully pressed, not press it a bajilion times. I need to to simulate a key press. And how ever long I pull the hand brake is how long the key is pressed down. So any way to make the code I have made better and to simulate a key being pressed down not getting pressed a gajillion times a second? Oh, and this is the code:
As I said StateChangeDetection is an example in the IDE
Detect when the button becomes pressed and start outputting 'p' as often as you want, then detect when the button becomes released and stop outputting 'p'
Try amending the code yourself and come back for help if you are stuck
Kamiizoo:
Uh what do you mean by StateChangeDetection?
You can find the StateChangeDetection example code in the Arduino IDE at File > Examples > 02.Digital > StateChangeDetection. Here's the tutorial that goes with it: https://www.arduino.cc/en/Tutorial/StateChangeDetection
UKHeliBob:
As I said StateChangeDetection is an example in the IDE
Detect when the button becomes pressed and start outputting 'p' as often as you want, then detect when the button becomes released and stop outputting 'p'
Try amending the code yourself and come back for help if you are stuck
Is there a way to add in something like when Pin 9 release, do Keyboard.release('p');?
pert:
You can find the StateChangeDetection example code in the Arduino IDE at File > Examples > 02.Digital > StateChangeDetection. Here's the tutorial that goes with it: https://www.arduino.cc/en/Tutorial/StateChangeDetection
I am very confused on how and what this exactly does still. I found the code but it's jibberesh to me. This is my first time doing something like this BTW
PaulS:
Of course. The fact that you asked this implies that you have not looked at the state change detection example. Close your browser until you have.
It greatly confuses me and I have no idea what i'm doing. This is my first time doing something like this.
It greatly confuses me and I have no idea what i'm doing.
The example reads the state of the switch pin, on this pass through loop(). Can you see where it does that?
It compares the current state to the state of the pin on the last pass. Can you see where it does that?
If the state has changed, it then considers whether the current state is pressed (the change was to pressed) or released (the change was to released). Can you see where it does that?
What happens when the change is to pressed can be deleted, and replaced with whatever code you want, like the code to press a key. What happens (nothing, in the example) when the change is to released can be replaced by whatever code you want, like the code to release a key.
Kamiizoo:
I am very confused on how and what this exactly does still. I found the code but it's jibberesh to me. This is my first time doing something like this BTW
Did you read the entire tutorial at the link I provided? If it still doesn't make sense to you after reading that, it means that you're trying to jump into more advanced concepts without first taking the time to learn the fundamentals. Start working your way through the File > Examples > 01.Basics sketches.
For each example, go through the code line by line, making sure you completely understand what each line of code does. If you don't understand, you need to research until you do understand. A good place to start is the Language Reference:
If you don't find the information there, use Google.
Once you think you understand the example sketch completely, verify your understanding by modifying the code and then running it on your Arduino board to verify that your change had the expected effect.
This might seem slow, but it's actually way faster less frustrating in the end than operating in a state of perpetual confusion and just trying random code without any understanding of what that code does.
pert:
Did you read the entire tutorial at the link I provided? If it still doesn't make sense to you after reading that, it means that you're trying to jump into more advanced concepts without first taking the time to learn the fundamentals. Start working your way through the File > Examples > 01.Basics sketches.
For each example, go through the code line by line, making sure you completely understand what each line of code does. If you don't understand, you need to research until you do understand. A good place to start is the Language Reference: Arduino Reference - Arduino Reference
If you don't find the information there, use Google.
Once you think you understand the example sketch completely, verify your understanding by modifying the code and then running it on your Arduino board to verify that your change had the expected effect.
This might seem slow, but it's actually way faster less frustrating in the end than operating in a state of perpetual confusion and just trying random code without any understanding of what that code does.
PaulS:
The example reads the state of the switch pin, on this pass through loop(). Can you see where it does that?
It compares the current state to the state of the pin on the last pass. Can you see where it does that?
If the state has changed, it then considers whether the current state is pressed (the change was to pressed) or released (the change was to released). Can you see where it does that?
What happens when the change is to pressed can be deleted, and replaced with whatever code you want, like the code to press a key. What happens (nothing, in the example) when the change is to released can be replaced by whatever code you want, like the code to release a key.
I was at school so sorry for the wait. I remade a code now, and removed what I didn't need and added what I did, or what I think I need. It now just spams "p" without me touching the switch.
The sketch:
const int buttonPin = 9; // the pin that the pushbutton is attached to #include <Keyboard.h>
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
// 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:
Keyboard.press('p');
// if the current state is LOW then the button went from on to off:
if (buttonState == LOW) {
Keyboard.release('p');
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;