hey. I have a question but it's hard to explain so i'll attempt to give an example. My code is too long and complicated to attempt to use it here. but hopefully i can explain it so it's halfway understood.
Currently, i have about 8 "program" functions that all use the same settingsprofile1() in it. Some functions only call it once or twice, others it may call it up to 10 times. All of functions are on counters that has different values..... some get called often, and some not. What i'm trying to do is each time the function gets called based on the counter, i'm trying to get it to alternate between using the settingsprofile1() and settingsprofile2() each time the "program" function is ran. Like the first time run1Program() gets called, it uses the settingsprofile1() as it's settings on that run. And then the next time it gets called, change all of the settingsprofile1() calls to settingsprofile2() in it's code. And so on for rest of the different counter functions.
The settingsprofiles was called often so i consolidated them to help save memory. and right now i'm at 64% full, which is down from the 80% i was at a couple days. so i don't really have room to duplicate the functions and keep both versions.... but that way created other problems to work around.
loop(){
loopcount++;
longcount++;
if ( loopcount > 10) { run1Program();}
if (longcount > 20) {run2Program();}
ect..
ect..
// lots of other code
} // end of loop
void settingsprofile1(){
// pump settings
}
void settingsprofile2(){
// different pump settings
}
void settingsprofile3(){
// different stuff
}
void run1Program(){
// code
settingsprofile1();
// code
delay(1000);
// code
settingsprofile3();
delay(1000);
}
void run2Program(){
// different code
settingsprofile1();
// code
delay(2000)
settingsprofile3();
}
void run3Program(){
//different more code
settingsprofile1();
// code
delay(5000)
settingsprofile3();
//more code
}
Thanks for any help. I'm not sure if it can be done somewhat easily or not, so i'd figured i'd ask.
What does a single settings profile consist of? ie., what types of variables? How many different variables? Are the variables fixed or can they be altered at run time?
Im controlling an air pump that is monitored by a pressure sensor, which is set by a pot. The first profile is the air pump on constantly on until the target pressure is reached, and the 2nd profile is crude pwm from 50 to 150ms until the target pressure is reached, all of those variables are basically set.
The programs are different run cycles for the pump. And then im trying to have it alternate between full speed and pwm profiles each time a specific cycle is ran
Usually, a "profile" would be a selectable set of settings that are accessed by code. Those would normally be in arrays, which are accessed by a uniform block of code. What you have, seems to be selectable code. I really wonder if you have to do it this way, it seems unusual. But, it's impossible to tell, since you didn't post all your code, or explain what the settings are.
Even a "run cycle" sounds like a uniform set of timings that could be stored as data in arrays rather than coded.
If you have memory problems, this is important since if you have redundant code, it will eat up memory.
Profile may have not been the correct term. But those are the pump settings, while some of the code is set, 90% of the code is random. . delays, pressures, and pump run times. Even the counters are set with random target counts. So it's hard to just look at and understand whats going on, or even hard to explain. But like the person after you suggested using even and odd numbers, which ive already considered im gonna check it out tomorrow's
Thanks for your help. I think i was able to get it figured out. I haven't had a need to use true or false statements until now so it was a little hard to understand at first. I ended up having to write a test code to see it work. now hopefully i can update my code with the changes and not mess anything up.
Here's a copy of the test code i wrote for someone else to see as an example.
In the IDE you can use the Autoformat tool and it will put your code into a standard format.
There are a few common formats, most code readers can tolerate them all, most programmers choose or are forced to use one of several that are popular.
Sooner later you will make your own choice of what conventions to follow, and the Autoformat tool will not be necessary, the convention you are following will become second nature, like a space always go after comma kinda thing.
Ppl (some) take it very seriously and will lobby for one or another. Standard.
if (alternateRun) {
testRun1();
}
else {
testRun2();
}
or maybe
if (alternateRun)
{
testRun1();
}
else
{
testRun2();
}
You might consider writing a function called testRun. Pass it alternateRun as a parameter. Have the if that decides whether to call testRun1 or testRun2 there once rather than scattered all over your code.
Yeah. i know about the formats, but i've created my own style that is similar to the standard way, but i like to use indentions when it comes to IF, else , print statements and other commands that flow in a way that i can read and keep up and know where each section ends and starts. personally when everything is on the left it's easy to get lost especially when the code starts jumping around. But when it comes to redundant code, i tend to keep it in one line like this below, but i changed it so people here can read it better.
if (alternateRun) { testRun1();} else { testRun2();}
but i've always thought that curly brackets was required after If and Else statements but maybe not.
nope. i've never tried it. It's been challenging enough to learn coding with minimal help, and i tend stick with what works. and know brackets are usually good
My hint was really that you can test things yourself and save the time of posting a question... it's not unwelcome but really the instant feedback from trying things is often more productive. It's also covered in any C syntax reference/tutorial.