Analogue IO.
The Board has two potentiometers (Analogue inputs). These can be read using the ADC (Analogue-Digital-Convertor).
The repository s3.1 illustrates how these can be read.
The output from the library is scaled into a 0…1 range.
We can recover the actual value read using a knowledge of the hardware that generated the signal.
0x0000
…0xFFFF
.One form of Analogue output is Pulse-Width-Modulation (PWM). This is often used for motor speed control or as a dimmer mode for controlling LEDs.
Repository s3.2 shows an example of PWM used to control an LED’s brightness.
The example uses a simple ramp
for( duty=0 ; duty<1.0 ; duty+=0.01 ) {
red.write(duty);
wait_ms(100);
}
A more complex algorithm uses:
for( duty=0 ; duty<4.6 ; duty+=0.23 ) {
float brightness = exp(duty)/100;
red.write(brightness);
wait_ms(100);
}
What do you observe as different about the two effects
[ hint ]?
(you may want to play about with the delays and values)
Play about with different functions to control the brightness
Only the Red and Green LED on the application shield can be controlled using a PWM output.
We can use the PWM output in another way for another form of analogue output. Here we are going to use a fixed 50% duty cycle, equal times on and off. We will vary the period (total time of on and off).
Example s3.3 scans through a frequency range.
for (f=20.0; f<20.0e3; f+=100) {
speaker.period(1.0/f);
...
}
What are the lowest and highest frequencies you can hear?
Look at the table of piano note frequencies.