Using serial read causing button pressed everytime receiving data

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 :
image

the "print" should appear only when I pressed the print button
TIA

      char buf [10];
      inData.toCharArray(buf, 100);

I am not a user of String functions but it looks to me as though you are putting 100 bytes into a buffer that is only 10 bytes in length

Hey, thanks for the reply, I've tried to change it and its still having the same problem

Post your revised code. There may be other problems

#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.

The button pins are 'normally HIGH' when no button is not connected or not pressed.
Because you coded so with pinMode(x, INPUT_PULLUP);
Leo..

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++.

1 Like

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.