void render(BelaContext *context, void *userData) { for(unsigned int n = 0; n<context->digitalFrames; n++){ gTriggerCount++; if(gTriggerCount == gTriggerInterval){ gTriggerCount = 0; analogWriteOnce(context, n / 2, gTriggerAnalogOutChannel, HIGH); //write the status to the trig pin } else { analogWriteOnce(context, n / 2, gTriggerAnalogOutChannel, LOW); //write the status to the trig pin } int pulseLength = pulseIn.hasPulsed(context, n); // will return the pulse duration(in samples) if a pulse just ended float duration = 1e6 * pulseLength / context->digitalSampleRate; // pulse duration in microseconds static float distance = 0; if(pulseLength >= gMinPulseLength){ static int count = 0; // rescaling according to the datasheet distance = duration / gRescale; if(count > 5000){ // we do not want to print the value every time we read it rt_printf("pulseLength: %d, distance: %fcm\n", pulseLength, distance); count -= 5000; } ++count; } // Logging to the scope the pulse inputs (gEchoDigitalInPin) and the distance scope.log(digitalRead(context, n, gEchoDigitalInPin), distance/100); } }
void render(BelaContext *context, void *userData) { static bool pulseOut = 0; static int count = 0; for(unsigned int n = 0; n < context->digitalFrames; n++){ // detect if a pulse just ended int duration = pulseIn.hasPulsed(context, n); if(duration > 0){ rt_printf("duration = %d\n", duration); } // generate a rectangular waveform as a test signal. // Connect gDigitalOutPin to gPulseInPin // to verify that the detected pulse length is gPulseLength if(count == gPulseLength ){ pulseOut = false; } if(count == (gPulseLength + gSamplesBetweenPulses)){ pulseOut = true; count = 0; } digitalWrite(context, n, gDigitalOutPin, pulseOut); count++; } }
bool setup(BelaContext *context, void *userData) { // Set the mode of digital pins pinMode(context, 0, gEchoDigitalInPin, INPUT); // receiving from ECHO PIN pulseIn.init(context, gEchoDigitalInPin, HIGH); //detect HIGH pulses on this pin scope.setup(2, 44100); if(context->analogInChannels != 8){ fprintf(stderr, "This project has to be run with 8 analog channels\n"); return false; } return true; }
bool setup(BelaContext *context, void *userData) { pinMode(context, 0, gDigitalOutPin, OUTPUT); pulseIn.init(context, gPulseInPin, 1); //third parameter is direction return true; }