Timer delay in js

If you are asking a question, please follow this template:

  1. My goal is: I want to put delay between two of my instructions to push DC motor forward and backward. I want to push motor for 600 millisecond first and then pull it backwards
  2. My actions are:
        GPIO.set_mode(2, GPIO.MODE_OUTPUT);
        GPIO.set_mode(4, GPIO.MODE_OUTPUT);

        Timer.set(600, 0, function() {
          GPIO.write(4, 0);
          GPIO.write(2, 1);
        }, null);

        Timer.set(600, 0, function() {
          GPIO.write(4, 1);
          GPIO.write(2, 0);
        }, null);
          
          //your code to be executed after 1 second
          GPIO.write(4, 0);
          GPIO.write(2, 0);
  1. The result I see is: Motor does not respond, I just here click-click sound.
  2. My expectation & question is: I am using H-bridge motor driver IC for controlling and power amplification purposes.

You’'ll have to chain the timers:

let doMotor = function () {
  // move motor forwards
  // ...
  Timer.set(600, 0, function () {
    // move motor backwards after 600 ms
    // ...
    Timer.set(600, 0, function () {
      // stop motor after 600 ms
      // ...
    }, null);
  }, null);
};
1 Like

Thank you, It worked for me:)