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 -> ⸮⸮⸮⸮⸮⸮⸮