Page 4 of 4

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

Posted: Sat Nov 28, 2020 1:35 pm
by LORDHADES
Long live @stevestrong

I changed

Code: Select all

#define NR_BYTES 7
to

Code: Select all

#define NR_BYTES 10
and

Code: Select all

 if ((m-rxTime)>50)
to

Code: Select all

 if ((m-rxTime)>80)
I've got output like :

000.120

000.120

000.120

000.120

000.120

But the output starts printing immediately after uploading, and doesn't respond to my button press, and again, gets printed more than once.. :o :o :o

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

Posted: Sat Nov 28, 2020 1:46 pm
by stevestrong
Yes, because that was the version which utputs data as soon as received.
This is the version with outputs data when button pressed, and also the number of received bytes:

Code: Select all

//-----------------------------------------------------------------------------
#define NR_BYTES 10
uint8 inBytes[NR_BYTES], inCnt, rxBytes;
uint32 rxTime;
//-----------------------------------------------------------------------------
void loop() 
{
  if (Serial2.available()>0) // serial data available?
  {
    uint32 m = millis();
    if ((m-rxTime)>80)
    {
      rxBytes = inCnt;
      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
    Serial.print("Received bytes: "); Serial.println(rxBytes);
    for (int i=0; i<rxBytes; i++)
      Serial.write(inBytes[i]);

    delay(debounce_delay);
  }
  
  if (is_button_press==HIGH && digitalRead(buttonPin)) // button released?
  {
    is_button_press = LOW;
    digitalWrite(ledPin, HIGH); // turn LED off
  }
}

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

Posted: Sat Nov 28, 2020 2:23 pm
by LORDHADES
Long Long Live Mr.@stevestrong

Thanks a lot sir... It's finally working... :D :D :D
I was working out on that issue.. But you solved the issue quickly... :)
Because of you only, I am completing my first ever Arduino-STM32 project.
Thanks a lot for being kind, gentle to this novice kid.
You're really a great great great person, Sir. Again, loads of thanks to you... :P :P :P

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

Posted: Mon Nov 30, 2020 12:22 pm
by LORDHADES
I thank all, for helping a lot in my problem, especially, Mr.@stevestrong .

Now I'm trying to write the weight data to STM32's emulated EEPROM. Before that, I was testing a test program influenced by inbuilt EEPROM example to write and read data. But while compiling, I get some error. Link to that post : "viewtopic.php?f=63&t=806"

Thanks,
Hades

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

Posted: Mon Nov 30, 2020 2:52 pm
by stevestrong
Please open a new post for this issue, and delete it from here.
Also, insert all your code into code tags.

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

Posted: Thu Dec 10, 2020 1:13 pm
by LORDHADES
stevestrong wrote: Sat Nov 28, 2020 1:46 pm

Code: Select all

//-----------------------------------------------------------------------------
#define NR_BYTES 10
uint8 inBytes[NR_BYTES], inCnt, rxBytes;
uint32 rxTime;
//-----------------------------------------------------------------------------
void loop() 
{
  if (Serial2.available()>0) // serial data available?
  {
    uint32 m = millis();
    if ((m-rxTime)>80)
    {
      rxBytes = inCnt;
      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
    Serial.print("Received bytes: "); Serial.println(rxBytes);
    for (int i=0; i<rxBytes; i++)
      Serial.write(inBytes[i]);

    delay(debounce_delay);
  }
  
  if (is_button_press==HIGH && digitalRead(buttonPin)) // button released?
  {
    is_button_press = LOW;
    digitalWrite(ledPin, HIGH); // turn LED off
  }
}
Sir, could you please integrate this code with this:

Code: Select all

#include <RTClock.h>
#define buttonPin PA10

RTClock rtclock (RTCSEL_LSE); // initialise
int timezone = 8;      // change to your timezone
time_t tt, tt1;
tm_t mtt;

uint8_t dateread[11];
bool dispflag = true;

const char * weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
const char * months[] = {"Dummy", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

uint8_t str2month(const char * d)
{
    uint8_t i = 13;
    while ( (--i) && strcmp(months[i], d)!=0 );
    return i;
}

const char * delim = " :";
char s[128]; // for sprintf

void ParseBuildTimestamp(tm_t & mt)
{
    // Timestamp format: "Dec  8 2017, 22:57:54"
    sprintf(s, "Time: %s, %s\n", __DATE__, __TIME__);
    //Serial.print(s);
    
    char * token = strtok(s, delim); // get first token
    // walk through tokens
    
    while( token != NULL ) {
        uint8_t m = str2month((const char*)token);
        if ( m>0 ) {
            mt.month = m;
            //Serial.print(" month: "); Serial.println(mt.month);
            token = strtok(NULL, delim); // get next token
            mt.day = atoi(token);
            //Serial.print(" day: "); Serial.println(mt.day);
            token = strtok(NULL, delim); // get next token
            mt.year = atoi(token) - 1970;
            //Serial.print(" year: "); Serial.println(mt.year);
            token = strtok(NULL, delim); // get next token
            mt.hour = atoi(token);
            //Serial.print(" hour: "); Serial.println(mt.hour);
            token = strtok(NULL, delim); // get next token
            mt.minute = atoi(token);
            //Serial.print(" minute: "); Serial.println(mt.minute);
            token = strtok(NULL, delim); // get next token
            mt.second = atoi(token);
            //Serial.print(" second: "); Serial.println(mt.second);
        }
        token = strtok(NULL, delim);
    }
}

void SecondCount ()
{
  tt++;
}

void setup()
{
  Serial.begin(115200);
  ParseBuildTimestamp(mtt);  // get the Unix epoch Time counted from 00:00:00 1 Jan 1970
  tt = rtclock.makeTime(mtt) + 25; // additional seconds to compensate build and upload delay
  rtclock.setTime(tt);
  tt1 = tt;
  rtclock.attachSecondsInterrupt(SecondCount);// Call SecondCount
}

void loop()
{
  if ( Serial.available()>10 ) 
  {
    for (uint8_t i = 0; i<11; i++) 
    {
     dateread[i] = Serial.read();
    }
    
    Serial.flush();
    tt = atol((char*)dateread);
    rtclock.setTime(rtclock.TimeZone(tt, timezone)); //adjust to your local date
  }
 
 if (tt1 != tt && dispflag == true && is_button_press == LOW && !digitalRead(buttonPin))
 {
    is_button_press = HIGH;
    tt1 = tt;
    rtclock.breakTime(rtclock.now(), mtt);
    sprintf(s, "Time: %s %u %u, %s, %02u:%02u:%02u\n",
    months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second);
    Serial.print(s);
  }
}
To make it display weight data along with RTC date on button press.... Please sir.....

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

Posted: Tue Dec 15, 2020 12:42 am
by GonzoG
LORDHADES wrote: Thu Dec 10, 2020 1:13 pm Sir, could you please integrate this code with this:

Code: Select all

#include <RTClock.h>
#define buttonPin PA10

RTClock rtclock (RTCSEL_LSE); // initialise
int timezone = 8;      // change to your timezone
time_t tt, tt1;
tm_t mtt;

uint8_t dateread[11];
bool dispflag = true;

const char * weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
const char * months[] = {"Dummy", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

uint8_t str2month(const char * d)
{
    uint8_t i = 13;
    while ( (--i) && strcmp(months[i], d)!=0 );
    return i;
}

const char * delim = " :";
char s[128]; // for sprintf

void ParseBuildTimestamp(tm_t & mt)
{
    // Timestamp format: "Dec  8 2017, 22:57:54"
    sprintf(s, "Time: %s, %s\n", __DATE__, __TIME__);
    //Serial.print(s);
    
    char * token = strtok(s, delim); // get first token
    // walk through tokens
    
    while( token != NULL ) {
        uint8_t m = str2month((const char*)token);
        if ( m>0 ) {
            mt.month = m;
            //Serial.print(" month: "); Serial.println(mt.month);
            token = strtok(NULL, delim); // get next token
            mt.day = atoi(token);
            //Serial.print(" day: "); Serial.println(mt.day);
            token = strtok(NULL, delim); // get next token
            mt.year = atoi(token) - 1970;
            //Serial.print(" year: "); Serial.println(mt.year);
            token = strtok(NULL, delim); // get next token
            mt.hour = atoi(token);
            //Serial.print(" hour: "); Serial.println(mt.hour);
            token = strtok(NULL, delim); // get next token
            mt.minute = atoi(token);
            //Serial.print(" minute: "); Serial.println(mt.minute);
            token = strtok(NULL, delim); // get next token
            mt.second = atoi(token);
            //Serial.print(" second: "); Serial.println(mt.second);
        }
        token = strtok(NULL, delim);
    }
}

void SecondCount ()
{
  tt++;
}

void setup()
{
  Serial.begin(115200);
  ParseBuildTimestamp(mtt);  // get the Unix epoch Time counted from 00:00:00 1 Jan 1970
  tt = rtclock.makeTime(mtt) + 25; // additional seconds to compensate build and upload delay
  rtclock.setTime(tt);
  tt1 = tt;
  rtclock.attachSecondsInterrupt(SecondCount);// Call SecondCount
}

void loop()
{
  if ( Serial.available()>10 ) 
  {
    for (uint8_t i = 0; i<11; i++) 
    {
     dateread[i] = Serial.read();
    }
    
    Serial.flush();
    tt = atol((char*)dateread);
    rtclock.setTime(rtclock.TimeZone(tt, timezone)); //adjust to your local date
  }
 
 if (tt1 != tt && dispflag == true && is_button_press == LOW && !digitalRead(buttonPin))
 {
    is_button_press = HIGH;
    tt1 = tt;
    rtclock.breakTime(rtclock.now(), mtt);
    sprintf(s, "Time: %s %u %u, %s, %02u:%02u:%02u\n",
    months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second);
    Serial.print(s);
  }
}
To make it display weight data along with RTC date on button press.... Please sir.....
It won't work with F103 as RTC does not have calendar and it does not store time in unix format.
It only stores time when it was set and counts seconds since then.

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

Posted: Sat Dec 19, 2020 6:45 am
by LORDHADES
It won't work with F103 as RTC does not have calendar and it does not store time in unix format.
It only stores time when it was set and counts seconds since then.
Thanks for your reply man!!!