Page 3 of 4
Re: I need to return several values from incoming serial data...
Posted: Sat Nov 28, 2020 11:24 am
by stevestrong
Try this (beside your setup)
Code: Select all
#define NR_BYTES 10
uint8 inBytes[10], inCnt;
uint32 rxTime;
void loop()
{
if (Serial2.received()>=0) // serial data available?
{
uint32 m = millis();
if ((m-rxTime)>20)
inCnt = 0
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inBytes[inCnt++] = Serial2.read();
}
if (is_button_press==LOW && !digitalRead(buttonPin)) // button pressed?
{
is_button_press = HIGH;
digitalWrite(ledPin, LOW); // turn LED on
for (int i=0; i<NR_BYTES; i++)
Serial.write(inBytes[i]);
Serial.write('\n');
delay(debounce_delay);
}
if (is_button_press==HIGH && digitalRead(buttonPin)) // button released?
{
is_button_press = LOW;
digitalWrite(ledPin, HIGH); // turn LED off
}
}
You can adjust NR_BYTES dependent on how many bytes do you see in monitor.
Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 11:45 am
by stevestrong
And here a version which outputs data each time the scale send some data, independent on any button.
Code: Select all
#define NR_BYTES 10
uint8 inBytes[10], inCnt, dataIn;
uint32 rxTime;
void loop()
{
uint32 m = millis();
if ((m-rxTime)>20)
{
inCnt = 0
if (dataIn==1)
{
dataIn = 0;
for (int i=0; i<NR_BYTES; i++)
Serial.write(inBytes[i]);
Serial.write('\n');
}
}
if (Serial2.received()>=0) // serial data available?
{
dataIn = 1;
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inBytes[inCnt++] = Serial2.read();
}
}
Re: I need to return several values from incoming serial data...
Posted: Sat Nov 28, 2020 12:02 pm
by LORDHADES
stevestrong wrote: Sat Nov 28, 2020 11:24 am
Try this (beside your setup)
Code: Select all
#define NR_BYTES 10
uint8 inBytes[10], inCnt;
uint32 rxTime;
void loop()
{
if (Serial2.received()>=0) // serial data available?
{
uint32 m = millis();
if ((m-rxTime)>20)
inCnt = 0
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inBytes[inCnt++] = Serial2.read();
}
if (is_button_press==LOW && !digitalRead(buttonPin)) // button pressed?
{
is_button_press = HIGH;
digitalWrite(ledPin, LOW); // turn LED on
for (int i=0; i<NR_BYTES; i++)
Serial.write(inBytes[i]);
Serial.write('\n');
delay(debounce_delay);
}
if (is_button_press==HIGH && digitalRead(buttonPin)) // button released?
{
is_button_press = LOW;
digitalWrite(ledPin, HIGH); // turn LED off
}
}
You can adjust NR_BYTES dependent on how many bytes do you see in monitor.
Thank you so much for your continuous support and being kind to me, Sir....
I tweaked your code as below:
Code: Select all
#define buttonPin PA0
#define ledPin PC13
#define NR_BYTES 7
uint8 inByte[10], inCnt;
uint32 rxTime;
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()
{
if (Serial2.available()>=0)
{
uint32 m = millis();
if ((m-rxTime)>20)
inCnt = 0;
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inByte[inCnt++] = Serial2.read();
}
if (is_button_press==LOW && !digitalRead(buttonPin)) // button pressed?
{
is_button_press = HIGH;
digitalWrite(ledPin, LOW); // turn LED on
for (int i=0; i<NR_BYTES; i++)
Serial.write(inByte[i]);
Serial.write('\n');
delay(debounce_delay);
}
if (is_button_press==HIGH && digitalRead(buttonPin)) // button released?
{
is_button_press = LOW;
digitalWrite(ledPin, HIGH); // turn LED off
}
}
But sir, unfortunately, the output on serial monitor is:
17:29:02.245 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:03.232 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:03.842 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:04.123 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:04.498 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:06.311 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:07.249 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:08.280 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:09.030 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:10.108 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:11.701 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:12.639 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:13.389 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:14.139 -> ⸮⸮⸮⸮⸮⸮⸮
17:29:14.982 -> ⸮⸮⸮⸮⸮⸮⸮
Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 12:19 pm
by LORDHADES
stevestrong wrote: Sat Nov 28, 2020 11:45 am
And here a version which outputs data each time the scale send some data, independent on any button.
Code: Select all
#define NR_BYTES 10
uint8 inBytes[10], inCnt, dataIn;
uint32 rxTime;
void loop()
{
uint32 m = millis();
if ((m-rxTime)>20)
{
inCnt = 0
if (dataIn==1)
{
dataIn = 0;
for (int i=0; i<NR_BYTES; i++)
Serial.write(inBytes[i]);
Serial.write('\n');
}
}
if (Serial2.received()>=0) // serial data available?
{
dataIn = 1;
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inBytes[inCnt++] = Serial2.read();
}
}
Again, I tweaked your code as below:
Code: Select all
#define buttonPin PA0
#define ledPin PC13
#define NR_BYTES 7
uint8 inByte[10], inCnt, dataIn;
uint32 rxTime;
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()
{
if (Serial2.available()>=0)
{
uint32 m = millis();
if ((m-rxTime)>20)
{
inCnt = 0;
if (dataIn == 1)
{
dataIn = 0;
for (int i=0; i<NR_BYTES; i++)
Serial.write(inByte[i]);
Serial.write('\n');
}
}
if (Serial2.available()>0)
{
dataIn = 1;
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inByte[inCnt++] = Serial.read();
}
}
}
But, unfortunately, this renders no output...
The Serial monitor is empty...
Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 12:35 pm
by stevestrong
If the serial monitor is empty that means that no data is received fron the scale.
This is why you see "?" in monitor for the first version, too.
Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 12:37 pm
by LORDHADES
stevestrong wrote: Sat Nov 28, 2020 12:35 pm
If the serial monitor is empty that means that no data is received fron the scale.
This is why you see "?" in monitor for the first version, too.
Any solutions Sir?
Code: Select all
int inByte;
void setup()
{
Serial.begin(9600);
Serial2.begin(9600);
}
void loop()
{
if(Serial2.available()>0)
{
inByte = Serial2.read();
Serial.write(inByte);
}
}
The above code generates below output:
18:18:02.504 -> 000.092
18:18:02.598 -> 000.092
18:18:02.705 -> 000.092
18:18:02.799 -> 000.092
18:18:02.939 -> 000.092
18:18:03.021 -> 000.092
18:18:03.115 -> 000.092
18:18:03.256 -> 000.092
18:18:03.349 -> 000.092
18:18:03.457 -> 000.092
18:18:03.551 -> 000.092
18:18:03.644 -> 000.092
18:18:03.784 -> 000.092
18:18:03.874 -> 000.092
Then why the previous code didn't receive weight?
I'm dying..

Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 12:58 pm
by stevestrong
The misstake is that you tweaked my code incorrectly.
should be
Code: Select all
inBytes[inCnt++] = Serial2.read();
I added LED blink by data reception:
Code: Select all
#define NR_BYTES 10
uint8 inBytes[10], inCnt, dataIn;
uint32 rxTime;
void loop()
{
uint32 m = millis();
if ((m-rxTime)>20)
{
inCnt = 0
if (dataIn==1)
{
dataIn = 0;
for (int i=0; i<NR_BYTES; i++)
Serial.write(inBytes[i]);
Serial.write('\n');
}
digitalWrite(ledPin, HIGH); // turn LED off
}
if (Serial2.available()>0) // serial data available?
{
dataIn = 1;
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inBytes[inCnt++] = Serial2.read();
digitalWrite(ledPin, LOW); // turn LED on
}
}
Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 1:17 pm
by LORDHADES
stevestrong wrote: Sat Nov 28, 2020 12:58 pm
The misstake is that you tweaked my code incorrectly.
should be
Code: Select all
inBytes[inCnt++] = Serial2.read();
I added LED blink by data reception:
Code: Select all
#define NR_BYTES 10
uint8 inBytes[10], inCnt, dataIn;
uint32 rxTime;
void loop()
{
uint32 m = millis();
if ((m-rxTime)>20)
{
inCnt = 0
if (dataIn==1)
{
dataIn = 0;
for (int i=0; i<NR_BYTES; i++)
Serial.write(inBytes[i]);
Serial.write('\n');
}
digitalWrite(ledPin, HIGH); // turn LED off
}
if (Serial2.available()>0) // serial data available?
{
dataIn = 1;
rxTime = m;
if (inCnt>=NR_BYTES)
inCnt = 0;
inBytes[inCnt++] = Serial2.read();
digitalWrite(ledPin, LOW); // turn LED on
}
}
Yout last two codes return output as:
800.11
800.11
800.11
800.11
800.11
800.11
800.11
But the weighing scale reading is 0.118..

Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 1:18 pm
by LORDHADES
stevestrong wrote: Sat Nov 28, 2020 12:58 pm
The misstake is that you tweaked my code incorrectly.
should be
Code: Select all
inBytes[inCnt++] = Serial2.read();
I'm sorry for that, Sir.. childish mistake..

Re: I need to return a single value from incoming serial data...
Posted: Sat Nov 28, 2020 1:23 pm
by stevestrong
change
to
And you don't need to quote every time my posts, it makes the reading hard.
