FSM Lecture 34: Exercise-003 Helper function implementation

  • Post author:
  • Post category:Blog

 

Exercise-003 Helper function implementation

 

 

In this article, we learn how to implement helper functions.

First, display_message. You have to call the first lcd_set_cursor, then call lcd_print_string, as shown below.

static void display_message(String s, uint8_t c , uint8_t r){
    lcd_set_cursor(c,r);
    lcd_print_string(s);
}

display_message function

And for the beep, you have to call the tone function with the pin of the Buzzer; the duration is 25 milliseconds; I mean, the beep will come for 25 milliseconds, a short beep. 4000 is a frequency of square wave generated, which is fed to the Buzzer 4 KHz( as shown below).  

static void do_beep(void){
       tone(PIN_BUZZER, 4000, 25);
}

do_beep function

 

//////////////////////////helper functions//////////////////////////
static void display_time(uint32_t time){
  char buf[7];
  String time_msg;

  uint16_t m = time / 60;
  uint8_t s = time % 60;
  sprintf(buf,"%03d:%02d",m,s);

  time_msg = (String)buf;
  lcd_set_cursor(5,0);
  lcd_print_string(time_msg);
}

display_time function

In the display_time function, you receive the current time. And that current time is divided by 60 to get the minutes, and then modules (%) with 60 to get the seconds, and then we convert this into a string. The minute and second information. For that, we will use a sprintf function; and we will store that string in the buffer, a character buffer. “%03d:%02d” is the format specifier used. Format specifier will convert m,s numbers to a string format, and that string will be stored in the buffer.

Here I have used ‘%03d’ for the minute, because I need a minute with a width of 3 characters. This generates an integer of width three characters. The unused character will be filled with 0. And same as ‘%02d’. 

And that buffer, I convert it into a string format. The Arduino framework provides this, just typecast that.

And lcd_set_cursor. Before printing the time information, set the cursor first. The column is 5, and the row is 0. And then call lcd_print_string.  

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.