Hi,I am a newbie in arduino programming world.now i am stuck at a little piece of sckech mean to burn to an attiny45 chip,my purpose is to use 2 sensors to trigger one same buzzer.one sound is when sensor received signal then alarm immediately.another sound is a little bit tricky.that's where my headache is coming from.it need to wait 3 minutes then trigger buzzer for 1 minute and repeat the cycle until i munally stwitch it off.Thanks.It's my homework.
int speakerPin = 0; //Attiny45 PB0 Buzzer pin+ (other pin on the GND)
int sensorPin = 1; //Attiny45 PB1 first sensor cable (other sensor on the VCC)
int sensorState = 0;
int speakerState = 0;
sensorState = digitalRead(sensorPin);
if (sensorState == HIGH)
First of all, when posting on the forum, always put your sketch in code tags. See the "#" button when you edit/post your topic. Edit your original post and put the tags in please.
You mention 2 sensors, but your sketch only gives the pin number for one. What happens with the second sensor?
How will you switch off the alarm? Do you mean you will cut the power to the Attiny?
For your 3 minute off/1 minute on repeating alarm, you can use delay() with longer values. To get this part of the sketch to repeat itself, see the while() command.
For longer delays the use of the delay() function is not a good idea as it paralyzes the processor while it is waiting. Nothing else can be done, such as reading a switch.
Look at the technique in the Blink Without Delay example sketch which shows how to use millis() for timing purposes. It will work fine for long delays even though it counts in milliseconds.
This is a simple homework project for the OP. No other tasks are to be performed as far as we know. We should keep the sketch as simple as possible. Until the OP says otherwise, I am assuming the alarm will be cancelled by switching off the power.