Good afternoon!
I create a human movement alarm.
I am signaling atmega328. I receive a signal from the sensor and if there is a signal, then using the "tone" I generate a sound.
I am using a motion sensor. I want that if one movement was noticed for a short time, then turn on a short melody, and if the movement continues longer than a certain time, then play a long melody.
I wrote the code to play a short melody. How can you track that the input from the sensor is at a high logical level for a long time and play a long melody in this case?
unsigned long timing; // Variable to store the starting point
int T = 15; // Signal playback time!
int S0 = PIND0; // With Doppler Probe
int S1 = PIND1; // Food
int S2 = PIND2; // Audio
void setup ()
{
// Port control via registers:
DDRD = B00000110; // Set the output pin (the first and second pins of register D as output pins, the rest (0.3-7) are set as input pins)
// 76543210
pinMode (PIND0, INPUT);
pinMode (PIND1, OUTPUT); // Power for the audio amplifier
pinMode (PIND2, OUTPUT); // Audio
}
void loop ()
{
if (digitalRead (PIND0))
{
sendSignal ();
}
if (digitalRead (PIND0) == 0)
{
digitalWrite (PIND1, LOW); // Food
digitalWrite (PIND2, LOW); // Sound
}
}
void sendSignal ()
{
digitalWrite (PIND1, HIGH); // Food
for (int n = 0; n <4; n ++)
{
for (int i = 4800; i <5800; i ++)
{
tone (PIND2, i, 10); // tone (port, frequency in Hz, duration in ms) 600 - 6 sec.
}
}
}
And yes, if my code can be optimized, tell me how!