uint32_t Read_Distance() { init_ultrasonic(); //let trigger pin sends signal GPIO_ResetBits(GPIOA, GPIO_Pin_7); delay(2); GPIO_SetBits(GPIOA, GPIO_Pin_7); delay(10); GPIO_ResetBits(GPIOA, GPIO_Pin_7); // Give some time for response uint32_t timeout = 1000000; while(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6)) { if(timeout -- == 0x00) { return -1; } } //if echo pin receive signal, start counting time. uint32_t wait_time = 0; while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1) { wait_time ++; delay(1); } return wait_time/58; }
int main (void) { VCOM_Init(); xprintf_output(&VCOM_putchar); // tell miniprintf to print to VCOM init_ultrasonic(); while (1) { uint32_t distance_mm = measure_distance_mm(); xprintf("Measured distance: %d mm\r\n", distance_mm); delayMS(1500); // Wait 1.5sec until next measurement } }
/******************************************************************************** * main(): Calls initalizing functions and contains the infinite loop for * distance detection. *******************************************************************************/ int main(int argc, char **argv) { uint64_t total_time = 0; double distance = 0; double threshold = 10.0; // This is in cm /* Ways to display LEDs */ char eight_rows[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; char six_rows[8] = {0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E}; char four_rows[8] = {0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C}; char two_rows[8] = {0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18}; char x[8] = {0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81}; char no_lights[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; /* Handle a signal */ signal(SIGINT, sigint_handler); printf("Signal handler set up.\n"); /* Check for command arguments */ check_args(argc, argv, &threshold); printf("Arguments have been checked.\n"); /* Setup LED Matrix pins */ if(init_gpio() != 0) { exit(1); } printf("GPIO initialized.\n"); /* Setup SPI */ if(init_spi() != 0) { exit(1); } printf("SPI initialized.\n"); /* Setup ultrasonic sensor */ if(init_ultrasonic() != 0) { exit(1); } printf("Ultrasonic initialized.\n"); /* Begin a never ending loop that will read from the ulstrasonic sensor * and display lights on the LED matrix until CTRL-C is hit */ while(1){ if (read(ultrasonic_fd, &total_time, sizeof(total_time)) < 0) { perror("ERROR: Could not read from ultrasonic device.\n"); } /* Pretty sure the ultrasonic data sheet lied because using the formulas didn't * get me anywhere. So, after some trial and error, I determined using a denominator * in th range of 22750 will give me a decent distance number. */ distance = (double)total_time / 22750; printf("Distance is %f\n", distance); /* Distance scenarios */ if (distance > (4 * threshold)) { // Object is too far away to register display_pattern(eight_rows); } else if (distance <= (4 * threshold) && distance > (3 * threshold)) { // Object is far but still registered display_pattern(six_rows); } else if (distance <= (3 * threshold) && distance > (2 * threshold)) { // Object is halfway display_pattern(four_rows); } else if (distance <= (2 * threshold) && distance > threshold) { // Object is getting too close display_pattern(two_rows); } else { // Object has crashed =( display_pattern(x); usleep(500000); display_pattern(no_lights); usleep(500000); } } }