O'Reilly logo

Arduino for Kids by Vijay Varada, Rishi Gaurav Bhatnagar, Priya Kuber

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The safe

You now have all the know-how to build our safety box!

Let's start with the code:

//Initializing a variable to store the value of the button state 
int lidState = 0; 
int keyState = 0; 
int lidPin = 2; 
int keyPin = 3; 
int alarmPin = 9; 
 
// the setup function runs once when you press reset or power the board 
 
 
void setup() { 
  // initialize digital I/O pins 
  pinMode(lidPin, INPUT); 
  pinMode(keyPin, INPUT); 
  pinMode(alarmPin, OUTPUT); 
} 
 
// the loop function runs over and over again forever 
void loop() { 
 
  lidState = digitalRead(lidPin); 
  if ( lidState == LOW ) 
  { 
    keyState = digitalRead(keyPin); 
    if ( keyState == HIGH ) 
      digitalWrite(alarmPin, LOW); 
    else 
      digitalWrite(alarmPin, HIGH); 
  } 
  else 
    digitalWrite(alarmPin, LOW); 
} 

We start by storing the pin numbers ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required