Currently I'm working with a function where I need to start a loop after it's been sent to it. As of right now I can't compile as I get the error:
C:\Users\X\AppData\Local\Temp\ccnJ5OKT.ltrans0.ltrans.o: In function `main':
C:\Users\X\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\cores\arduino/main.cpp:46: undefined reference to `loop'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Nano.
So what I'm thinking is I'm setting it up completely wrong. Either I'm not setting up loops correctly OR I'm not supposed to be able to start in a function, refer to other functions within this function, and have these secondary or tertiary functions refer back to the original function meaning it's basically referring back to itself. Here's some example code to explain what I mean:
//Main Loop used for skipping through the main script's functions
void mainLoop()
{
loop();
{
buttonCheck();
etcFunction();
}
}
void buttonCheck()
{
{
buttonA_value = digitalRead(buttonA_pin);
if(buttonA_value == HIGH)
{
buttonA_function()
}
buttonB_value = digitalRead(buttonB_pin);
if(buttonB_value == HIGH)
{
buttonB_function()
}
}
void buttonA_function()
{
loop();
{
buttonC_value = digitalRead(buttonC_pin);
if(buttonC_value == HIGH)
{
...do whatever...
}
buttonD_value = digitalRead(buttonD_pin);
if(buttonD_value == HIGH)
{
mainLoop();
}
}
void buttonB_function()
{
loop();
{
buttonC_value = digitalRead(buttonC_pin);
if(buttonC_value == HIGH)
{
...do whatever...
}
buttonD_value = digitalRead(buttonD_pin);
if(buttonD_value == HIGH)
{
mainLoop();
}
}
void etcFunction()
{
...do whatever...
}
Of course this is a gross oversimplification of what I'm trying to work with, but hopefully it shows what I'm trying to do if my previous explanation doesn't work. Also it's requiring me to put a semicolon after the loop() for some reason which may show I'm not using it right. I'd appreciate any help as I've only been able to find posts about "loops in loops" which is not what I'm trying to do. This is a loop in a function.
loop();
Why ?
What's with this mainLoop() function ?
//Main Loop used for skipping through the main script's functions
That is exactly what the loop() function is for. Why are you trying to reinvent the wheel ?
loop (){ }; --> while(1){ };
rtek1000:
loop (){ }; --> while(1){ };
Great if you want to stop the program dead in its tracks and do nothing for ever
UKHeliBob -
From my understanding later on in the code I can't just go
void whatever()
{
void loop();
}
I'd need to address this "void loop()" function by a particular name. And inside many functions I have to add a loop until it exits upon some event.
Besides, did you miss the 2 other parts later on where it's a completely separate function that also requires a loop once it enters said function? It's not like I can have many "void loops()" and have any possibility of referring back to those even if they are different in containing code.
rtek1000 -
I've seen that but I thought the 1 in while() referred to how many times it would loop. As in not at all.
You seem to have completely missed the point of the loop() function and how to use it. loop() is just a function like all others. Calling it just causes the code that it contains to be executed, it does not cause the code that it is called from to loop in any way. Calling it from inside the loop() function itself, or from any function called by loop() will almost certainly cause the program to crash.
If you want to repeat a number of statements within a function there are a number of ways to do it such as a for loop or a while loop but calling the loop() function is not one of them
What exactly are you trying to do and/or what problem are you trying to solve ?
1 Like
ChintzyPCs:
UKHeliBob -
From my understanding later on in the code I can't just go
void whatever()
{
void loop();
}
I'd need to address this "void loop()" function by a particular name. And inside many functions I have to add a loop until it exits upon some event.
Besides, did you miss the 2 other parts later on where it's a completely separate function that also requires a loop once it enters said function? It's not like I can have many "void loops()" and have any possibility of referring back to those even if they are different in containing code.
rtek1000 -
I've seen that but I thought the 1 in while() referred to how many times it would loop. As in not at all.
No, did you read the PDF file?
The value 1 is always 1. If used as a test for a conditional function it will be understood as valid value, so in the case of conditional loop the loop is infinite, because 1 is always 1.
But if you want a finite loop, you can use some variable to test instead of value 1. Another option is to use the 'for' function instead of 'while'.
The word 'loop' in the arduino sketch is just to replace the main subroutine. Do not confuse the loop (for, while) function with the loop () subroutine.
Ah ok, so void loop() isn't a description of what it does necessarily. It's an actual title Arduino gives to that function which is part of every script. I just so happens to loop as well since almost all scripts need to loop.
I figured that as long as I knew my entire script was going to loop in the end I didn't have to stick to keeping it named void loop() specifically, and could call it whatever I wanted, especially if I needed to jump back to it later on.
But I suppose that explains why it's not working. Every time I'm trying to make my functions loop I'm just calling back to the void loop() function (that isn't there).
In my actual code I'm trying to set up the void loop() section as a neat checklist to go through several functions of my code so I don't have to write literally everything in that one function. Specifically:
void loop();
{
readButtons();
modeState_top();
modeState_bottom();
proofOfLife();
}
readButtons() reads the state of all the buttons.
modeState_top&bottom() checks the current mode I've set the RGB LED's to based on what button was pressed, then executes that mode (so color cycle, just white, just red, etc are all different modes)
proofOfLife() is part of the "blink without delay" setup
Then later on, one of the modes is to use 3 of the buttons to increase or decrease the colors by increments. Considering I want this mode (or rather function) to be contained from the rest of the script, I need it to loop until I exit by calling another function. However, I didn't know if I could just call up the loop() function by that name specifically and thought I had to change it to something else since, in my mind, that's just describing what it's doing, not what the name of the function is.
So you mention for loop or while loop. I guess I'll try using that instead.