Exemplo n.º 1
0
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);
    }
}
Exemplo n.º 2
0
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++;
	}
}