Need a continuous loop

I've done a basic ultrasonic alarm with the HC-SR04 and some LEDS, which lights up some LEDS upon detection, but I'm not sure how to do a continuous loop when the alarm go's off to carry on flashing LEDS?
I'm hoping to improve this little project to attach it to my letterbox to detect post coming through the door and set off a small alarm and flashing of some LEDS.
I only need a small part of the looping code..(nothing too complicated as this is my first project) ( just to jump from one part of the code to another and stay continuous there looping until I reset the alarm via switch or possibly I might opt for a bright LED to slowly flash until reset)

Thanks.. :smiley:

I've done a basic ultrasonic alarm with the HC-SR04 and some LEDS,

...but I'm not going to show you the code.

One way to do it:

Make your own alarm function, containing a infinite loop.

something like this:

if (letter_in_box==true){
  alarm();
}

void alarm(){
  while(1){
  //alarm stuff goes here
}
}

Don't think in terms of blinking the LED within a loop. Embrace the 'blink without delay' example and use this approach to blink the LED while leaving you free to deal with anything else that needs looking after in the meantime. Want to be able to control whether an LED should be blinking? Define a boolean variable that tells you whether it is currently blinking; set it true to start blinking and false to stop it. Write some other code (which will be very similar to 'blink without delay') to blink the LED when the variable is true and call that from loop().

AWOL:

I've done a basic ultrasonic alarm with the HC-SR04 and some LEDS,

...but I'm not going to show you the code.

Sorry I do apologize, here is the code so far, noticed there isn't much range from the sensor, works best hitting a large object rather than small ones :

#define trigPin 3
#define echoPin 2
int led1 = 11; //Set up 4 LEDS
int led2 = 10;
int led3 = 9;
int led4 = 8;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT); }

void loop() {
int monitor; digitalWrite(trigPin, HIGH); monitor = pulseIn(echoPin, HIGH); if (monitor > 1)
// 4 LEDS flashing
{digitalWrite(led4, HIGH);delay (10);digitalWrite(led1, HIGH);delay (10);digitalWrite(led4, LOW);delay (10);digitalWrite(led1, LOW);
digitalWrite(led3, HIGH);delay (10);digitalWrite(led2, HIGH);delay (10);digitalWrite(led3, LOW);delay (10);digitalWrite(led2, LOW);}
else {} digitalWrite(trigPin, LOW);} // Listen out for ping

PeterH:
Don't think in terms of blinking the LED within a loop. Embrace the 'blink without delay' example and use this approach to blink the LED while leaving you free to deal with anything else that needs looking after in the meantime. Want to be able to control whether an LED should be blinking? Define a boolean variable that tells you whether it is currently blinking; set it true to start blinking and false to stop it. Write some other code (which will be very similar to 'blink without delay') to blink the LED when the variable is true and call that from loop().

Hi PeterH
The flashing LEDS was just a beginning to indicate a capture of movement for that project, I will improve it on that one by removing the blinking LEDS and add in a alarm tone sound instead, this is only my first step into the world of Arduino, I'm just starting to get my head around most of the commands.
Thanks for the advice XD

Erni:
One way to do it:

Make your own alarm function, containing a infinite loop.

something like this:

if (letter_in_box==true){

alarm();
}

void alarm(){
  while(1){
  //alarm stuff goes here
}
}

Hi Erni,
Many thanks for the advice, the Letter Box project will be another idea down the line as I learn more about the Arduino commands, I was wondering can you buy a wireless add on (Transmitter/Receiver)?
Thinking as the post drops into the letterbox attached on my front door, I could possible use a sensitive touch switch to set off a signal for my Arduino to pick up and announce "Post has arrived", thinking of using a recordable greeting card sound chip so I can record my own voice for that.
Cheers Erni... :smiley: