How to merge these?

My professor has asked me to research how to merge both of these codes, he's running low on DC motors but has plenty of servos and needs the (continuous) servo motors to run like DC motors. Please help! He has specifically asked how to merge these into one sketch. Here's the code:

Continous Servo code

#include <PULSE.h>
PULSE pulse;

void setup() {
  pulse.PulseBegin();

  pulse.setServoSpeed(1,25);

}

void loop() {
  pulse.setServoPosition(1,0);
  delay(3000);
  pulse.setServoPosition(1,0);
  delay(3000);

DC motor code:

#include <PULSE.h>
PULSE pulse;

void setup() {
  pulse.PulseBegin();

  pulse.setMotorInvert(1,1);

}

void loop() {
  if (pulse.readSonicSensorCM(3) > 25) {
    pulse.setMotorPowers(35,35);
    pulse.setRedLED(LOW);
    pulse.setGreenLED(HIGH);
  } else {
    pulse.setGreenLED(LOW);
    pulse.setRedLED(HIGH);
    pulse.setMotorPowers(0,0);
    delay(500);
    pulse.setMotorPowers(-35,-35);
    delay(1000);
    pulse.setMotorPowers(0,0);
    delay(500);
    pulse.setMotorPowers(35,-35);
    delay(500);
  }

}

If you could give me any advice or guidance on how to merge these, that would be awesome!

See if this works:

#include <PULSE.h>
PULSE pulse;

void setup() {
  pulse.PulseBegin();
  pulse.setServoSpeed(1,25);
  pulse.setMotorInvert(1,1);

}

void loop() {
  if (pulse.readSonicSensorCM(3) > 25) {
    pulse.setMotorPowers(35,35);
    pulse.setRedLED(LOW);
    pulse.setGreenLED(HIGH);
  } else {
    pulse.setGreenLED(LOW);
    pulse.setRedLED(HIGH);
    pulse.setMotorPowers(0,0);
    delay(500);
    pulse.setMotorPowers(-35,-35);
    delay(1000);
    pulse.setMotorPowers(0,0);
    delay(500);
    pulse.setMotorPowers(35,-35);
    pulse.setServoPosition(1,0);
    pulse.setServoPosition(1,0);
    delay(3000);
    }
}

Before merging these convert them separately to use millis() instead of delay() so that after merging these they do not not block each other. Then you can start working on merging these. In case of doubt look at the BlinkWithoutDelay example in the IDE.

For new sketches, be sure to use delay only within setup() and very sparingly within loop()

1 Like

Good point!

Can you explain what EVERY line does in both programs?
Until you can, you’ll just be mashing the keyboard.

The first sketch was incomplete, assuming all that was missing was the closing brace it yet makes no sense:

Set a position, waste three seconds, set the identical position, waste three more seconds and… repeat forever.

a7

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