<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>There&amp;#39;s something I don&amp;#39;t understand about LED timer.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/67349/there-s-something-i-don-t-understand-about-led-timer</link><description>Hello, 
 I have a question because there is a problem with writing code using timer. 
 I wrote the following code to avoid using the LED timer when I first ran it. 
 However, All_Stop() will not function properly the first time the code below is executed</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 21 Oct 2020 08:57:23 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/67349/there-s-something-i-don-t-understand-about-led-timer" /><item><title>RE: There's something I don't understand about LED timer.</title><link>https://devzone.nordicsemi.com/thread/276112?ContentTypeID=1</link><pubDate>Wed, 21 Oct 2020 08:57:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e011612-9868-40d6-ab52-c17748fa456e</guid><dc:creator>Karl Ylvisaker</dc:creator><description>[quote user="schosdas"]I think it worked out well.[/quote]
&lt;p&gt;Are you by this saying that the issue is resolved, or are you still experiencing an issue? If so, which issue are you still seeing?&lt;/p&gt;
[quote user="schosdas"]Changed to operate according to Bluetooth connection status.[/quote]
&lt;p&gt;I suppose here that your&amp;nbsp;&lt;em&gt;pairing_state&lt;/em&gt; variable indicates whether the device is in a connection or not. While the name might not be a perfect fit, the functionality and implementation is fine, and should work along the lines of what you described earlier.&lt;br /&gt;Most examples already does this, using the&amp;nbsp;&lt;em&gt;m_connection_handle&amp;nbsp;&lt;/em&gt;variable to indicate if it is in a connection or not.&lt;br /&gt;&lt;br /&gt;From the code you have included you are using some dots, . . . , to indicate that there is some code, but it is not relevant to your question. Please instead share the entire function, rather than a cutout, since this makes it harder to track where the code snippet comes from and where it is called from.&lt;br /&gt;&lt;br /&gt;In general, the code you shared in your previous comment looks much better and more correct than the code you posted earlier - well done!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: There's something I don't understand about LED timer.</title><link>https://devzone.nordicsemi.com/thread/276055?ContentTypeID=1</link><pubDate>Wed, 21 Oct 2020 02:41:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5ce29da8-ab4a-4ebf-beec-e9af14d17f60</guid><dc:creator>schosdas</dc:creator><description>&lt;div class="edit_box___1KtZ3 active___3VPGL" id="txtTarget"&gt;&lt;span&gt;I think it worked out well.&lt;/span&gt;&lt;/div&gt;
&lt;div class="edit_box___1KtZ3 active___3VPGL"&gt;&lt;span&gt;Changed to operate according to Bluetooth connection status.&lt;/span&gt;&lt;/div&gt;
&lt;div class="edit_box___1KtZ3 active___3VPGL"&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;bool pairing_state = false;

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) 
{
    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED: //when connected pairing
        {
            pairing_state = true;
            .
            .
        }
        case BLE_GAP_EVT_DISCONNECTED: //when disconnected
        {
            pairing_state = false;
            .
            .
        }
    }
}
.
.
.
    else //no charging
    {
      //charging_state = 0;
      
      if(charging_state == 0)
      {
        nrf_gpio_pin_clear(BAT_LED_1); //LED Off
        nrf_gpio_pin_clear(BAT_LED_2);
        nrf_gpio_pin_clear(BAT_LED_3);

        if(pairing_state == true) //In bluetooth pairing 
        {
          CenterLED_timers_start(); //LED1,2,3 toggle repeate timer
        }
&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: There's something I don't understand about LED timer.</title><link>https://devzone.nordicsemi.com/thread/276051?ContentTypeID=1</link><pubDate>Wed, 21 Oct 2020 01:52:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c4eb9a8e-536a-4d1f-9854-3cb604018b67</guid><dc:creator>schosdas</dc:creator><description>&lt;p&gt;I&amp;#39;m modified it.&lt;/p&gt;
&lt;p&gt;After further testing, I connected the battery charge connection when there was no Bluetooth connection. CenterLED_timers_start is called when battery charging removed, and the LED turns on.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: There's something I don't understand about LED timer.</title><link>https://devzone.nordicsemi.com/thread/276048?ContentTypeID=1</link><pubDate>Wed, 21 Oct 2020 01:25:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a7ce7adc-b2ba-4276-bb16-3daa0c6246e7</guid><dc:creator>schosdas</dc:creator><description>&lt;p&gt;Hello, Karl.&lt;br /&gt;Thank you for your answer.&lt;/p&gt;
&lt;p&gt;I use one internal LED on the board and three external LEDs.&lt;br /&gt;External LEDs have several operating routines.&lt;/p&gt;
&lt;p&gt;1. When Bluetooth is connected, CenterLED_timers_start() is called.&lt;/p&gt;
&lt;p&gt;(CenterLED_timers_stop() is called before connection).&lt;/p&gt;
&lt;p&gt;2. When the battery charging connector is connected, the three LEDs stop working and print the battery value to the LED.&lt;/p&gt;
&lt;p&gt;3. If the battery charging connector is not connected, the CenterLED Timer starts or stops as shown in 1.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void gpio_init(void)
{
  nrf_gpio_cfg_output(APP_LED_1); //board_led1
  nrf_gpio_pin_set(APP_LED_1);

  nrf_gpio_cfg_output(APP_LED_2); //board_led2
  nrf_gpio_pin_set(APP_LED_2);

  nrf_gpio_cfg_output(APP_LED_3); //board_led3
  nrf_gpio_pin_set(APP_LED_3);

  nrf_gpio_cfg_output(APP_LED_4); //board_led4
  nrf_gpio_pin_set(APP_LED_4);

  nrf_gpio_cfg_output(BAT_LED_1); 
  nrf_gpio_pin_set(BAT_LED_1);

  nrf_gpio_cfg_output(BAT_LED_2); 
  nrf_gpio_pin_set(BAT_LED_2);

  nrf_gpio_cfg_output(BAT_LED_3); 
  nrf_gpio_pin_set(BAT_LED_3);

  nrf_gpio_cfg_input(APP_BTN_1,NRF_GPIO_PIN_PULLUP);
  nrf_gpio_cfg_input(bat_state, NRF_GPIO_PIN_PULLDOWN);
}


static void CenterLED_timer_handler(void * p_context)
{
  nrf_gpio_pin_toggle(BAT_LED_1);
  nrf_gpio_pin_toggle(BAT_LED_2);
  nrf_gpio_pin_toggle(BAT_LED_3);
}


static void PairLED_timer_handler(void * p_context)
{
  nrf_gpio_pin_toggle(APP_LED_1); //toggle led
  //printf(&amp;quot;Pairing LED....\n&amp;quot;);
}


/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module. This creates and starts application timers.
 */
static void timers_init(void)
{
    ret_code_t err_code;

    // Initialize timer module.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    //Create Center LED timers
    err_code = app_timer_create(&amp;amp;m_CenterLED_timerid,
                                APP_TIMER_MODE_REPEATED,
                                CenterLED_timer_handler);
    APP_ERROR_CHECK(err_code);


   //Create Pairing LED timers
    err_code = app_timer_create(&amp;amp;m_PairLED_timerid,
                                APP_TIMER_MODE_REPEATED,
                                PairLED_timer_handler);
    APP_ERROR_CHECK(err_code);
}


static void CenterLED_timers_start() //Bat_LED1,2,3 toggle start
{
  ret_code_t err_code;

  err_code = app_timer_start(m_CenterLED_timerid, APP_TIMER_TICKS(1000), NULL); //Toggle 1sec
  APP_ERROR_CHECK(err_code);
  printf(&amp;quot;Center Led Start\n&amp;quot;);
}


static void PairLED_timers_start() //APP_LED1 toggle start (advertising)
{
  ret_code_t err_code;

  err_code = app_timer_start(m_PairLED_timerid, APP_TIMER_TICKS(1000), NULL); //Toggle 1sec
  APP_ERROR_CHECK(err_code);
  printf(&amp;quot;Scanning Start\n&amp;quot;);
}


static void CenterLED_timers_stop() //Stop Center Timer
{
    ret_code_t err_code;
    
    err_code = app_timer_stop(m_CenterLED_timerid); 
    APP_ERROR_CHECK(err_code);
    printf(&amp;quot;Center Led Stop\n&amp;quot;);
}

static void PairLED_timers_stop() //Stop pairing LED Timer
{
    ret_code_t err_code;
    
    err_code = app_timer_stop(m_PairLED_timerid); 
    APP_ERROR_CHECK(err_code);
}


void All_Start()
{
  uint32_t err_code;

  CenterLED_timers_start();
  //err_code = app_button_enable();
  //APP_ERROR_CHECK(err_code);
}

void All_Stop()
{
  uint32_t err_code;

  //err_code = app_button_disable();
  //APP_ERROR_CHECK(err_code);
  CenterLED_timers_stop();
  nrf_gpio_pin_clear(BAT_LED_1); //LED Off
  nrf_gpio_pin_clear(BAT_LED_2);
  nrf_gpio_pin_clear(BAT_LED_3);
}  
.
.
.
.
//battery level range
#define BAT_ADC_100 465
#define BAT_ADC_75  350 
#define BAT_ADC_50  230 
#define BAT_ADC_0   0

int charging_state = 0; //Used to prevent repeated output of CenterLED_timers_start.

void turn_on_batter_level_led(int16_t bat_level) //battery LED
{ 
   //int charging_state = 0;
   ret_code_t err_code;

   int charge_state = nrf_gpio_pin_read(bat_state); //read battery charge state
   printf(&amp;quot;%d\n&amp;quot;, charge_state); //charging : 1, no charging : 0 (TP4056 Charge IC)

   if(charge_state == 1) //charging
   {
      charging_state = 0; //Used to prevent repeated output of CenterLED_timers_start.

      //stop center LED, start Battery LED
      CenterLED_timers_stop();
      
      printf(&amp;quot;battery : %u \n&amp;quot;, bat_level); //%u : unsigned decimal number

      if(bat_level &amp;gt;= BAT_ADC_75) { // over 75%
        printf(&amp;quot;over 75persent \n&amp;quot;); //led1,2,3 on

        nrf_gpio_pin_toggle(BAT_LED_1);
        nrf_gpio_pin_toggle(BAT_LED_2);
        nrf_gpio_pin_toggle(BAT_LED_3);
      }

      else if (BAT_ADC_75 &amp;gt; bat_level &amp;gt;= BAT_ADC_50) { //over 50%
        printf(&amp;quot;over 50persent \n&amp;quot;); //led1,2 on

        nrf_gpio_pin_toggle(BAT_LED_1);
        nrf_gpio_pin_toggle(BAT_LED_2);

        nrf_gpio_pin_clear(BAT_LED_3); //bat_led3 off

        char bat_array2[] = &amp;quot;over 50&amp;quot;;
        uint16_t bat_length2 = sizeof(bat_array2);
      }
  
      else if (bat_level &amp;lt; BAT_ADC_50) { //under 50%
       printf(&amp;quot;under 50persent \n&amp;quot;); //led1 on

       nrf_gpio_pin_toggle(BAT_LED_1);

       nrf_gpio_pin_clear(BAT_LED_2); //bat_led2 off
       nrf_gpio_pin_clear(BAT_LED_3);  //bat_led3 off
      }
   }
   
/*When first executed, Bluetooth is before connection and the battery charging connector is also before connection. 
Therefore, the CenterLED must be turned off. */
    else //no charging
    {
      //charging_state = 0;
      
      if(charging_state == 0)
      {
        nrf_gpio_pin_clear(BAT_LED_1); //LED Off
        nrf_gpio_pin_clear(BAT_LED_2);
        nrf_gpio_pin_clear(BAT_LED_3);

        CenterLED_timers_start(); //LED1,2,3 toggle repeate timer

        charging_state = 1; //Used to prevent repeated output of CenterLED_timers_start.
        //nrf_delay_ms(100);
        printf(&amp;quot;charging_state : %d\n&amp;quot;, charging_state); //check charging_state

        if(charging_state == 1) //No LED activity before bluetooth connection
        {
          printf(&amp;quot;All_Stop\n&amp;quot;);
          //CenterLED_timers_stop();
          All_Stop(); //center timer stop &amp;amp; LED Off
        }
      }
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The reason why I used &amp;#39;if(charging_state == 0)&amp;#39; is that &amp;#39;CenterLED_timers_start()&amp;#39; is continuously called when the battery charging connector is not connected. If it continues to be called, the LED behavior becomes strange, so I only printed it once.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1603244269042v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;The output on the serial terminal indicates that All_Stop is called.&lt;br /&gt;But the LEDs are working without stopping.&lt;br /&gt;(No Bluetooth connection when upload or reset Therefore, the LED should be turned off.)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Please feel free to let me know if I lack explanation or need anything else.&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: There's something I don't understand about LED timer.</title><link>https://devzone.nordicsemi.com/thread/275618?ContentTypeID=1</link><pubDate>Mon, 19 Oct 2020 12:28:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2e2078cb-b029-4eba-9faa-5efff689a1ff</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello,&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote user=""]I wrote the following code to avoid using the LED timer when I first ran it.[/quote]
&lt;p&gt;Please share the actual code you used. Where is the timer configuration and initialization? Which function is used as the timers expiration callback?&lt;br /&gt;Where is your GPIO configuration and initialization?&lt;br /&gt;Which function contains the if and else code you have pasted at the bottom?&lt;/p&gt;
[quote user=""]&lt;span&gt;However,&amp;nbsp;&lt;/span&gt;All_Stop() will not function properly the first time the code below is executed (that is, when the upload or when the pressed reset button).[/quote]
&lt;p&gt;Please elaborate on this, what are you seeing and how is it different from what you would have expected?&lt;br /&gt;Does your&amp;nbsp;&lt;em&gt;All_stop&amp;nbsp;&lt;/em&gt;function not work without power-cycling or resetting your device first?&lt;/p&gt;
[quote user=""]This issue only occurs the first time I run it, and it will work normally after that (after a single change in the value of the charging_state).[/quote]
&lt;p&gt;Your last conditional is&amp;nbsp;&lt;strong&gt;&lt;em&gt;inside&lt;/em&gt;&lt;/strong&gt; the if( .. == 0) conditional.&lt;br /&gt;Is this intentional? With the current design, the last conditional will always be true, when the function is entered with (charging == 0).&lt;br /&gt;Please review this part of the code, and tell me if this is the intended behavior. If not, could you have meant to place the last part as a separate conditional check?&lt;br /&gt;As it stands right now the conditional is redundant, since charging is set to 1 right before it is checked.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>