Multitasking question

Deleted by Author

The loop() should run over and over again as fast as possible. Avoid a while-loop to wait for something and avoid delay(). Both can be avoided with millis().

I prefer to have the first part of loop() to collect all the data from buttons and sensors, and the second part to make decisions.

What is this doing ? I don’t understand it.

  while (ftairState < ftprogairState) {
    digitalWrite(ftgoupPin, HIGH);           // power the up output pin
    ftairState = analogRead(ftairPin);
    Serial.println("Auto Airing Up Front");  //Print to serial for debug

The digitalWrite() is called over and over again, but once that output is HIGH, it stays high.
The while-loop waits for a certain value of the analogRead(). Why wait ?

Have a look at several things at a time and planning and implementing a program. Both use non-blocking code.

As @Peter_n says, avoid WHILE or FOR (unless they are very quick) and delay(), and read ALL the inputs at the start of loop().

Your code makes so sense with a big chunk missing. If it is too long post the .ino file as an attachment.


Rather than having functions for Front Up, Front Down, Back Up, and Back Down you could put code like this in loop() to continuously raise or lower the front and back to achieve the desired ride height:

void loop() {
  const int margin = 2;  // Dead band to keep the 
  digitalWrite(ftgoupPin, analogRead(ftairPin) < ftprogairState - margin);           // Front Up as needed
  digitalWrite(ftgodnPin, analogRead(ftairPin) > ftprogairState + margin);           // Front Down as needed
  digitalWrite(rrgoupPin, analogRead(rrairPin) < rrprogairState - margin);           // Rear Up as needed
  digitalWrite(rrgodnPin, analogRead(rrairPin) > rrprogairState + margin);           // Rear Down as needed

That will continuously try to keep the analog input value within 2 units of the setpoints.

Did you understand my questions ?

This sets pin 13 high : digitalWrite ( 13 , HIGH ) ;
If pin 13 is set high, it has no use to call that 100 times more, since it is already high.
It only turns off with : digitalWrite ( 13 , LOW ) ;

The while-loop waits for a certain value of analogRead(). Why ? I just waits, there is nothing else, wait for what ?

There is no such thing as holding a pin high.
A pin can be set high and can be set low, that’s it.

I could work if in other code in the sketch sets the pin to low.

I was just going to use two Arduinos, one for front and one for rear ...

If you need to bake a cake, and make a cup of coffee, do you use two kitchens?

Read this: How to do multiple things at once ... like cook bacon and eggs.

you could put code like this in loop()

digitalWrite(ftgoupPin, analogRead(ftairPin) < ftprogairState - margin);

Everyone has his preferred style. of course.

However I don’t like the idea of reading an input as part of an output statement because there is no opportunity to print the value of the input for debugging purposes.

I am a strong advocate of small single-purpose functions with meangingful names. They allow you to understand what the program is trying to do without needing to study the code in detail. Hence I favour goDownrr() etc.


Peter, I know it sounds like I'm being argumentative after asking for help, but I'm not. All three of you have been great help all along my journey into arduino land. I dont understand why you say the "while" wont turn the output high and then turn it low. I used a linear pot on the sensor input, and as long as it was less than the programmed air state, it set the output high as I saw my test LED illuminate. Once I turned the pot to the point where the air state was equal to the programmed air state, the LED on the output extinguished.

The sketch as you see it does work. Are you saying the "while" is the reason it will only run one autoairup at a time? My goal is to have it auto air up the front and back while reading the linear transducers and the programmed height (eprom read)simultaneously.

Robin, being able to print for debugging has been most helpful. How would you do it so the front and rear will auto air up simultaneously.

nick, thanks for the link

Thanks again to all of you for your help, adown

When I can choose between working ugly code or non-working beautiful code, then I choose the non-working beautiful code 8)

Robin, being able to print for debugging has been most helpful. How would you do it so the front and rear will auto air up simultaneously.

while (rrairState < rrprogairState)

How long does it take for that to become true ?
I presume it takes more than a few microseconds.

If you want both of these to work simultaneously you have to eliminate this WHILE and allow both to work a little at a time.

If I am misunderstanding the problem it is because your variable names are not very meaningful. Perhaps you can provide an explanation.


It only takes a second or two to reached the desired shock height if the air tank is full. If it is empty from sitting for a week, it could take 20 to 40 seconds. I think my problem is I only want to run auto air on start up, so I put it in setup. I could loop through if’s until it reached height if I put it in void loop. I’m just not clever enough to figure out non blocking code that can run both front and rear from set up.

One or two seconds is a very long time for an Arduino. 40 seconds would be forever :slight_smile:

You seem only to call the autoairupXX() functions from setup().

That means each of them is only called once.

It MIGHT work if you use this function

void autoAirUp() {
	while ((rrairState < rrprogairState) || (ftairState < ftprogairState)) {
		rrairState = analogRead(rrairPin);
		ftairState = analogRead(ftairPin);
		if (rrairState < rrprogairState) {
			digitalWrite(rrgoupPin, HIGH);          // power the up output pin
			Serial.println("Auto Airing Up Rear"); //Print to serial for debug
		else {
			digitalWrite(rrgoupPin, LOW); 
		if (ftairState < ftprogairState) {
			digitalWrite(ftgoupPin, HIGH);          // power the up output pin
			Serial.println("Auto Airing Up Front"); //Print to serial for debug
		else {
			digitalWrite(ftgoupPin, LOW); 

and call that from setup() in place of your existing calls to the two separate functions.

Read what I have posted very carefully as I may have made some silly typos.
Also think carefully about it in case it is not doing the correct thing at all.