Arduino Projects Experiments Part5
Arduino Projects Experiments Part5
*/
// RGB pins wired to the Arduino microcontroller.
// give them names:
int redled = 9;
int grnled = 10;
int bluled = 11;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pins as outputs:
pinMode(redled, OUTPUT);
pinMode(grnled, OUTPUT);
84
86
89
Figure 10-3. The Magic Light Bulb running through the tricolor pattern
Example 10-1. The Magic Light Bulb sketch
/*
Magic Light Bulb
Flashes the red, green, and blue LEDs of an RGB LED three times by
briefly pressing a mini pushbutton switch.
25 Feb 2013
Don Wilcher
*/
// Pushbutton switch and RGB pins wired to the Arduino microcontroller.
// give them names:
int redled = 9;
int grnled = 10;
int bluled = 11;
int Pbutton = 8;
// initialize counter variable
int n =0;
// monitor pushbutton switch status:
int Pbuttonstatus = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pins as outputs:
pinMode(redled, OUTPUT);
pinMode(grnled, OUTPUT);
pinMode(bluled, OUTPUT);
// initialize the digital pin as an input:
90
The Arduino will turn on the piezo buzzer. Now youre ready to unlock the metal
mysteries hiding in your house!
Example 11-1. The Metal Checker sketch
/*
Metal Checker
Turns on and off a piezo buzzer at pin 7 when metal is placed across
the sense wires of the metal sensor circuit attached to pin 6.
The
*
*
*
circuit:
Piezo buzzer attached from pin 7 to ground
Metal Checker sensor attached to pin 7
1K fixed resistor attached from pin 6 to ground
March 2013
by Don Wilcher
*/
// set pin numbers:
const int MSensePin = 6;
const int PBuzzerPin = 7;
void setup() {
// initialize the LED pin as an output:
pinMode(PBuzzerPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(MSensePin, INPUT);
}
void loop(){
// read the state of the metal sense value:
MetalStatus = digitalRead(MSensePin);
// check if metal is present
// if it is, the MetalStatus is HIGH:
if (MetalStatus == HIGH) {
// turn piezo buzzer on:
digitalWrite(PBuzzerPin, HIGH);
}
else {
// turn MetalStatus off:
digitalWrite(PBuzzerPin, LOW);
}
}
99