Can this be Simplified?

Hello,

I'm wondering if the println percentage increase of my subroutine can be simplified such that it increases by 10% every 2500ms or increase by 1% every 2.50ms.

Is there an equation that I can add that would let me do that or do have to do something else?

Thanks in advance.

//Systems Check 
 void SystemCheck()
 {
   display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("0%");
   display.display();
    recognizer.say("Starting Scan");
    delay(2000);
    recognizer.say("Checking Essential Colors");
    delay(2000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("10%");
   display.display();
    recognizer.say("Checking Red"); 
    RGB_color(255, 0, 0); //Red
    delay(3000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("20%");
   display.display();
    recognizer.say("Checking Green");
    RGB_color(0, 255, 0); //Green
    delay(3000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("30%");
   display.display();
    recognizer.say("Checking Blue");
    RGB_color(0, 0, 255); //Blue
    delay(3000);
    recognizer.say("Color Check Completed");
    RGB_color(255, 255, 0); //Yellow
    delay(2000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("40%");
   display.display();
    recognizer.say("Checking Non-Essential Colors");
    delay(2000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("50%");
   display.display();
    recognizer.say("Checking White"); 
    RGB_color(255, 255, 255); //White
    delay(3000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("60%");
   display.display();
    recognizer.say("Checking Purple");
    RGB_color(255, 0, 255); //Purple
    delay(3000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("70%");
   display.display();
    recognizer.say("Checking Yellow");
    RGB_color(255, 255, 0); //Yellow
    delay(3000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("80%");
   display.display();
    recognizer.say("Non-Essential Color Check Complete");
    delay(3000);
    display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("90%");
   display.display();
    recognizer.say("Scanning");
    delay(3000);
    recognizer.say("Scan Complete");
    display.clearDisplay();
    display.setTextSize(2);
    display.setCursor(0,0);
    display.print("Scanning");
    display.setCursor(0,17);
    display.print("Complete");
    display.display();
    delay(3000);
  }
  

Please post a complete sketch illustrating your question

This is it. It's just a subroutine for show (Right now).

(Right now, this just let's me know if my RGB led is working properly by running through the colors)

You have a bunch of repeated bits.

a
b
c (number1)
d

a
b
c (number2)
d

a
b
c (number3)
d

Break this into a function

dothis(numberX)

a
b
c (numberX)
d

Then your routine no longer grows. Or not by much.

dothis(number1)
dothis(number2)
dothis(number3)
dothis(number4)

-jim lee

1 Like

Is there an example I can refer to.

I remember seeing something like that where the Number would be x (current number) and it would increase by y (Which would be 1 or something), but I can't remember where I saw it.

Umm.. no examples that I think would help. But lets do this..

I'm betting you used copy paste to replicate blocks of code for this didn't you?

-jim lee

1 Like

Yes. I wanted to keep the "Scanning" "Please Wait..." display the same and have the percentage at the bottom increase, so instead of typing everything out over and over again, I just copied and pasted it and just changed the percent value.

Ok that block of code you replicated is your, lets call it... displayFunction()

Lets see one of those blocks here.

void displayFunction() {

// block goes here

}
1 Like

So it would be this:

void displayFunction() {

   display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.println("0%");
   display.display();
    recognizer.say("Starting Scan");
    delay(2000);
}

Yes! Now what part changes from call to call? The % thing?

void displayFunction(int percent) {

   display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.print(percent);
   display.println(" %");
   display.display();
    recognizer.say("Starting Scan");
    delay(2000);
}

void SystemCheck() {

   displayFunction(0);
   displayFunction(10);
   displayFunction(20);
   // See the pattern?
}
1 Like

Ok, I think I got it.

So display.print(precent); will display the displayFunction(10); value/number (10 in this case). Correct?

But I'm a bit confused on how it increases/counts up.

In your systemCheck() I call it with 0, 10, 20, etc.

1 Like

So if I were to do it in my code it would be like this:
Correct?

//Systems Check 
 void SystemCheck()
 {
    displayFunction(); //Would I add this or do I have to put the value in between the 
 //brackets  like displayFunction(0); 
    recognizer.say("Starting Scan");
    delay(2000);
    recognizer.say("Checking Essential Colors");
    delay(2000);
    displayFunction();
    recognizer.say("Checking Red"); 
    RGB_color(255, 0, 0); //Red
    delay(3000);
    //So on and so on
  }

think of functions like... Contract workers. I give you this stuff and you do that job. If you want the display stuff you wrote to happen you give it a percent value (its in the contract) and it will work that into the display like you wrote it.

Your code above looks fine, but you forgot to put in the percent values for the display to write out.

Oh yeah, sorry. brackets.

1 Like

See how delay() has brackets with a passed in value? The contract is "HOLD Here for (this many milliseconds)" Just like your new displayFunction(). "Draw this with (this percentage value)"

1 Like

So would I have 3 separate subroutines like this?

//Systems Check 
 void SystemCheck()
 {
    displayFunction(0);
    recognizer.say("Starting Scan");
    delay(2000);
    recognizer.say("Checking Essential Colors");
    delay(2000);
    displayFunction(10);
    recognizer.say("Checking Red"); 
    RGB_color(255, 0, 0); //Red
    delay(3000);
    //So on and so on
  }
void  displayFunction() {

   displayFunction(0);
   displayFunction(10);
   displayFunction(20);
}
void displayFunction(int percent) {

   display.clearDisplay();
   display.setTextSize(2);
   display.setCursor(0,0);
   display.print("Scanning");
   display.setTextSize(2);
   display.setCursor(0,17);
   display.println("Please");
   display.println("Wait...");
   display.setTextSize(1);
   display.print(percent);
   display.println(" %");
   display.display();
}

No, just put the percent values into your systemCheck() one where you want them.

You don't have a function with a list of delay()s do you?

1 Like

I'm sorry I'm a bit confused. What do you mean?

Why did you add the third function?

void  displayFunction() {

   displayFunction(0);
   displayFunction(10);
   displayFunction(20);
}
1 Like

Because I thought it's supposed to be called upon so the display.print(precent) knows what to write.