The big problem of my servo and oled combination

Hi, I have an I2C display OLED, and I just combined the code with the servo, and now the servo moved but it's like It has a long delay time before the next action is made, the screen is the same. Both the screen and servo feel like they only do one action and don't loop.
, could anyone help me check? I'm not pretty sure where the problem is.

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

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 
#define OLED_RESET 4
#define SSD1306_NO_SPLSH
#define SSD1306_BLACK 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Servo servo1;
Servo servo2;
int pos = 90;    // variable to store the servo position 
void setup() 
{ 
  servo1.attach(8);
  servo2.attach(7);
  Serial.begin(115200);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  // Display static text
  display.println("To be or not to be");
  display.display(); 
  delay(100);
 servo1.write(0);
  delay(100);
 servo1.write(90); 
 servo2.write(0);
  delay(100);
 servo1.write(90); 
}
void loop() 
{ 
display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
  delay(1000);
  for(pos = 90; pos >= 30; pos --){
  servo1.write(pos);
  servo2.write(180-pos);
  } 
  //delay(5000);
  for(pos = 30; pos <= 90; pos ++)
  {
    servo1.write(pos+100);
    servo2.write(180-pos);
    delay(15);
  } 
}

edit your post put the code in code tags.

Do you need about 15 seconds of delay in your code? Especially when the issue is

i did try to change the value smaller, and it did work for a while, but then the new problem is the screen is not working, when i tried and found that if i just change the last 15 seconds value, then it's the same, so i change all of them, and the servo is good, but the screen now really make me crazy

// Display static text
  display.println("To be or not to be");
  display.display(); 
  delay(100);
 servo1.write(0);
  delay(100);
 servo1.write(90); 
 servo2.write(0);
  delay(100);
 servo1.write(90); 
}
void loop() 
{ 
display.startscrollright(0x00, 0x0F);
  delay(20);
  display.stopscroll();
  delay(10);
  display.startscrollleft(0x00, 0x0F);
  delay(20);
  display.stopscroll();
  delay(10);
  display.startscrolldiagright(0x00, 0x07);
  delay(20);
  display.startscrolldiagleft(0x00, 0x07);
  delay(20);
  display.stopscroll();
  delay(10);
  
  for(pos = 90; pos >= 30; pos --){
  servo1.write(pos);
  servo2.write(180-pos);
  } 
  //delay(5000);
  for(pos = 30; pos <= 90; pos ++)
  {
    servo1.write(pos+100);
    servo2.write(180-pos);
    delay(5);
  } 
}

If you have a ESP32 the matter could be solved by using the OS, freeRTOS.

A state machine and delay using millis() are 2 tools that will solve the responsiveness issue. A search on this site will lead you to Finite State Machine and millis().

Doing multiple things at the same time - Using Arduino / Programming Questions - Arduino Forum

so sad, i don't have it, and the day after tomo is my deadine :sob:

What is the “it” you don’t have?
That was a link to tell you how to solve your problem.
You combined your code incorrectly for the code you have. Because the displays seem to need a lot of time between accessing and the servo code is disrupted by long delays you have to convert your code into a state machine. This is where you split your code into two tasks and write a function for each. All the delays need to be removed and any for loops unrolled or replaced by code that just does one iteration of the loop and then returns.

It is not hard if you follow the link and look at the blink without delay example in the IDE.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.