I'm using hardware serial to read from scale using rs232, I'm also using regex to get the data I need. It's doing its job, but every time a data is read, a button is also pressed .
#include <Regexp.h>
#include <Wire.h>
const int addButtonPin = 49;
const int printButtonPin = 51;
const int resetButtonPin = 53;
const int henry = 8;
String inData;
String datas;
char cap[10];
char state[1];
boolean addState = LOW;
boolean printState = LOW;
boolean resetState = LOW;
float value = 0;
int set = 0;
boolean debounceButton(boolean state, int readed)
{
boolean stateNow = digitalRead(readed);
if (state != stateNow)
{
delay(10);
stateNow = digitalRead(readed);
}
return stateNow;
}
void matchCallback (
const char * match,
const unsigned int length,
const MatchState & ms
) {
ms.GetCapture(state, 1);
ms.GetCapture(cap, 0);
String s = String(state);
float caped = atof(cap);
value = caped;
Serial.println(caped);
memset(cap, 0, 10);
memset(state, 0, 1);
}
void setup () {
Serial.begin (9600);
Serial1.begin(2400, SERIAL_7N1);
//Pin mode
pinMode(addButtonPin, INPUT_PULLUP);
pinMode(printButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(henry, OUTPUT);
}
void loop () { // what we are searching (the target)
if (Serial1.available()) {
char received = Serial1.read();
// delay(10);
inData += received;
if (received == '\n') {
char buf [10];
inData.toCharArray(buf, 100);
MatchState ms(buf);
ms.GlobalMatch("((.)(%d+)(.)(%d+))", matchCallback);
inData = "";
}
}
if (debounceButton(addState, addButtonPin) == HIGH && addState == LOW) {
addState = HIGH;
Serial.println("add");
datas += String(value);
datas += ";";
} else if (debounceButton(addState, addButtonPin) == LOW && addState == HIGH) {
addState = LOW;
}
if (debounceButton(printState, printButtonPin) == HIGH && printState == LOW ) {
printState = HIGH;
Serial.println("Print");
Serial.println(datas);
datas = "";
}
else if (debounceButton(printState, printButtonPin) == LOW && printState == HIGH)
{
printState = LOW;
}
if (debounceButton(resetState, resetButtonPin) == HIGH && resetState == LOW)
{
resetState = HIGH;
Serial.println("reset");
datas = "";
}
else if (debounceButton(resetState, resetButtonPin) == LOW && resetState == HIGH)
{
resetState = LOW;
}
}
the results are :
the "print" should appear only when I pressed the print button
TIA
#include <Regexp.h>
#include <Wire.h>
const int addButtonPin = 49;
const int printButtonPin = 51;
const int resetButtonPin = 53;
const int henry = 8;
String inData;
String datas;
char cap[10];
char state[1];
boolean addState = LOW;
boolean printState = LOW;
boolean resetState = LOW;
float value = 0;
int set = 0;
boolean debounceButton(boolean state, int readed)
{
boolean stateNow = digitalRead(readed);
if (state != stateNow)
{
delay(10);
stateNow = digitalRead(readed);
}
return stateNow;
}
void matchCallback (
const char * match,
const unsigned int length,
const MatchState & ms
) {
ms.GetCapture(state, 1);
ms.GetCapture(cap, 0);
String s = String(state);
float caped = atof(cap);
value = caped;
Serial.println(caped);
memset(cap, 0, 10);
memset(state, 0, 1);
}
void setup () {
Serial.begin (9600);
Serial1.begin(2400, SERIAL_7N1);
//Pin mode
pinMode(addButtonPin, INPUT_PULLUP);
pinMode(printButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(henry, OUTPUT);
}
void loop () { // what we are searching (the target)
if (Serial1.available()) {
char received = Serial1.read();
inData += received;
if (received == '\n') {
char buf [20];
inData.toCharArray(buf, 20);
MatchState ms(buf);
ms.GlobalMatch("((.)(%d+)(.)(%d+))", matchCallback);
inData = "";
}
}
if (debounceButton(addState, addButtonPin) == HIGH && addState == LOW) {
addState = HIGH;
Serial.println("add");
datas += String(value);
datas += ";";
} else if (debounceButton(addState, addButtonPin) == LOW && addState == HIGH) {
addState = LOW;
}
if (debounceButton(printState, printButtonPin) == HIGH && printState == LOW ) {
printState = HIGH;
Serial.println("Print");
Serial.println(datas);
datas = "";
}
else if (debounceButton(printState, printButtonPin) == LOW && printState == HIGH)
{
printState = LOW;
}
if (debounceButton(resetState, resetButtonPin) == HIGH && resetState == LOW)
{
resetState = HIGH;
Serial.println("reset");
datas = "";
}
else if (debounceButton(resetState, resetButtonPin) == LOW && resetState == HIGH)
{
resetState = LOW;
}
}
here's the revised code. I don't really think that this is a hardware problem, I have 2 Arduino Mega (original one and chinese knock off) they both have the same problem. also its not connected to the buttons rn.
I'm Sorry im forgot to mention that when i pressed the button that get constantly pressed, its stopped pressing repeatedly. I've tested the button and other buttons that I use and they're all normal.
Hello aditya930go
I´m in trouble to understand the parameters for the debounceBotton() function.
Why the debounceBotton() function needs a parameter called "state"? The variable "state" shall be scoped locally and contain the last button state.
Have a nice day and enjoy coding in C++.
hello, thank you so much for your reply, I saw this button debouncing function somewhere in this forum. I add that parameters so I can use it for more than one buttons, it works great, but when I'm using it with the serial read, this problem happens