hey guys,
I am fairly new to arduino and have been stuck on a project for weeks now... My project was to try and create a three button keyboard with arduino. That is to say, there are three buttons, and each one prints something into the serial COM. I was also going to be able to change what each one prints by typing into the serial com, but I haven't gotten there yet.
Here's a slightly modified version of the debounce code that works (note: it's only for the first button right now):
const int buttonPin = 9; // the number of the pushbutton pin
// Variables will change:
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
Serial.print("1");
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
When I tried to write it myself, it didn't work...
I can't even find the difference between the two.
here's my one:
const int b1 = 9;
int b1State;
int b1LastState = LOW;
long b1lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
Serial.begin(9600);
pinMode(b1, INPUT);
}
void loop() {
int reading1 = digitalRead(b1);
if (reading1 != b1LastState) {
b1lastDebounceTime = millis();
}
if (millis()-b1lastDebounceTime > debounceDelay) {
if (reading1=!b1LastState) {
b1LastState = reading1;
Serial.print("1");
}
}
}
I would really appreciate it if someone could explain what's not working.
Thanks so much in advance,
Pacdude42