ASER Lesson 6: Arduino trip wire
Part of our ASER Home Security Course
Today's project is a challenge to build a trip wire. The challenge builds on the Arduino input and output skills we put into practice last week. All we have to do is slightly adapt last weeks PushButton project.
Installing the tripwire
The trip wire is nothing more than a long wire that acts just like a button. Remember that a button is just a part of the circuit that can either be connected or not.
So when we build our trip wire all we have to do is replace the button with the long loop of wire. The trip wire goes into the circuit exactly where the button went before.
Adapting the pushbutton code
The only major difference between our push button and the trip wire is that the trip wire should activate when the circuit isn't complete. So the logic that made the LEDs blink is backwards from what it was before.
To fix the code we just have to make one change so that if statement only activates when the circuit voltage is 0. If you change the code like this, the if statement will be off until the digitalRead on pin 2 says 0.
// digital pin 2 has a pushbutton attached to it. Give it a name: int pushButton = 2; int led1 = 13; int led2 = 12; // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // make the pushbutton's pin an input: pinMode(pushButton, INPUT); pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // read the input pin: int buttonState = digitalRead(pushButton); //if the buttonState is 1, make the LEDs blink 4 times if(buttonState==0){ digitalWrite(led1,HIGH); digitalWrite(led2,HIGH); delay(500); digitalWrite(led1,LOW); digitalWrite(led2,LOW); delay(500); digitalWrite(led1,HIGH); digitalWrite(led2,HIGH); delay(500); digitalWrite(led1,LOW); digitalWrite(led2,LOW); delay(500); digitalWrite(led1,HIGH); digitalWrite(led2,HIGH); delay(500); digitalWrite(led1,LOW); digitalWrite(led2,LOW); delay(500); digitalWrite(led1,HIGH); digitalWrite(led2,HIGH); delay(500); digitalWrite(led1,LOW); digitalWrite(led2,LOW); delay(500); } Serial.println(buttonState); delay(1); // delay in between reads for stability }
Now the trip wire should work how we want it.