Cmucam4 and ping sensor on mega2560 r3

I am making an rc car that will follow a specific color but cant make the cmucam4 and parallax ping sensor work at the same time, the ping sensor worked before i added the cam code, then it worked for a minute or two when i put in the break at the end of the for(;:wink: loop. I cant find what the issue is any advice or suggestions would be greatly appreciated.

Just to be clear my problem is that the ping wont run, but the cmucam4 is working.

also would it be possible to add a sg-5010 servo to this setup, im running the arduino with an 8 AA battery holder, and a seeed motor shield powered with the same, the servo would only run at the very begining while searching for the color as the car ran in a circle, (is it possible to power the servo with a 4 AA batter pack directly?)

Using a breadboard to power the ping, cam, and also have the motor shield connected to the 5v from the arduino although the motor shield is powered by 8 AA it won’t work otherwise, but even when i have only the ping connected to the arduino it wont start.

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface 
int speedpinA=9;//enable motor A //forward/backward M1 on shield
int pinI3=12;//define I3 interface 
int pinI4=13;//define I4 interface 
int speedpinB=10;//enable motor B // left/right M2 on shield
int spead =150;//define the spead of motor
 int turnspeed= 180;
 const int pingPin = 53;
 
 
 #include <CMUcam4.h>
#include <CMUcom4.h>
#include <Servo.h>
#define YUV_MODE false
Servo myservo;

int pos = 0;

#define RED_MIN 220
#define RED_MAX 255
#define GREEN_MIN 100
#define GREEN_MAX 140
#define BLUE_MIN 95
#define BLUE_MAX 125

#define LED_BLINK 5 // 5 Hz
#define WAIT_TIME 5000 // 5 seconds

#define PIXELS_THRESHOLD 2
#define CONFIDENCE_THRESHOLD 15
#define NOISE_FILTER_LEVEL 5

CMUcam4 cam(3);

 
 
 
void setup()
{
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
  Serial.begin(9600);
  myservo.attach(7);

  cam.begin();

  cam.LEDOn(LED_BLINK);
  delay(WAIT_TIME);

  cam.autoGainControl(false);
  cam.autoWhiteBalance(false);

  cam.LEDOn(CMUCAM4_LED_ON);

  cam.LEDOn(CMUCAM4_LED_ON);
  cam.colorTracking(YUV_MODE);

  //  pinMode(LEFT_LED_PIN, OUTPUT);
  //  pinMode(MIDDLE_LED_PIN, OUTPUT);
  //  pinMode(RIGHT_LED_PIN, OUTPUT);


  cam.noiseFilter(NOISE_FILTER_LEVEL);

}
 
void forward()
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     //analogWrite(speedpinB,turnspeed);
     //digitalWrite(pinI4,LOW);//turn DC Motor B move clockwise
     //digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);
}
void backward()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
    // analogWrite(speedpinB,turnspeed);
     //digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     //digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);
}
void left()//
{
     //analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,turnspeed);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     //digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
     //digitalWrite(pinI1,LOW);
}
void right()//
{
     //analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,turnspeed);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     //digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
     //digitalWrite(pinI1,LOW);
}
void stop()//
{
     digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor. 
     digitalWrite(speedpinB,LOW);
     delay(100);
}
void straight(){
  digitalWrite(speedpinB,LOW);
}
long microsecondsToInches(long microseconds) 

{
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
void loop()
{
    CMUcam4_tracking_data_t data;

  cam.trackColor(RED_MIN, RED_MAX, GREEN_MIN, GREEN_MAX, BLUE_MIN, BLUE_MAX);

  for(;;)
  {
    cam.getTypeTDataPacket(&data); // Get a tracking packet.

    Serial.print("Confidence = "); 
    Serial.print(data.confidence); 
    Serial.print('\t');
    Serial.print("PostionX = "); 
    Serial.println(data.mx);
    Serial.print('\t'); 
    Serial.print("Pixels = "); 
    Serial.println(data.pixels);
    int xpos=data.mx;
  
    if (xpos<1){
      straight();
    } 
    else if (xpos<68){
     left();
    delay(5);
    }
    else if (xpos>92) {
      right();
    }
    else{
      straight();
    }
    break;
  }
  long duration, inches, cm;
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
if (cm > 29)
{forward();
}
else if (cm < 25)
{backward();}
else
{stop();}
}

loop() is called in an infinite loop. Why have you added an infinite loop inside loop()? You need to restructure your program to get rid of that.

sorry i really dont know what that means could it be the for(;; ) loop that your talking about

could it be the for(;;) loop that your talking about

Yes.

The break after the if statements wont get it out of that?

This break;?

  for(;;)
  {
    cam.getTypeTDataPacket(&data); // Get a tracking packet.

   // <snip>

    break;
  }

Why do you have a loop at all? The very last thing that will happen before you loop the second time is that you will breal out of the loop so that does not happen. A complete waste.

thanks, i tried the break because i couldnt get rid of the for loop without it giving me some compiling error guess I missed commenting out part of the loop. I will try this when i get home. what does for(;; ) mean anyway is the ;; mean its an infinite loop?

what does for(;; ) mean anyway

A for loop statement has three parts. The first part, optional, is an initialization section. You don't have to initialized anything.

The second part is a while clause. It's optional, too, and defaults to true.

The first part is the increment clause, and is also optional, if there is nothing to increment (or the increment is done in the body or in the while clause.

Your for loop is equivalent to

for(;true;)
{
}

which means that the loop will always be executed again when it ends, unless something causes a break in the body.

That you needed to add this statement implies that there is something wrong with the code that you masked. You need to unmask and fix that problem.