How do I use a delay within a sketch loop which will not disrupt other commands?

Hi guys,

So yea basically I want to know how I can use a delay command in my sketch but I want the other commands/lines to continue working as normal, because a normal delay will stop other functions from carrying on… so here’s my sketch (I have added a comment where the delay is in the void loop section)

/*
 Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
 For more information visit my blog: http://blog.tkjelectronics.dk/ or 
 send me an e-mail:  kristianl@tkjelectronics.com
 */

#include <PS3BT.h>
#include <Servo.h>
Servo manzoorsServo;
Servo manzoorsSpeedController;
Servo manzoorsSpeedControllerHover;

USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
PS3BT PS3(&Btd); // This will just create the instance
//PS3BT PS3(&Btd,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
int servoPin = 8;
int speedController = 9;
int sCH = 6;
int b = 100;
int d = 70;
int ledPin =  7;      // the number of the LED pin

void setup()

  {  
    pinMode(ledPin, OUTPUT);
    manzoorsServo.attach(servoPin);
    manzoorsSpeedController.attach(speedController);
    manzoorsSpeedControllerHover.attach(sCH);
  {
    Serial.begin(115200);
    if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
    Serial.print(F("\r\nPS3 Bluetooth Library Started"));
 }
}
 
void loop() {
  Usb.Task();
  
  if(PS3.PS3Connected || PS3.PS3NavigationConnected) 
  
       {
       manzoorsServo.write(map(PS3.getAnalogHat(RightHatX), 0, 255, 0, 120));
       }
       else
       {
       manzoorsServo.write(115);
       }
       
       if(PS3.PS3Connected || PS3.PS3NavigationConnected)
       {
                                                                               
       manzoorsSpeedController.write(map(PS3.getAnalogButton(R2_ANALOG), 0, 255, 50, 135));
       
       manzoorsSpeedControllerHover.write(map(PS3.getAnalogButton(L2_ANALOG), 0, 255, 100, 160));
       
       //below is the command I need working vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
       if(PS3.getButtonClick(LEFT))
       {
         digitalWrite(7, HIGH);
         delay(300);
         digitalWrite(7, LOW);
       }
       //I basically want this above command working within this sketch which I know it wont as it currently is. ^^^^^^^^^^^^^^^^^^
       
       if(PS3.getButtonClick(PS)) {
       manzoorsSpeedControllerHover.write(100);
       manzoorsSpeedController.write(d);
       manzoorsServo.write(115);
       ledState = LOW;
       PS3.disconnect();
       }
      }
     }

Any help would be really appreciated, thanks!

Look at the "Blink without delay" example in the IDE, without delay

I did check that but , basically I want the LED to flash when I press a button,

So how do I modify the code below to only continuously run when I do this:

"if(PS3.getButtonClick(LEFT))"

{
       unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   


    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
       }

When you detect the button as clicked, note the time (using millis() ) and set pin 7 HIGH, then 300 ms later, set it LOW.

You need to use millis()

view this thread by Robin2 - good stuff

You need to look for button press and digitalWrite it and take note of the time, so like currentMillis, and also set the state to HIGH, after your desired interval of time set it low through checking that

if(currentMillis - previousMillis >= interval)

Thankyou for your help, it worked out for me :smiley: