My first mini project: home security

My first mini project with Arduino. It’s a simple home security device. When a human is detected in the room by the “white eye” it sets off a LED and a small piezo “speaker”. Please give me your suggestions and observations! Thanks.

void setup()  { 
  pinMode(7, INPUT);  pinMode(9, OUTPUT);
} 

int val = 0;

void loop()  { 
  val = digitalRead(7);
  if (val == HIGH) {
   for (int i=1; i <= 60; i++) { beep(i); }
  }
}

void beep(unsigned char delayms){
  analogWrite(9, 20); delay(delayms);
  analogWrite(9, 0);  delay(delayms);
}

Looks fine, although the formatting makes it a little difficult to read. See what the IDE's autoformat tool does for you. Does it work as desired?

Thanks, yes.

Looks good. Feels good getting the first one working doesn’t it?

The only thing I would say is maybe get into the habit of making things a bit more descriptive.

It’s easy enough to understand now, but when you start doing more complicated code it would make debugging easier to do something like:

   const int sensorPin = 7;
   const int buzzerPin = 9;
   motionSensorReading = digitalRead(sensorPin );

That way when you go to access the pins, it’s immediately clear which one you are using. As your projects get more complex, having descriptive variable names can save you countless headaches in debugging.

Thanks, that's a good idea.

quite interesting one... i jus hav a doubt can we use the same lik an alarm ringing, till some one switches it off?

Yes, quite easy to do that. Just need to modify the code.