pulseIn() and PWM

I am programming a bot I built, but I've noticed that whenever I use pulseIn() to check the ultrasonic sensors, the PWM output cuts out momentarily. Is this normal? Shouldn't the PWM output stay on until changed?

It should, yes, assuming you're using analogWrite() to set the PWM. Can you post a simplified version of the code that has the same problem?

I will post a minimum code example tonight, but I have to wait until I get home from work! I am using analogWrite() to set the PWM output. I really just wanted to see if this rang a bell for anyone; have you ever used pulseIn() and analogWrite() together?

I haven't, but maybe someone else has. Anyone?

The code sample would be great, thanks.

MY BAD. The problem was elsewhere in code. Sorry for the false alarm. I mispositioned my rampup() call! Every time the main loop ran, it ramped the motors up from stop! Doh!

eustace, could you post your code anyway?
I’m having issues with the ping causing delays (jitters) on my servos, and vise versa

Sure - I’m pretty sure this is the code I was running (I’m at work, and don’t have access to all my code).

// Botcode 2
// Mike Crist 1/6/08

int xFwdPin = 9;
int xRevPin = 10;
int yFwdPin = 3;
int yRevPin = 5;
int frontOut = 6;
int frontIn = 7;
int backOut = 11;
int backIn = 12;
long usValue = 0;
long closeUp = 3000;

void setup()
{
  pinMode(frontIn, INPUT);
  pinMode(backIn, INPUT);
  pinMode(frontOut, OUTPUT);
  pinMode(backOut, OUTPUT);
  rampup();
}
 
void loop()
{
  usValue = scanfront();
  if(usValue > closeUp);
  {
    if(usValue < closeUp)
    {
      stopboth();
      delay(500);
      turnright();
      delay(500);
      usValue = scanfront();
      if(usValue < closeUp)
      {
        usValue = scanback();
        if(usValue > closeUp)
        {
          turnleft();
          delay(500);
          turnleft();
          delay(500);
          usValue = scanfront();
        }
        else
        {
          turnright();
        }
      }
      rampup();
    }
  }
}

long scanfront()
{
  long sensorReturn = 0;
  digitalWrite(frontOut, LOW);
  delayMicroseconds(2);
  digitalWrite(frontOut, HIGH);
  delayMicroseconds(5);
  digitalWrite(frontOut, LOW);
  sensorReturn = pulseIn(frontIn, HIGH);
  return(sensorReturn);
}

long scanback()
{
  long sensorReturn = 0;
  digitalWrite(backOut, LOW);
  delayMicroseconds(2);
  digitalWrite(backOut, HIGH);
  delayMicroseconds(5);
  digitalWrite(backOut, LOW);
  sensorReturn = pulseIn(backIn, HIGH);
  return(sensorReturn);
}

void turnright()
{
  analogWrite(xFwdPin, 40);
  analogWrite(yFwdPin, 0);
  analogWrite(xRevPin, 0);
  analogWrite(yRevPin, 55);
  delay(700);
  //analogWrite(xFwdPin, 90);
  //analogWrite(yRevPin, 115);
  //delay(600);
  analogWrite(xFwdPin, 0);
  analogWrite(yRevPin, 0);
}

void turnleft()
{
  analogWrite(xFwdPin, 0);
  analogWrite(yFwdPin, 55);
  analogWrite(xRevPin, 40);
  analogWrite(yRevPin, 0);
  delay(700);
  //analogWrite(xRevPin, 90);
  //analogWrite(yFwdPin, 115);
  //delay(600);
  analogWrite(xRevPin, 0);
  analogWrite(yFwdPin, 0);
}

void rampup()
{
  analogWrite(xFwdPin, 40);
  analogWrite(yFwdPin, 55);
  analogWrite(xRevPin, 0);
  analogWrite(yRevPin, 0);
  delay(200);
  analogWrite(xFwdPin, 140);
  analogWrite(yFwdPin, 165);
  delay(300);
  analogWrite(xFwdPin, 210);
  analogWrite(yFwdPin, 255);
  delay(400);
}

void stopboth()
{
  analogWrite(xFwdPin, 0);
  analogWrite(yFwdPin, 0);
  analogWrite(xRevPin, 0);
  analogWrite(yRevPin, 0);
}

The values I’m using for each motor are different to compensate for a difference in drag between sides.