Liebe Community,
heute wollte ich mal wieder mit meinem Ultraschallsensor arbeiten, aber ich erhalte durch das von mir modifizierte Standardprogramm immer falsche Messungen:
C
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include "pigpio.h"
uint32_t startTick, endTick;
short bIsHigh;
short bIsLow;
void calcDistance() {
if ( startTick < 1 || endTick < 1 )
return;
uint32_t difTick = endTick - startTick;
int distance = difTick*343/2000;
printf("Distance: %d mm\n", distance);
}
void aFunction(int gpio, int level, uint32_t tick)
{
if( level && !bIsHigh )
{
startTick = gpioTick();
bIsHigh = 1;
}
else
{
if( bIsHigh )
{
endTick = gpioTick();
bIsLow = 1;
calcDistance();
}
}
}
main()
{
int diffTick;
if( gpioInitialise() > 0 )
{
gpioSetMode(23, PI_INPUT); /* set gpio23 as input */
gpioSetMode(24, PI_OUTPUT); /* set gpio18 as output */
gpioSetPullUpDown(23, PI_PUD_OFF); /* clear pull-ups/downs on gpio23 */
gpioSetPullUpDown(24, PI_PUD_OFF); /* clear pull-ups/downs on gpio24 */
bIsHigh = bIsLow = 0;
gpioSetAlertFunc(23, aFunction); /* call alert every time GPIO23 changes state */
for( ;; )
{
/* send 10 uSeconds burst to start measuring */
gpioWrite(24, 1); /* sets gpio24 high */
gpioDelay(10);
gpioWrite(24, 0); /* sets gpio24 high */
if( bIsLow && bIsHigh )
{
bIsLow = bIsHigh = 0;
}
else
{
gpioDelay(10000); /* allow CPU to do other jobs */
}
}
gpioTerminate(); /* cleanup library */
}
}
Alles anzeigen
Beispiel (unterschiedliche Distancen [von 10cm - 30cm | Durchgehende Entfernungsveränderung!]):
ZitatAlles anzeigenDistance: 168 mm
Distance: 332 mm
Distance: 166 mm
Distance: 333 mm
Distance: 166 mm
Distance: 165 mm
Distance: 173 mm
Distance: 166 mm
Distance: 329 mm
Distance: 166 mm
Distance: 334 mm
Distance: 166 mm
Distance: 330 mm
Distance: 166 mm
Distance: 334 mm
Distance: 166 mm
Distance: 166 mm
Distance: 166 mm
Distance: 167 mm
Distance: 166 mm
Distance: 165 mm
Wer kann mir helfen?
MfG