replace the delays

This is only part of my 12,000 character sketch !! How can I replace all the delays with Millis or some other thing. I have tried many Millis configuration but it just does not work.

This is for a piezo to beep in that sequence…then move on down the code to the next thing. Might seem very simple for you guys but not for me :frowning: I have a few more hundred lines like these for various alarms

this part goes: beep beep - beep beep - beep beep

lcd.print("<< CHECK FUEL PSI >>");
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(50);
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(500);
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(50);
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(500);
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(50);
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(500);
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(50);
digitalWrite(03, HIGH);
delay(50);
digitalWrite(03, LOW);
delay(500);

…continues down to the next thing

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

In the IDE there is an Example called, “Blink without Delay”, that will help.
Also a simple “for” loop, or “if” statement and counter variable to count the seven beeps.

Tom… :slight_smile:

Oops sorry. This only my 2nd post ever and last time my code was a link to Pastbin

Are you going to fix this ?

I have tried BWD and a bunch of other things. Nothing seem to work for me :frowning:

I have several beep routines and lcd.prints that require delays of 50, 250, 500, 750 and 1000ms.

A L W A Y S
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

You can attach a sketch to a post also.

In words tell us what is supposed to happen.

“I have tried BWD and a bunch of other things.”
Simplify one section and show us what you tried.

It sounds like you CAN use millis(), but to shorten and simplify your sketch, you'll need to understand arrays and possibly structs.
Using these three things - I suspect your code will shrink by more than 60%, use less SRAM, and operate co-operatively with any other functions you might add later... if you follow the non-blocking rules!
... and no, we aren't going to write it for you.
Take baby steps on pieces of your code as you put it all together, post that here if you need help on each concept, and we'll push you forward.

rotorway133:
This is only part of my 12,000 character sketch !!

Sounds like you have fallen into the trap of putting the whole program together and then tinkering with individual pieces to get it right. That's backwards. Build up the program from little pieces you've thoroughly tested and know to work.

IMHO, for now, focus on just making one correctly functioning beep-on beep-off cycle using millis, triggered with a switch input. Yes, it's uphill but it's *got * to be mastered to be able to do useful real-world things. Once that is done you might make it a function to which you can pass timing information. The function could be inside a for() loop which gets the timing info from an array, as suggested by @lastchancename.

What is your roadblock with millis()?

but it's got to be mastered to be able to do useful real-world things.

... and RELIABLE real-world things.

You write a function like this

void beep( int pin, int times, int speed){
for( int i =0; i < times; i ++){
  digitalWrite (pin, LOW);
   delay(speed);
 digitalWrite (pin, HIGH);
   delay( speed );
   }
}

Then when ever you want to do a bleep like your first code you write

beep(3,8,50);

The numbers you pass to this function define the pin, the number of beeps and the delay between each.

Tip if you find yourself writing the near same thing over and over and over then you are doing it wrong and you need to learn to do it better.

rotorway133:
This is only part of my 12,000 character sketch !! How can I replace all the delays with Millis or some other thing. I have tried many Millis configuration but it just does not work.

You will need a major re-write of that sort of program to change to using millis()

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

Also have a look at Planning and Implementing a Program

...R

OK, Try this for starters
This is the simplest way - using delay()
(NOT TESTED, but it should compile as-is)

unsigned int pointer;	// global - so the pointer can be remembered - ready for playNextSequence()
const int outPin 3;

// each row (delimited by -1) is a sequence of ON,OFF delay pairs
int cadence[] = {	50,50,50,500,50,50,50,500,50,50,50,500,50,50,50,500, -1,	// this sequence starts at [0]
			50,50,50,500,50,50,50,500,50,50,50,500,50,50,50,500, -1,	// this sequence starts at [17]
			50,50,50,500,50,50,50,500,50,50,50,500,50,50,50,500, -1		// etc...
								};
// ---------------------------									
// e.g. playSequenceAt(0) will play the first line
void playSequenceAt(unsigned int startPosition) {
	pointer = startPosition;
	while (cadence[pointer] >=0) {	// play sequence until pointer reaches a negative delay ((-1))
		digitalWrite(outPin, HIGH);
		delay(cadence[pointer++]);	// on time
		digitalWrite(outPin, LOW);
		delay(cadence[pointer++]);	// off time
	}
	pointer++;		// bump pointer ready for next cadence value
}
// ---------------------------									
// e.g. playNextSequence() will play the line following the last one played
void playNextSequence() {
	while (cadence[pointer] >=0) {	// play sequence until pointer reaches a negative delay ((-1))
		digitalWrite(outPin, HIGH);
		delay(cadence[pointer++]);	// on time
		digitalWrite(outPin, LOW);
		delay(cadence[pointer++]);	// off time
	}
}
// ---------------------------

The same can be done using millis() to step through the cadence array.