u8glib with 12864 SPI Display + ultrasonic + motor control - huge delays

Hello,

I´m quite new to Arduino´s world and currently dealing with a SPI 12864 Display http://www.sainsmart.com/sainsmart-12864-128x64-graphic-blue-lcd-display-module-backlight-for-arduino-avr.html and the u8glib library.
Everything´s working fine but I´m having performance issues and thus hope whether someone can help.

I´d like to display five ultrasonic (hc-sr04) values on the lcd, which is working fine. However since the Arduino is also controlling a motor driver, I´m facing delays. A motor action (e.g. forward) should normally last for a short period of time (delay(200), but as soon as I enable the lcd function to draw the motor won´t stop until the display has been refreshed which seems to take 2-3 seconds.

Does anyone know how to solve this?

Here´s the code:

void distance() {
 
  front_distance_av = sonar_01.ping_median(3);
  front_distance = sonar_01.convert_cm(front_distance_av);
  front_left1_distance_av = sonar_02.ping_median(3);
  front_left1_distance = sonar_02.convert_cm(front_left1_distance_av);
  front_left2_distance_av = sonar_03.ping_median(3);
  front_left2_distance = sonar_03.convert_cm(front_left2_distance_av);
  front_right1_distance_av = sonar_04.ping_median(3);
  front_right1_distance = sonar_04.convert_cm(front_right1_distance_av);
  front_right2_distance_av = sonar_05.ping_median(3);
  front_right2_distance = sonar_05.convert_cm(front_right2_distance_av);
  delay(200);
  
}


void draw() {
  distance();
  
  u8g.setFont(u8g_font_5x8);
  
  u8g.drawStr( 0, 10, "FL2: ");
  u8g.setPrintPos(30, 10);
  u8g.print(front_left2_distance);
  
  u8g.drawStr( 0, 20, "FL1: ");
  u8g.setPrintPos(30, 20);
  u8g.print(front_left1_distance);

  u8g.drawStr( 0, 30, "F: ");
  u8g.setPrintPos(30,30);
  u8g.print(front_distance);
  
  u8g.drawStr( 0, 40, "FR1: ");
  u8g.setPrintPos(30, 40);
  u8g.print(front_right1_distance);
  
  u8g.drawStr( 0, 50, "FR2: ");
  u8g.setPrintPos(30, 50);
  u8g.print(front_right2_distance);
  
  u8g.drawLine(50, 1, 50, 60);
  
  delay(500);
}


void loop(void) {

  idleMotor_a();
 
   //  433Mhz Remote)
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      int i;
      for (i = 0; i < buflen; i++)
      {
        if(buf[i]=='1')
        {
          lcd_int = 1;
          forward_a();
        }
        
        if(buf[i]=='2')
        {
          backward_a();
        }
        
        if(buf[i]=='3')
        {
          leftTurn_a();  
        }
        
        if(buf[i]=='4')
        { 
          rightTurn_a();     
        }
        
        if(buf[i]=='5')
        {
          idleMotor_a();
        } 
    }}
    
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  delay(200);
}

void forward_a()
{
  digitalWrite(PoM1b, LOW);
  analogWrite(PoM1a, 120);
  digitalWrite(PoM2b, LOW);
  analogWrite(PoM2a, 120);
  delay(200);
}

I didn´t have such delay-issues with a 20x4 i2c display using the LiquidCrystal-Librabry, but I´d like to go for the 12864-Display, which has more “drawing” features.

Can anyone help?

Greetings
Norman

Hi Norman

I removed some delays and also placed the call to “distance()” at a different positition. Maybe some more delays can be removed.

See my comments in the fixed code (not tested):

void distance() {
 
  front_distance_av = sonar_01.ping_median(3);
  front_distance = sonar_01.convert_cm(front_distance_av);
  front_left1_distance_av = sonar_02.ping_median(3);
  front_left1_distance = sonar_02.convert_cm(front_left1_distance_av);
  front_left2_distance_av = sonar_03.ping_median(3);
  front_left2_distance = sonar_03.convert_cm(front_left2_distance_av);
  front_right1_distance_av = sonar_04.ping_median(3);
  front_right1_distance = sonar_04.convert_cm(front_right1_distance_av);
  front_right2_distance_av = sonar_05.ping_median(3);
  front_right2_distance = sonar_05.convert_cm(front_right2_distance_av);
  delay(200);			// maybe this can be removed
  
}


void draw() {
  // instead of calling distance() here, do this call in "loop()"
  // distance();
  
  u8g.setFont(u8g_font_5x8);
  
  u8g.drawStr( 0, 10, "FL2: ");
  u8g.setPrintPos(30, 10);
  u8g.print(front_left2_distance);
  
  u8g.drawStr( 0, 20, "FL1: ");
  u8g.setPrintPos(30, 20);
  u8g.print(front_left1_distance);

  u8g.drawStr( 0, 30, "F: ");
  u8g.setPrintPos(30,30);
  u8g.print(front_distance);
  
  u8g.drawStr( 0, 40, "FR1: ");
  u8g.setPrintPos(30, 40);
  u8g.print(front_right1_distance);
  
  u8g.drawStr( 0, 50, "FR2: ");
  u8g.setPrintPos(30, 50);
  u8g.print(front_right2_distance);
  
  u8g.drawLine(50, 1, 50, 60);
  
  // the delay does not make sense here, removed
  // delay(500);
}


void loop(void) {

  idleMotor_a();
 
   //  433Mhz Remote)
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      int i;
      for (i = 0; i < buflen; i++)
      {
        if(buf[i]=='1')
        {
          lcd_int = 1;
          forward_a();
        }
        
        if(buf[i]=='2')
        {
          backward_a();
        }
        
        if(buf[i]=='3')
        {
          leftTurn_a();  
        }
        
        if(buf[i]=='4')
        { 
          rightTurn_a();     
        }
        
        if(buf[i]=='5')
        {
          idleMotor_a();
        } 
    }}
    
  // distance should be called outside the "picture loop"
  distance();
    
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  delay(200);
}

void forward_a()
{
  digitalWrite(PoM1b, LOW);
  analogWrite(PoM1a, 120);
  digitalWrite(PoM2b, LOW);
  analogWrite(PoM2a, 120);
  delay(200);
}

Oliver

Hello Oliver,

YOU MADE MY DAY!

I used delays here and there just being desperate, as the LCD wouldn't update properly and giving broken outputs sometimes concerning the numbers.
Guess I made everything worse.

Well, removing the delays and placing distance() right before the u8glib-draw, made everything work like a charm. Fast and correct LCD updates and no delays about the motor-functions.

I was already thinking there was something with u8glib compared to LiquidCrystal, but in the end I should focus more on how to program.

Thanks for your fast support!

Regards
Norman