Hello
First of all, I`ve been spending way to many hours trying to figure this out, so I decided to open this ticket. I am new to microcontrollers in general and the Nordic Semiconductor infocenter is above my level understanding. I gave this post the tag of Health and Safety as I am loosing my mind over this minor issue.
I am using SEGGER Embedded Studio.
The controller I am using is a nRF52840 DK.
The ultrasonic sensor I am using is a HC-SR04.
As the ultrasonic sensor either returns a 0.0 value or a 1.0 value, I need to calculate the time from when the `echo` (input pin) is high (1.0) to when it is low (0.0). I have confirmed that the everything works using the nRF52840 as input (5V and trigger pin) and an Arduino as output (echo pin, terminal gave distance in cm), due to the Arduino being much simpler to implement. However, for my project I need to use the nRF due to the BLE thingy.
I have looked into many different tutorials using APP_TIMER, drv_clock and simple_timer. Everything I have looked into is either for the wrong controller, outdated, gives insufficient implementation info or gives errors in my compiler which I cant figure out how to fix.
All I need is a simple timer, my dream scenario looks like this: #include <super_simple_timer> and then time_start, time_stop and printf(time). However, I understand that more code is needed.
Is there a very simple example code for this, and please do tell what libraries to include, and if there are any changes in the sdk_config.h.
Thanks!
This is what my simple sensor-setup looks like:
This is kind of what my code is going to look like, just need to take a closer look after I hopefully get the timer to work:
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "nrf_log.h"
#include "boards.h"
#include "nrf_gpio.h"
#define trig 25 // Output pin
#define echo 11 // Input pin
int main() {
nrf_gpio_cfg_output(trig);
nrf_gpio_cfg_input(echo, NRF_GPIO_PIN_NOPULL);
while(true) {
// Clear the trig pin
nrf_gpio_pin_clear(trig);
nrf_delay_ms(2);
// Sets the trig pin for 10 ms
nrf_gpio_pin_set(trig);
nrf_delay_ms(10);
nrf_gpio_pin_clear(trig);
// Start the timer here
while(nrf_gpio_pin_read(echo) == 1) {
//Wait for the input pin to go back to zero
if (nrf_gpio_pin_read(echo) == 0) {
//Stop the timer
break
}
}
// Calculates the distance using the timer and the speed of sound.
float distance = timer_value *0.034 / 2;
printf("%f", distance);
}
}