Step 4Arduino Test
What's cool about this sensor is that the output is latched. For those of you who don't know what a latch is, it's one of the most basic memory elements in all computing. This particular latch will remember a digital High reading at the output of the sensor until an opposing magnetic field is sensed, at which point it will drop to digital Low and stay there until it is triggered again.
What this means for our code is that we only need to scan the inputs and turn the lights on or off based on what the sensor's memory holds. If you wanted to make this project really energy efficient, you could set up an interrupt to wait for a sensor trigger but since I'll be running this from a wall wart, it doesn't really matter that much to me weather the microcontroller uses 10milliamps or 50.
int sensor=2;
int val=0;
int ledPin=12;
void setup(){
pinMode(sensor,INPUT);
pinMode(ledPin,OUTPUT);
}
void loop(){
val=digitalRead(sensor);
if(val){
digitalWrite(ledPin,HIGH);
else
digitalWrite(ledPin,LOW);
}
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|













































