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 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.