{
newState newState = = eState.TooDark;eState.TooDark;
} }
Next in the chain is to see if it was too high:
if (cdsVoltage >
if (cdsVoltage >
highPotVoltage) highPotVoltage)
{ {
newState newState = = eState.TooBright;eState.TooBright;
} }
And then we let the code know to use a dierent method, to determine what to do with the state of the light:
await await
CheckForStateChange(newState);
CheckForStateChange(newState);
The CheckForStateChange code has been mostly completed in this example, but you’ll need to add the following under ‘use another method to wrap the speech synthesis functionality’ to do just that:
await TextToSpeech(whatToSay);
await TextToSpeech(whatToSay);
Now we can add the part that lets the Pi talk to us! Under async, where
it says ‘insert code’, insert the following code which rst converts text input into a stream, and then plays the audio:
SpeechSynthesisStream SpeechSynthesisStream synthesisStream;
synthesisStream;
synthesisStream = await synthesisStream = await synthesizer.SynthesizeTextToStr synthesizer.SynthesizeTextToStr eamAsync(textToSpeak);
eamAsync(textToSpeak);
media.AutoPlay = true;
media.AutoPlay = true;
media.
Coding the ADC Coding the ADC Now we’re done with the MainPage code, it’s time to code up the ADC. Open upMCP3008.csMCP3008.cs from the project le.
We need to start by creating an initialising method to set up communication with the SPI bus the ADC chip runs on. Just after the line ‘Debug.WriteLin e(“MCP3008::Initialize”);’ add the following, starting with the conguration of the SPI bus and telling it the ADC’s clock speed:
try
settings.ClockFrequency settings.ClockFrequency == 3600000;
3600000;
settings.Mode settings.Mode = = SpiMode.SpiMode.
Mode0;
Mode0;
This line will return all SPI devices on the system:
string string aqs aqs = = SpiDevice.SpiDevice.
GetDeviceSelector();
GetDeviceSelector();
This line nds the SPI bus controller from the previous line:
PRACTICAL USES PRACTICAL USES
How could such a project be used in the real world? Well, making sure some owers are kept in proper sunlight may be useful for certain
Featur Featur
botanists. You could also place it near your TV to let you know when it’s time to shut the curtains to avoid massive screen glare!
31
And the rest of the code creates an SPI device using all the information we just grabbed:
mcp3008 mcp3008 = = await await SpiDevice.SpiDevice.
FromIdAsync(dis[0].Id, settings);
FromIdAsync(dis[0].Id, settings);
if (mcp3008 == null) if (mcp3008 == null) {{
Debug.WriteLine(
Debug.WriteLine(
"SPI "SPI Controller Controller {0} {0} isis currently in use by another currently in use by another application. Please ensure that application. Please ensure that no other applications are using no other applications are using SPI.",
catch (Exception e) catch (Exception e) {
{
Debug.WriteLine("Exception:
Debug.WriteLine("Exception:
" + e.Message + "\n" +
Now we have the information for the chip, we’re able to add code so we can read it:
public int ReadADC(byte public int ReadADC(byte whichChannel) byte[] { 0x01, command, 0x00 };
byte[] { 0x01, command, 0x00 };
byte[] byte[] readBuf readBuf = = new new byte[]byte[] ((readBuf[1] & 0x03) << 8);
((readBuf[1] & 0x03) << 8);
int int s2 = s2 = sample & sample & 0x3FF;0x3FF;
Debug.Assert(sample Debug.Assert(sample == == s2);s2);
return return sample;sample;
} }
Remember when we were turning the input into a voltage so it was easier to read? We need to add a helper method here to work with that:
public oat ADCToVoltage(int public oat ADCToVoltage(int adc)
adc) { {
return return (oat)adc (oat)adc ** ReferenceVoltage / (oat)Max;
ReferenceVoltage / (oat)Max;
} }
It’s game time It’s game time
Your project is ready! Run the code with the setup in a normally lit area. The output window will show you the voltages read from the ADC: the lower-boundary resistor, the higher-boundary resistor, and the light sensor. It should look something like this:
MainPage::timerCallback MainPage::timerCallback Read values 211, 324, 179 Read values 211, 324, 179 Voltages 1.031281, 1.583578, Voltages 1.031281, 1.583578, 0.8748778
0.8748778
Turn the lower-boundary potentiometer, watching the value of the rst number change. Adjust it so it’s just a touch lower than the light sensor reading. Do the same with the other resistor, turning it to be just above the light sensor. This is now our ‘just right’ zone of light.
Now, when you put your hand over the light sensor, it will print in the output window ‘I need a light’;
it will also say it to you if you have speakers or a headphone connected.
Experiment with uncovering and shining a light on it to see just how sensitive it is.
This tutorial has been adapted from Bright This tutorial has been adapted from Bright or Not? by the Windows IoT team or Not? by the Windows IoT team consist-ing of David Shoemaker, Anthony Ngu, ing of David Shoemaker, Anthony Ngu, and Aparajita Dutta, from the and Aparajita Dutta, from the MicrosoftMicrosoft Projects site:
Projects site:magpi.cc/2abUvx0magpi.cc/2abUvx0
L
The light sensor, or The light sensor, or photoresistor, changes photoresistor, changes resistance depending resistance depending on the amount of light on the amount of light hitting it. Like the hitting it. Like the variablevariable
resistor, this changes the resistor, this changes the voltage at the output voltage at the output There's a whole host There's a whole host of other tutorials on of other tutorials on GitHub where this GitHub where this project lies; give them project lies; give them a look if you want to try
raspberrypi.org/magpi
34 August 2016
e’ve seen all sorts of Pi-powered robots in our time, but the Discoverer is the rst with a built-in metal detector. Mounted on a PVC pipe arm in front of the four-wheeled robot, the detector emits a beep whenever it passes over a metallic object. Prolic robot maker Ingmar Stapel, from Munich, came up with the idea after watching a TV show about people trying to nd gold with a sophisticated metal detector. “I was immediately inspired to build my own aordable robot-car with a metal detector in order to discover some treasure in my garden,” he tells us.