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;
}
}
}
}
}
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!
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);
}
LarryD
September 3, 2021, 8:06pm
6
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).
Tutorial on code blocking.
Attached is:
LEDsequenceDelay.ino
LEDsequenceMillis.ino
LEDsequenceDelayFlowChart.jpg
LEDsequenceMillisFlowChart.jpg
Two sketches are presented in this tutorial:
In the first sketch, the user wanted to be able to push a switch on pin 2 and have LED6 toggle on/off. However, switch control of LED6 is a hit and miss proposition as other LED control takes up most of the controller’s processing time.
This sketch uses the delay() function which stops normal code exe…
And
Newcomers to Arduino, soon discover the usefulness of using the Blink Without Delay (BWD) technique for timing events.
As most old timers realize, adding a Flag to this technique can prevent subtle and often unanticipated problems.
For those newcomers who do not know what and why a Flag should be used, hopefully this discussion will shed some light on the topic.
In the code segment below, we are looking for a time interval to expire.
When we want to start event timing with BWD, we make ledOn…
Study the State Machine method of writing sketches.
This sketch introduces the concepts of:
macros
structure
enumeration
BWD (Blink Without Delay timing) technique
timers
State Machine
The purpose of this example is to integrate the above concepts into a sketch to see how they can be used to simplify coding and make that coding more efficient.
An important feature is you can control your Timers.
Under program control, Timers can be running, restarted and disabled.
When it comes to the ‘State Machine’, you can see how the sketch’s current st…
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!
LarryD
September 3, 2021, 8:45pm
11
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!
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.
adrianoles:
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...
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.
system
Closed
January 2, 2022, 7:33am
17
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.