Why won't my custom functions be called?

As I beginner, this is my first time trying custom functions and I was confused as to why RGBloop and RGBoff would not be called/executed...
Code:

const int BUTTON = A1;
const int RLED = 10;
const int GLED = 9;
const int BLED = 8;
const unsigned long debouncePeriod = 20;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long debounceStartMillis;
byte previousButtonState;
byte currentButtonState;
int count = 0;
bool debouncing = false;


void setup()
{
	Serial.begin(115200);
	pinMode(BUTTON, INPUT_PULLUP);
	pinMode(RLED, OUTPUT);
	pinMode(GLED, OUTPUT);
	pinMode(BLED, OUTPUT);
	startMillis = millis();

}

int RGBloop()
{
	Serial.println("Starting loop.");
	digitalWrite(RLED, LOW);	//Red
	digitalWrite(BLED, HIGH);
	digitalWrite(GLED, HIGH);
	delay(1000);
	digitalWrite(RLED, HIGH);	//Green
	digitalWrite(BLED, HIGH);
	digitalWrite(GLED, LOW);
	delay(1000);
	digitalWrite(RLED, HIGH);	//blue
	digitalWrite(BLED, LOW);
	digitalWrite(GLED, HIGH);
	delay(1000);
	digitalWrite(RLED, LOW);	 //orange
	digitalWrite(BLED, HIGH);
	digitalWrite(GLED, LOW);
	delay(1000);
	digitalWrite(RLED, HIGH);	//teal
	digitalWrite(BLED, LOW);
	digitalWrite(GLED, LOW);
	delay(1000);
	digitalWrite(RLED, LOW);	//purple
	digitalWrite(BLED, LOW);
	digitalWrite(GLED, LOW);
	delay(1000);
	digitalWrite(RLED, LOW);	//white
	digitalWrite(BLED, LOW);
	digitalWrite(GLED, LOW);
	delay(1000);

}

int RGBoff()
{
	Serial.println("RGB off.");
	digitalWrite(RLED, HIGH);
	digitalWrite(BLED, HIGH);
	digitalWrite(GLED, HIGH);
}


void loop()
{
	currentMillis = millis();
	previousButtonState = currentButtonState;
	currentButtonState = digitalRead(BUTTON);
	if (currentButtonState != previousButtonState)
	{
		debouncing = true;
		debounceStartMillis = currentMillis;
	}
	if (currentMillis - debounceStartMillis >= debouncePeriod)
	{
		if(debouncing == true)
		{
			if (currentButtonState == LOW)
			{
				debouncing = false;
				count++;
				Serial.println(count);
				if (count % 2 == 0)
				{
					Serial.println("Turning off.");
					RGBoff;
				}
				else
				{
					Serial.println("running loop function.");
					RGBloop;
				}
			}
		}
	}
}

RGBoff( );

1 Like

A function call has to have the parenthesis whether or not there are arguments for the function.

1 Like

Thank you, it works well now! :pray: :slightly_smiling_face:

Thank you!

Is there a way I can restart this function after it reaches white... Or is it best to rewrite it into a for or while loop?

int RGBloop()
{
		digitalWrite(RLED, HIGH);	//Red
		digitalWrite(BLED, LOW);
		digitalWrite(GLED, LOW);
		delay(1000);
		digitalWrite(RLED, LOW);	//Green
		digitalWrite(BLED, LOW);
		digitalWrite(GLED, HIGH);
		delay(1000);
		digitalWrite(RLED, LOW);	//blue
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, LOW);
		delay(1000);
		digitalWrite(RLED, HIGH);	 //orange
		digitalWrite(BLED, LOW);
		digitalWrite(GLED, HIGH);
		delay(1000);
		digitalWrite(RLED, LOW);	//teal
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, HIGH);
		delay(1000);
		digitalWrite(RLED, HIGH);	//purple
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, LOW);
		delay(1000);
		digitalWrite(RLED, HIGH);	//white
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, HIGH);
		delay(1000);
}

A for( ) loop would work nicely.


BTW
Suggest you never use delay( ) as nothing else runs while in the delay time.


Study BWD (blink without delay).

And


Study the State Machine method of writing sketches.

1 Like

Ah, thank you! I'll definitely study them!

Also, I was hoping you could help me turn the RGB colors into a for() loop...

This is what I thought of doing, but it doesn't look like it will work...
(I wanted to increment the modes in the for() function, but I'm not sure how.)

void ledMode(int mode)
{
	if (mode == 1) {
		digitalWrite(RLED, HIGH);	//Red
		digitalWrite(BLED, LOW);
		digitalWrite(GLED, LOW);
		delay(1000);
	}
	else if (mode == 2) {
		digitalWrite(RLED, LOW);	//Green
		digitalWrite(BLED, LOW);
		digitalWrite(GLED, HIGH);
		delay(1000);
	}
	else if (mode == 3) {
		digitalWrite(RLED, LOW);	//blue
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, LOW);
		delay(1000);
	}
	else if (mode == 1) {
		digitalWrite(RLED, HIGH);	 //orange
		digitalWrite(BLED, LOW);
		digitalWrite(GLED, HIGH);
		delay(1000);
	}
	else if (mode == 4) {
		digitalWrite(RLED, LOW);	//teal
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, HIGH);
		delay(1000);
	}
	else if (mode == 5) {
		digitalWrite(RLED, HIGH);	//purple
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, LOW);
		delay(1000);
	}
	else if (mode == 6) {
		digitalWrite(RLED, HIGH);	//white
		digitalWrite(BLED, HIGH);
		digitalWrite(GLED, HIGH);
		delay(1000);
	}
}

int RGBloop()
{
	for(ledMode())
}

Hello
You have made the test condition twice

else if (mode == 1) {

You may take a look to the switch/case instruction.

if (mode == 1) {
		digitalWrite(RLED, HIGH);	//Red
		digitalWrite(BLED, LOW);
		digitalWrite(GLED, LOW);
		delay(1000);
    mode = 2;  // use a different mode next time through loop()
	}

Ah, nice catch, thank you!

Maybe some version of this:

//*******************************************
void ledMode(byte mode)
{
  switch (mode)
  {
    case 1:
      digitalWrite(RLED, HIGH); //Red
      digitalWrite(BLED, LOW);
      digitalWrite(GLED, LOW);
      delay(1000);

      break;

    case 2:
      digitalWrite(RLED, LOW);  //Green
      digitalWrite(BLED, LOW);
      digitalWrite(GLED, HIGH);
      delay(1000);

      break;

    case 3:
      digitalWrite(RLED, LOW);  //blue
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, LOW);
      delay(1000);

      break;

    case 4:
      digitalWrite(RLED, HIGH);  //orange
      digitalWrite(BLED, LOW);
      digitalWrite(GLED, HIGH);
      delay(1000);

      break;

    case 5:
      digitalWrite(RLED, LOW);  //teal
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, HIGH);
      delay(1000);

    case 6:
      digitalWrite(RLED, HIGH); //purple
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, LOW);
      delay(1000);

      break;

    case 7:
      digitalWrite(RLED, HIGH); //white
      digitalWrite(BLED, HIGH);
      digitalWrite(GLED, HIGH);
      delay(1000);

      break;
  }

} //END of      ledMode(int mode)

//*******************************************
void RGBloop()
{
  for (byte x = 1; x < 8; x++)
  {
    ledMode(x);
  }

} //END of      RGBloop()
1 Like

This would indeed work, however, I was hoping to put the modes in a for() loop if possible, so that once it reaches the last mode it would restart to mode 1... Assuming that a for() is the most efficient solution that is...

Yes, thank you very much! This would work great! You reminded me how I could do this ledMode(x);. I also see why using switch case in this scenario is much easier than if() and else(). Your help is much appreciated! :pray: :slightly_smiling_face:

Hello
Try this sketch and check it out for your application.

//BLOCK COMMENT
// https://forum.arduino.cc/t/why-wont-my-custom-functions-be-called/902008
#define ProjectName "Why won’t my custom functions be called?"
// CONSTANT DEFINITION
// you may need to change these constants to your hardware and needs
constexpr byte Output_[] {2, 3, 4}; // red,green,blue
// VARIABLE DECLARATION AND DEFINTION
constexpr bool  RGBcolours[][sizeof(Output_)] {
  {false,false,false}, // red,green,blue
  {false,false,true},
  {false,true,false},
  {false,true,true},
  {true,false,false},
  {true,false,true},
  {true,true,false},
  {true,true,true},
};
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  for (auto Output : Output_) pinMode(Output, OUTPUT);
 }
void loop () {
  int n=0;
  static int colour;
  for (auto rgbLed:Output_) digitalWrite(rgbLed,RGBcolours[colour][n++]);
  colour++;
  colour=colour % (sizeof(RGBcolours)/sizeof(RGBcolours[0]));
  delay (1000); // bad delay for test purposes only 
}

Have a nice day and enjoy coding.

If it's a 'for' loop, what is the exit condition? I think you don't want one. So when you get to the last mode, what is difficult about just going back to mode 1? Don't make life difficult for yourself...

The code working very well.