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

Parents
  • 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

Reply
  • 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

Children
No Data
Related