robert@372: /* robert@372: ____ _____ _ _ robert@372: | __ )| ____| | / \ robert@372: | _ \| _| | | / _ \ robert@372: | |_) | |___| |___ / ___ \ robert@372: |____/|_____|_____/_/ \_\.io robert@372: robert@372: */ robert@372: robert@372: /** robert@372: \example 3_scope_analog robert@372: robert@372: Connecting potentiometers robert@372: ------------------------- robert@372: robert@372: This example reads from analogue inputs 0 and 1 via `analogReadFrame()` and robert@372: generates a sine wave with amplitude and frequency determined by their values. robert@372: It's best to connect a 10K potentiometer to each of these analog inputs. Far robert@372: left and far right pins of the pot go to 3.3V and GND, the middle should be robert@372: connected to the analog in pins. robert@372: robert@372: The sine wave is then plotted on the oscilloscope. Click the Open Scope button to robert@372: view the results. As you turn the potentiometers you will see the amplitude and robert@372: frequency of the sine wave change. robert@372: robert@372: This project also shows as example of `map()` which allows you to re-scale a number robert@372: from one range to another. Note that `map()` does not constrain your variable robert@372: within the upper and lower limits. If you want to do this use the `constrain()` robert@372: function. robert@372: */ l@273: giuliomoro@301: #include l@273: #include l@273: #include l@273: l@273: Scope scope; l@273: l@273: float gInverseSampleRate; l@273: float gPhase; l@273: giuliomoro@301: bool setup(BelaContext *context, void *userData) l@273: { l@273: l@273: // setup the scope with 3 channels at the audio sample rate l@273: scope.setup(3, context->audioSampleRate); l@273: l@273: gInverseSampleRate = 1.0 / context->audioSampleRate; l@273: gPhase = 0.0; l@273: l@273: return true; l@273: } l@273: giuliomoro@301: void render(BelaContext *context, void *userData) l@273: { l@273: l@273: for(unsigned int n = 0; n < context->audioFrames; n++) { l@273: l@273: // read analogIn channels 0 and 1 andrewm@308: float in1 = analogRead(context, n, 0); andrewm@308: float in2 = analogRead(context, n, 1); l@273: l@273: // map in1 to amplitude and in2 to frequency l@273: float amplitude = in1 * 0.8f; l@273: float frequency = map(in2, 0, 1, 100, 1000); l@273: l@273: // generate a sine wave with the amplitude and frequency l@273: float out = amplitude * sinf(gPhase); l@273: gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; l@273: if(gPhase > 2.0 * M_PI) l@273: gPhase -= 2.0 * M_PI; l@273: l@273: // log the sine wave and sensor values on the scope l@273: scope.log(out, in1, in2); l@273: l@273: // pass the sine wave to the audio outputs l@273: for(unsigned int channel = 0; channel < context->audioChannels; channel++) l@273: context->audioOut[n * context->audioChannels + channel] = out; l@273: l@273: } l@273: } l@273: giuliomoro@301: void cleanup(BelaContext *context, void *userData) l@273: { l@273: l@273: }