This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Increment Counter on Button Clicked

Hello everyone,
i need to increment a counter each time my button on the nrf534DK is pressed 
i'm using sdk v 1.7.1 . 
is there a simple way to do it or a function which supports this 
do you have any example that can guide me through, so that i can implement and increment my counter each time the button is pressed .


i already have my button example running perfectly 
i just need to implement a counter that increments by 1 each time the button is pressed
i did some researches and i saw that RTC counter need to be used 

do you have an example on that or can you guide me through to get it done 
the image below is showing how my button is pressed 
1-button pressed ( printk("***************Button pressed at %" PRIu32 "\n", k_cycle_get_32());  k_cycle_get_32->Current hardware clock up-counter (in cycles)  ) 
2-get bme data
3-get Mpu data


knowing the details can you help me please implement the counter
feel free to provide me with any example that can help me 
i really appreciate your help in advance 

very thankful 

kindly,

Rihab

  • Hello Community
    i'm currently trying to increment a counter every time the button is pressed 
    the counter is considered like the number of cycle 
    well i added some modification to my code to do this function 

         int on_click = 0;
            while(gpio_pin_get_dt(&button) != true ) {
    	    on_click++;
            printk("Number of cycle : %d\n", on_click);
            k_msleep(SLEEP_TIME_MS);
            }

    this function is part of my project 
    and whole project role is :
    0-button pressed
    1-display BME280 values
    2-display MPU6050 values
    and now i'm trying to add the number of cycle which means technically how many times the button was pressed 
    when i try to view the result on my debug Terminal
    it only displays the number of cycle and it doesn't display the values of the other 2 sensors 
    and then 5 sec later it stops and it displays the disassembly window on the right just like below

    can anyone help me fix this please 

    please if you know what is causing this tell me as soon as you can 
    thank you in advance 

    Kindly,
    Rihab

  • someone can help me please 
    is anybody here 

  • Hey Rihab,

    You are very close, but you need to study the programming concept of scope and how it affects variables. See https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm.

    If you want to access the counter variable outside the button_pressed function you need to declare it as a global variable.

    or, if you only want to access the counter variable inside the function, you need to declare it as a static variable in order to keep its state in memory each time the button_pressed function is called. See https://www.tutorialspoint.com/static-variables-in-c

  • thank you for you recommendation 
    I made it through with your help 
    here's my output from the result 
    thank you very much 
    very grateful

    ***************Button pressed at Number of cycle : 43
    +++++++++++++++++Get BME280 data
    temp: 21.210000; press: 100.595343; humidity: 39.749023
    -----------------Get MPU6050 data
    [0:02:54.717]:95158.9 Cel
      accel 210000.069375 94622.000021 210000.000100 m/s/s
      gyro  595343.000039 0.000000 210000.069375 rad/s
    ***************Button pressed at Number of cycle : 44
    +++++++++++++++++Get BME280 data
    temp: 21.210000; press: 100.595343; humidity: 39.749023
    -----------------Get MPU6050 data
    [0:02:55.158]:95158.9 Cel
      accel 210000.069375 94622.000021 210000.000100 m/s/s
      gyro  595343.000039 0.000000 210000.069375 rad/s
    ***************Button pressed at Number of cycle : 45
    +++++++++++++++++Get BME280 data
    temp: 21.240000; press: 100.596410; humidity: 40.249023
    -----------------Get MPU6050 data
    [0:02:55.365]:95158.9 Cel
      accel 240000.069375 94622.000021 240000.000100 m/s/s
      gyro  596410.000040 0.000000 240000.069375 rad/s

Related