An alternative to the delay() function ?

Hello !

So i have a 128x64 OLED display that is showing some text and animations.

Im using the Adafruit SSD1306 and GFX library to create a rectangle and i want the rectangle to flash/blink independently from the rest of the animations and text.

I know this can be done using the delay() function but then everything else stops during the delay.

The part i want to be flashing is : display.drawRect(0, 38, 50, 13, WHITE);

Any suggestions on how this could be done?

/ Max

[code]
#include <Adafruit_SH1106.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include "Oled3DDriver.h"

#define OLED_RESET 4



Adafruit_SH1106 display(OLED_RESET);



int count1 = 0;
int count2 = 0;
int count3 = 0;
int shape = 0;

int p1 = random(0,100);



float zoom = 2.5;
float angle = 1.02;

GPoint rotate;
GPoint linear;






void setup()



{

 

  display.begin(SH1106_SWITCHCAPVCC, 0x3c);

 



  display.display();



  delay(500);


  display.invertDisplay(true);
  delay(700);
  display.invertDisplay(false);
  delay(700);


;

}

void loop() {


    count1 = (count1 + 6) % 3600;
    count2 = (count2 + 100) % 3600;
    count3 = (count3 + 2) % 3600;
    if (count1 == 0) {
      shape = (shape + 1) % 8;
    }


    display.clearDisplay();







    transformReset();
    transformLinear(25, 0, 0);
    transformRotate(count1 / 10, count2 / 10, count3 / 10);
    wireQube(GraphicPoint(15, 15, 15), GraphicPoint(-15, -15, -15));
    
  
    
    //display.drawCircle( 120,  30,  3,  WHITE);

    //display.drawCircle( 120,  30,  15,  WHITE);
    //display.drawLine(10,  45,  50,  45,  WHITE);

    
    
    
    
    
    
    
    display.fillRect(0, 0, 70, 11, WHITE);
    display.fillRect(0, 53, 74, 12, BLACK);
    display.fillRect(73, 53, 56, 18, BLACK);
    display.fillRect(0, 26, 88, 12, BLACK);


    display.setTextSize(1);
    display.setTextColor(BLACK);
    display.setCursor(2, 2);
    display.println("DREAM:#2836");




    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(10, 41);
    display.println("START!");



    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 15);
    display.println("dur:32h23m");

    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 27);
    display.println("date:1990.02.17");


    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 55);
    display.println("WAKEUP:NEVER");



    drawPercentbar( 75, 55, 53, 7,p1);



 
  
/* This is the part that i want to turn on and off repeatedly*/    
display.drawRect(0, 38, 50, 13, WHITE);

 


    display.display();


    p1++;
    if( p1 > 100) p1 =0;


    delay(5);




  }

void drawPercentbar(int x,int y, int width,int height, int progress)
{

   progress = progress > 100 ? 100 : progress;
   progress = progress < 0 ? 0 :progress;

   float bar = ((float)(width-4) / 100) * progress; 
 
   display.drawRect(x, y, width, height, WHITE);
   display.fillRect(x+2, y+2, bar , height-4, WHITE);

  }
  void transformLinear(float x, float y, float z) {
    linear.x = linear.x + x;
    linear.y = linear.y + y;
    linear.z = linear.z + z;
  }

  void transformRotate(float gradX, float gradY, float gradZ) {
    rotate.x = rotate.x + (gradX / 57.2957795);
    rotate.y = rotate.y + (gradY / 57.2957795);
    rotate.z = rotate.z + (gradZ / 57.2957795);
  }

  void transformReset() {
    linear.x = 0;
    linear.y = 0;
    linear.z = 0;
    rotate.x = 0;
    rotate.y = 0;
    rotate.z = 0;
  }

  GPoint transformPoint(GPoint ap) {
    GPoint bp;
    //Z rotate
    bp.x = (ap.x * cos(rotate.z)) - (ap.y * sin(rotate.z));
    bp.y = (ap.x * sin(rotate.z)) + (ap.y * cos(rotate.z));
    bp.z = ap.z;
    ap = bp;

    //X rotate
    bp.z = (ap.z * cos(rotate.x)) - (ap.y * sin(rotate.x));
    bp.y = (ap.z * sin(rotate.x)) + (ap.y * cos(rotate.x));
    bp.x = ap.x;
    ap = bp;

    //Y rotate
    bp.x = (ap.x * cos(rotate.y)) - (ap.z * sin(rotate.y));
    bp.z = (ap.x * sin(rotate.y)) + (ap.z * cos(rotate.y));
    bp.y = ap.y;


    //Move linear
    bp.x = bp.x + linear.x;
    bp.y = bp.y + linear.y;
    bp.z = bp.z + linear.z;

    return bp;
  }

  void line(GPoint p1, GPoint p2) {
    p1 = transformPoint(p1);
    p2 = transformPoint(p2);
    display.drawLine(
      round((p1.x / pow(angle, p1.z)) * zoom) + 63, round((p1.y / pow(angle, p1.z)) * zoom) + 31,
      round((p2.x / pow(angle, p2.z)) * zoom) + 63, round((p2.y / pow(angle, p2.z)) * zoom) + 31,
      WHITE);
  }

  void wireQube(GPoint p1, GPoint p2) {
    line(GraphicPoint(p1.x, p1.y, p1.z), GraphicPoint(p1.x, p1.y, p2.z));
    line(GraphicPoint(p1.x, p1.y, p1.z), GraphicPoint(p1.x, p2.y, p1.z));
    line(GraphicPoint(p1.x, p2.y, p2.z), GraphicPoint(p1.x, p2.y, p1.z));
    line(GraphicPoint(p1.x, p2.y, p2.z), GraphicPoint(p1.x, p1.y, p2.z));

    line(GraphicPoint(p1.x, p1.y, p1.z), GraphicPoint(p2.x, p1.y, p1.z));
    line(GraphicPoint(p1.x, p1.y, p2.z), GraphicPoint(p2.x, p1.y, p2.z));
    line(GraphicPoint(p1.x, p2.y, p1.z), GraphicPoint(p2.x, p2.y, p1.z));
    line(GraphicPoint(p1.x, p2.y, p2.z), GraphicPoint(p2.x, p2.y, p2.z));

    line(GraphicPoint(p2.x, p1.y, p1.z), GraphicPoint(p2.x, p1.y, p2.z));
    line(GraphicPoint(p2.x, p1.y, p1.z), GraphicPoint(p2.x, p2.y, p1.z));
    line(GraphicPoint(p2.x, p2.y, p2.z), GraphicPoint(p2.x, p2.y, p1.z));
    line(GraphicPoint(p2.x, p2.y, p2.z), GraphicPoint(p2.x, p1.y, p2.z));
  }

  GPoint GraphicPoint(float x, float y, float z) {
    GPoint temp;
    temp.x = x;
    temp.y = y;
    temp.z = z;
    return temp;
  }

  void triangle(GPoint p1, GPoint p2, GPoint p3) {
    line(p1, p2);
    line(p2, p3);
    line(p3, p1);
  }

[/code]

I think you will find the appropriate techniques if you look at the 'Blink without delay' tutorial.

Don

Thank you Don!

This is probably the right way to go , however i cant figure out how to apply this to MY code.

In the Blink Without Delay example , you deal with I/O and LED,s but all i want to do is to show a rectangle , and then not show the rectangle repeatedly .

I assume this is done in the IF statement section , but i cant find any example of this anywhere.

Any help would be greatly appreciated.

/ Max

Why

do

you

have all

the

blank lines in

your code?

It makes it hard to read your code. Also, without comments I wouldn't even try.

The example shows that in your preliminary steps you establish a variable (they called it 'previousMillis') to keep track of the last time you did something.

You also establish a variable (they called it 'interval') to keep track of how much time you want between events.

In the loop you check to see if the time between now ('currentMillis') and the last event ('previousMillis') is greater than the desired time ('interval') between steps.

If the interval has not elapsed then you keep doing whatever else you were doing. If the time interval is elapsed then you go do what you want to do and also don't forget to reset 'previousMillis'.

Don