Here I used the Reed switch to turn on and off a motor (cell phone vibrator) and a set of LEDs. The LEDs are being run on a variation the Knightrider Tutorial from the Arduino website.
// set pin numbers:
const int buttonPin = 7; // the number of the reedswitch pin
const int motorPin = 8; // the number oh the motor pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int timer = 100;
int timer2 = 1000;
void setup() {
// initialize the reedswitch pin as an input:
pinMode(buttonPin, INPUT);
// initialize the motor as as output
pinMode(motorPin, OUTPUT);
// initialize the LED pin as an output:
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
Serial.begin(9600);
}
void loop(){
// read the state of the reedswitch value:
buttonState = digitalRead(buttonPin);
// check if the reedswitch is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn motor on
digitalWrite(motorPin, HIGH);
delay(timer2);
digitalWrite(motorPin, LOW);
delay(timer);
// turn LED on:
digitalWrite(pin2, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
delay(timer);
digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);
digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
Serial.println("ON");
digitalWrite(pin2, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
delay(timer);
digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);
digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);
digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);
}
else {
// turn motor off:
digitalWrite(motorPin, LOW);
// turn LED off:
digitalWrite(pin2, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin5, LOW);
Serial.println("OFF");
}
}
Friday, January 22, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment