How do I make a function that will continue forever when it is called? Please help me

How do I make a function that will continue forever when it is called? Like analogWrite() and tone()? And has a stop function like noTone()? Please help me.

The reason that those functions appear to continue forever is that they use special parts of the hardware on the microcontroller chip that can run independently of your code. All the function does is set up the special hardware. The function then ends. The hardware continues to run independently.

So you can't write any function of your choice and expect it to run forever.

Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


Always describe exactly what you are attempting to do.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


The better you describe things the better we can help you.

Ok. Then how does the Tone library work?

The source code for anything that ever ends up being part of your sketch is available for your study, somewhere on your hard drive.

Also you can google generally and follow your nose to the source code for many things. github, for example, is where many choose to leave their source code.

Try

HTH

a7

Go back an re-read Pauls post. It EXPLAINS how tone() does that...

XY problem?

In words, describe exactly what you are attempting to do.


	for(;;)
	{
		/*Loop forever.*/
	}

Your comment is very true.

This is the code I made trying to make a function that will continue forever when it is called:

int ledState;             // ledState used to set the LED
long previousMillis;
unsigned long currentMillis;
int pin = 13;         //  pin 13 is used for the LED
void setup()
{


}
void loop()
{
   Blink(1000);
   //Blink forever?

   for(;;)
   {
   	/*Loop forever.*/
   }

}
void Blink(long time)
{

   if(currentMillis < 5)
   {
   	pinMode(pin, OUTPUT);
   	int ledState = LOW;
   	long unsigned previousMillis = 0;
   }

   unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > time)
   {
   	// save the last time you blinked the LED
   	previousMillis = currentMillis;
   	// if the LED is off turn it on and vice-versa:
   	ledState = !ledState;
   	// set the LED with the ledState of the variable:
   	digitalWrite(pin, ledState);
   }
}

A few more words are needed.

You really should. No one can read your mind, so giving us code that doesn't do something when we don't know what it doesn't do, doesn't help. :person_shrugging:
C

Ok.

Thank you guys!

The better you describe things the better we can help you.

@joshpeletier the way things appear to run forever, sometimes, is that they are functions meant to be called very frequently, those functions being written to decide if it is time to do something or not.

Your code, with adjustments, blinks forever, as your loop() will loop forever and constantly call Blink, which decides if it is time to turn on or off the LED.

I moved your kludge setup steps into setup() where they belong.

int ledState;             // ledState used to set the LED
long previousMillis;
unsigned long currentMillis;

int pin = 13;         //  pin 13 is used for the LED

void setup()
{
  pinMode(pin, OUTPUT);

  previousMillis = 0;
}

void loop()
{
   Blink(1000);
}

void Blink(long time)
{
   unsigned long currentMillis = millis();

   if(currentMillis - previousMillis > time) {
   	// save the last time you blinked the LED
   	previousMillis = currentMillis;
   	// if the LED is off turn it on and vice-versa:
   	ledState = !ledState;
   	// set the LED with the ledState of the variable:
   	digitalWrite(pin, ledState);
   }
}

HTH

a7

tone() uses one of the Arduino's built in timers. The timers are quite flexible and one of the things they can be configured to do is make a square wave with a desired frequency appear on an output pin. Once configured, the timer will make the signal independently of what the code is doing, until another function tells it to stop.

analogWrite() is similar, it also uses a timer but configures it slightly differently to tone(). The frequency is always the same but the duty cycle, which is the length of the HIGH part of the square wave compared to the LOW part can be configured. The timer continues to make this square wave independently of the code, until told to stop.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.