Hello
I have a problem. From the example shown in the "Debounce" ID, I would like to command the same ledPin from two different inputs. These two inputs are the buttonPin and the Serial (from the serial monitor). How should I do? Thank you.
Hello
I have a problem. From the example shown in the "Debounce" ID, I would like to command the same ledPin from two different inputs. These two inputs are the buttonPin and the Serial (from the serial monitor). How should I do? Thank you.
I have two problems
I can't see your code, and I don't understand your problem description.
const int buttonPin = 10; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
// the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int buttonState;
char v5;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
if (ledState == HIGH) {
digitalWrite(ledPin, HIGH);
}
if (ledState == LOW) {
digitalWrite(ledPin, LOW);
}
}
}
}
v5 = Serial.read();
if (v5 == 'b') {
digitalWrite(ledPin, HIGH);
Serial.println("ON 13");
}
if (v5 == 'c') {
digitalWrite(ledPin, LOW);
Serial.println("OFF 13");
}
lastButtonState = reading;
}
There's a handy-dandy link at the top of every forum page titled: Read this before posting a programming question ...
Despite this rather obvious title, you appear to have either not read the post or ignored the advice it contains. Please (re)read it now. Pay particular attention to Item #6 which tell you how to post your code using CODE TAGS so that it doesn't look so ugly and is easier for folks to read. Also, type 'ctrl-t' in the Arduino IDE to format you code with proper indentations.
Doing these things will make your posts and code much more readable. Remember, you're asking people on the forum to help you for FREE. The easier you make it for them, the more inclined they will be to do so. On the other hand, ignoring the well-documented forum guidelines displays a disrespect that will discourage people from helping you.
Excuse the inexperience. I'm new in these things. I simply can not control the pin 13 output with the "buttonState" from the serial monitor. Here is the file.
debounce_from_serial.ino (1.4 KB)
Here is the file.
const int buttonPin = 10; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
// the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int buttonState;
char v5;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
if (ledState == HIGH) {
digitalWrite(ledPin, HIGH);
}
if (ledState == LOW) {
digitalWrite(ledPin, LOW);
}
}
}
}
v5 = Serial.read();
if (v5 == 'b') {
buttonState == HIGH;
Serial.println("ON 13");
}
if (v5 == 'c') {
buttonState == LOW;
Serial.println("OFF 13");
}
lastButtonState = reading;
}
If you use the IDE's auto format tool, it may help you to see where your problem is,
Your debounce code depends on the value of the variable reading, which has no meaning if the LED was set by the serial monitor. Why not just set the pin directly when you see the character?
v5 = Serial.read();
if (v5 == 'b') {
ledState = HIGH;
digitalWrite(ledPin, HIGH);
Serial.println("ON 13");
}
if (v5 == 'c') {
ledState = LOW;
Serial.println("OFF 13");
digitalWrite(ledPin, LOW);
}
You should not, however, read the serial port unless you know a character is there to read by checking Serial.available.
if ( Serial.available() > 0 )
{
v5 = Serial.read();
if (v5 == 'b') {
ledState = HIGH;
Serial.println("ON 13");
} else if (v5 == 'c') {
ledState = LOW;
Serial.println("OFF 13");
}
digitalWrite(ledPin, ledState);
}
clod79:
I have a problem. From the example shown in the "Debounce" ID, I would like to command the same ledPin from two different inputs. These two inputs are the buttonPin and the Serial (from the serial monitor). How should I do? Thank you.
Under what conditions would you like the LED to turn on?
Under what conditions would you like the LED to turn off?