[Solved] 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!
LORDHADES
Posts: 24
Joined: Thu Nov 19, 2020 4:53 am

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

Post by LORDHADES »

Code: Select all

int inByte;
const int buttonPin = PA0;     
const int ledPin =  PC13;      
int is_button_press = 0;         
int debounce_delay = 300;

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

void loop() 
{
  is_button_press = digitalRead(buttonPin);

  if (Serial2 || is_button_press == HIGH) 
  {
    delay(debounce_delay);
    if(is_button_press == HIGH)
    {
      digitalWrite(ledPin, HIGH);
    }
    inByte = Serial2.read();
    digitalWrite(ledPin, HIGH);
    Serial.write(inByte);
    delay(100);
  } 
  
  else 
  
  {
    digitalWrite(ledPin, LOW);
  }
}
This is not working... :cry: :cry: :cry:

output is :

0


0


2

for 0.022 kg. When I remove the delay, the output is:

000.020?????????????????????????????????????????????????????????????????

I thank all for being so grateful. It seems I have to wage a war against the setup. Thanks again...

P.S. If someone suggest me a best e-book to learn arduino programming, I'll be so grateful to ya. Thanks in advance.
LORDHADES
Posts: 24
Joined: Thu Nov 19, 2020 4:53 am

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

Post by LORDHADES »

I am explaining my requirement again.

* I have an electronic scale connected to stm32 via max232.
* Stm32 is connected to pc via ftdi.
* I'm using arduino ide for programming and i'm using cores and board managers from stm32duino.

* I need to print the weight from scale on a thermal printer only once per button press.
* To check whether it's working, I'm testing it via serial monitor.
* But weight gets printed more than once on button press, or weight gets printed continuously, even if I don't press button.

* If someone has any Idea, please help me. Or if you have any other ways for implementing this, Please help me. This is killing me...

Thanks in advance. Have a nice day... :cry:
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

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

Post by fredbox »

P.S. If someone suggest me a best e-book to learn arduino programming, I'll be so grateful to ya. Thanks in advance.
The book I started with many years ago and still use as a reference is "Arduino Cookbook."

See https://books.google.com/books?id=raHyK ... &q&f=false page 142 for an example on how to detect a switch closure.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

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

Post by stevestrong »

Try this:

Code: Select all

void loop() 
{
  if (Serial2.received()>=0) // serial data available?
    inByte = Serial2.read();

  if (is_button_press==LOW && !digitalRead(buttonPin)) // button pressed?
  {
    is_button_press = HIGH;
    digitalWrite(ledPin, LOW); // turn LED on
    Serial.write(inByte);
    delay(debounce_delay);
  }
  
  if (is_button_press==HIGH && digitalRead(buttonPin)) // button released?
  {
    is_button_press = LOW;
    digitalWrite(ledPin, HIGH); // turn LED off
  }
}
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

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

Post by mlundin »

So without bothering about code syntax lets consider what you want to do.
So first lets be clear that button up and down is not quite the same as button pressed and released.

Second dont mix up

Code: Select all

loop() /* do this forever */

Read any new characters from the scales, Serial2, and store them in an array. save this array as current weight when you read end of line. 

Read and debounce button state, up or down, remember what it was last time

Test if button falling, if it was up last time and down this time 
	If presssed print the current weight value

Notice that you have all of these functions in your previous posts, the trick is to disentangle things and also not to mix them up because, its important to keep each logical part of your requirements separate. Also notice this is exactly your requirements as stated.
Now this must be turned into arduino C/C++ code.
First go back and read and UNDERSTAND how the previous code you have tested works, why does it do as it does like printing all the time button is pressed or not printing complete weight values or not. Read all if statements carefully and draw logical diagrams. You can find the button logic in @GonzoG's first post, the reading of weight sentences is in your first post
LORDHADES
Posts: 24
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: Sat Nov 28, 2020 8:55 am Try this:

Code: Select all

void loop() 
{
  if (Serial2.received()>=0) // serial data available?
    inByte = Serial2.read();

  if (is_button_press==LOW && !digitalRead(buttonPin)) // button pressed?
  {
    is_button_press = HIGH;
    digitalWrite(ledPin, LOW); // turn LED on
    Serial.write(inByte);
    delay(debounce_delay);
  }
  
  if (is_button_press==HIGH && digitalRead(buttonPin)) // button released?
  {
    is_button_press = LOW;
    digitalWrite(ledPin, HIGH); // turn LED off
  }
}
Output renders one inverted question mark per button press... :cry:
I'm having hard time with this bro. I'm working with other ways and methods, still no luck... Seems this has to be dropped... :cry: :cry: :cry: :cry:
Thanks a lot for spending your time for me.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

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

Post by stevestrong »

The question is what is a "value" returned by the scale? Which format has the value? Is it a single byte or they are several bytes? In which order are they sent?

I assume that there must be several bytes, so that my example has to be extended with receiving them one by one. I can do that but only after knowing more information.

Can you post a link to the used scale or user manual?
Last edited by stevestrong on Sat Nov 28, 2020 11:07 am, edited 1 time in total.
LORDHADES
Posts: 24
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: Sat Nov 28, 2020 11:01 am The question is what is a "value" returned by the scale? Which format has the value? Is it a single byte or they are several bytes?
Can you post a link to the used scale?
It is a custom built scale, known as Acurel, brother. It renders value like "000.112" (in Kgs on serial monitor)
Last edited by LORDHADES on Sat Nov 28, 2020 11:15 am, edited 2 times in total.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

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

Post by stevestrong »

Are there always 7 bytes? In which interval are they sent? Each second or once each time you put someting on it?
LORDHADES
Posts: 24
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: Sat Nov 28, 2020 11:07 am Are there always 7 bytes? In which interval are they sent? Each second or once each time you put someting on it?
Each Second, I think... Weight on the scale deviates by +or- 2 over time. This is also continuously updated on the serial monitor.

"https://www.acurelweighingindia.com/pre ... ion-scales"

My scale is similar to first scale listed on the above website.
Post Reply

Return to “General discussion”