I need to return a single value from incoming serial data...

Post here all questions related to LibMaple core if you can't find a relevant section!
Post Reply
LORDHADES
Posts: 3
Joined: Thu Nov 19, 2020 4:53 am

I need to return a single value from incoming serial data...

Post by LORDHADES »

Hey all !!! Hope you're doing well...

I am new to STM32 and Arduino, and I'm doing a project, which is so difficult for me. I'm trying to get weight data from digital weighing scale through STM32f1, and print current weight on a thermal printer. It's a prototype for my college project.

Setup : Digital weighing scale using Renesas chip connected to one channel of Max232, another channel of Max232 connected to STM32. STM32 is connected to PC via FTDI232 serial to USB converter. A push button is attached to STM32, to facilitate data printing on serial monitor when enabled.

I managed to get the weight data on serial monitor on button press. But the problem is the weight gets printed several times even if the button is pressed for 1 second. If I try some other method, it returns invalid characters infinitely.

For the prototype, I need only current weight data to return. But I have no idea on doing that.

I hope somebody has any suggestions or some help regarding this issue. Thanks in advance.

Forgive me if my English was bad. It isn't my mother tongue. :roll:

P.S. I'm attaching the code I wrote. I hope someone will be able to help me. If there is need for circuit/connection diagram, I can provide. :roll:
I tried to buffer incoming serial values to print current value. But it ended up in vain... :(

I used this tutorial for preliminary setup : "https://www.instructables.com/Getting-S ... duino-IDE/"
Arduino IDE: v1.8.13(Windows Store 1.8.42.0) from Microsoft store
Board Manager: STM32F1XX/GD32F1XX by stm32duino v2020.11.14
Additional board manager URL: "http://dan.drown.org/stm32duino/package ... index.json"

Code: Select all


int inByte;  
const int buttonPin = PB10;
int buttonState;

char recArray[30];
int debouncecnt = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
  Serial2.begin(9600);
  }

void loop() {
  
  buttonState = digitalRead(buttonPin);
  if(buttonState == LOW)
  {
    debouncecnt++;
  }
  
  if (debouncecnt > 100000)
  {
    Serial.println(debouncecnt);
    debouncecnt = 0;
    
    int cnt = 0;
    clearArray();
    Serial2.flush();
    while(true)
    {
      if(Serial2.available()>0)
      {
        byte tmp = Serial2.read();
        if(tmp == '\r' || tmp == '\n')
        {
          break;
        }
        else
        {
          recArray[cnt] = tmp;
          cnt++;
          
          if(cnt > 30)
          {
            break;
          }
        }
      }
      
    }
    Serial.println(recArray);
  }
}

void clearArray()
{
  for(int i = 0; i < 30; i++)
  {
    recArray[i] = '\0';
  }
}
Last edited by LORDHADES on Thu Nov 19, 2020 9:29 am, edited 2 times in total.

stevestrong
Posts: 344
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 7
Location: Munich, Germany
Contact:

Re: I need to return a single value from incoming serial data...

Post by stevestrong »

Edit your post and put the code part into code tags, otherwise it is unreadable.

viewtopic.php?f=2&t=301
Which core do you use?

LORDHADES
Posts: 3
Joined: Thu Nov 19, 2020 4:53 am

Re: I need to return a single value from incoming serial data...

Post by LORDHADES »

stevestrong wrote:
Thu Nov 19, 2020 8:21 am
Edit your post and put the code part into code tags, otherwise it is unreadable.
Sorry sir... Forgive me if I had committed any mistakes. I've done editing it. I've also added required details.

GonzoG
Posts: 65
Joined: Wed Jan 15, 2020 11:30 am
Answers: 5

Re: I need to return a single value from incoming serial data...

Post by GonzoG »

LORDHADES wrote:
Thu Nov 19, 2020 5:27 am

I managed to get the weight data on serial monitor on button press. But the problem is the weight gets printed several times even if the button is pressed for 1 second. If I try some other method, it returns invalid characters infinitely.
....
If you want to use buttons without repeating while button is pressed you want to send data only when there is a change in button state.
For this you need a global variable to store previous button state;

Code: Select all

#define buttonPin PA1
bool prevButtonState=1;  //1 for pulled up pin, 0 for pulled down pin.

void loop
{
....
  bool buttonState = digitalRead(buttonPin);
  if(buttonState != prevButtonState) //button was just pressed or released
  {
    delay(5); //debounce delay
    prevButtonState=buttonState;
    if(!buttonState) //button was pressed
    {
  	Serial.println("pressed");
    }
    else
    {
  	Serial.println("released");
    }
  }
}
Also your debounce method isn't good. It just counts loop cycles when button is pressed.
I know that many are using software debouncing but I prefer RC filters (but I often use interrupts). I start with 10k resistor and 10nF capacitor.

I've added debounce delay as it's the simplest way to debounce buttons but it's not a good way. There are many good examples on the internet using time (millis) or reading port (e.g when program reads 8 LOW/HIGH states in a row)


As for the whole program, I would go different way.
1. don't wait for button to read data from scale. Read them continuously, but set a flag if they are complete or not so you don't send incomplete data.
2. If you get weight from scale, store it in variable.
3. if you press button, send stored data.

LORDHADES
Posts: 3
Joined: Thu Nov 19, 2020 4:53 am

Re: I need to return a single value from incoming serial data...

Post by LORDHADES »

Thanks a lot for replying, Sir. I started learning arduino last week only, that's why this much difficulty. :cry:
My previous output on serial monitor was:

0.900 Kgs
0.900 Kgs
0.900 Kgs
0.900 Kgs
0.902 Kgs
0.900 Kgs
0.900 Kgs
0.900 Kgs
0.900 Kgs

*within a second of pressing button

Loads of thanks for your suggestions... :D I'll try and post the results, Sir..

Post Reply

Return to “General discussion”