04.07.2013 Views

Arduino Cookbook - Cdn.oreilly.com

Arduino Cookbook - Cdn.oreilly.com

Arduino Cookbook - Cdn.oreilly.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 1-9. Connections for a speaker with the LDR circuit<br />

You will need to increase the on/off rate on the pin to a frequency in the audio spectrum.<br />

This is achieved, as shown in the following code, by decreasing the min and max<br />

durations:<br />

const int outputPin = 9; // Speaker connected to digital pin 9<br />

const int sensorPin = 0; // connect sensor to analog input 0<br />

const int minDuration = 1; // 1ms on, 1ms off (500 Hz)<br />

const int maxDuration = 10; // 10ms on, 10ms off (50 hz)<br />

void setup()<br />

{<br />

pinMode(outputPin, OUTPUT); // enable output on the led pin<br />

}<br />

void loop()<br />

{<br />

int sensorReading = analogRead(sensorPin); // read the analog input<br />

int rate = map(sensorReading, 200,800,minDuration, maxDuration);<br />

rate = constrain(rate, minDuration,maxDuration); // constrain the value<br />

digitalWrite(outputPin, HIGH); // set the LED on<br />

delay(rate); // wait duration dependent on light level<br />

digitalWrite(outputPin, LOW); // set the LED off<br />

delay(rate);<br />

}<br />

20 | Chapter 1: Getting Started

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!