Problem with code for autonomous barbie jeep

I’m working on a project for a school club for an autonomous barbie jeep and im the only one that understands the code the most. We have two different ultrasonic sensors one of the PING sensors and an i2c SRF10 sensor and a servo. I’ve got the program to read both sensors and light up an led but now I’m trying to move the servo different ways when one of the sensors detects something 12 inches in front of the jeep. The code compiles fine and uploads to the board but with the servo in the code the sensors stop reading and now i don’t know whats wrong with the code.

#include <Wire.h>


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

const int pingPin = 7;

void setup()
{
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          // start serial communication at 9600bps
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
}

int reading = 0;

void loop()
{
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(112); // transmit to device #112 (0x70)
                               // the address specified in the datasheet is 224 (0xE0)
                               // but i2c adressing uses the high 7 bits so it's 112
  Wire.send(byte(0x00));      // sets register pointer to the command register (0x00)  
  Wire.send(byte(0x50));      // command sensor to measure in "inches" (0x50)
                               // use 0x51 for centimeters
                               // use 0x52 for ping microseconds
  Wire.endTransmission();      // stop transmitting

  // step 2: wait for readings to happen
  delay(70);                   // datasheet suggests at least 65 milliseconds

  // step 3: instruct sensor to return a particular echo reading
  Wire.beginTransmission(112); // transmit to device #112
  Wire.send(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

  // step 5: receive reading from sensor
  if(2 <= Wire.available())    // if two bytes were received
  {
    reading = Wire.receive();  // receive high byte (overwrites previous reading)
    reading = reading << 8;    // shift high byte to be high 8 bits
    reading |= Wire.receive(); // receive low byte as lower 8 bits
    Serial.println(reading);   // print the reading
  }

  delay(250);                  // wait a bit since people have to read the output :)


if (reading <= 12 && reading > 1)
{ 
  for(pos = 0; pos < 45; pos -= 1)  // goes from 0 degrees to 45 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 45; pos>=1; pos+=1)     // goes from 45 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

/*===============================================================
=================================================================
=================================================================
=================================================================
===============================================================*/




  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  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);
  
  
  
  
  if (inches <= 12)
{ 
  for(pos = 0; pos < 45; pos += 1)  // goes from 0 degrees to 45 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 45; pos>=1; pos-=1)     // goes from 45 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  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;
}

/*
// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
// usage: changeAddress(0x70, 0xE6);

void changeAddress(byte oldAddress, byte newAddress)
{
  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(byte(0xA0));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(byte(0xAA));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(byte(0xA5));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(newAddress);
  Wire.endTransmission();
}

*/

Moderator edit: When posting code, please use the # icon on the editor’s toolbar to put the code into a code box. It is also polite to remove code that is commented-out and excessive whitespace, to reduce the length of the post.

if (reading <= 12 , reading > 1)

Sorry, it may compile, but it ain’t right.
Try “&&”, not “,”

for(pos = 0; pos < 45; pos -= 1)  // goes from 0 degrees to 45 degrees

Read the code, read the comment; try to figure a way to make them agree.

Thank you I did what you said and know the sensors read with the servo in the program but now once the SRF10 sensor reads with in those parameters set for it the program just stops but when the PING sensor reads within its parameter the program keeps going and now I'm stuck again. At least I'm farther ahead than I was.

I doubt the program stops. Maybe you could post your code.

Good point what I meant by stops is the sensors stop when the SRF10 sensor reads with in those parameters. The code is the same as before except I changed the “,” to “&&”.

#include <Wire.h>


#include <Servo.h> 
 
Servo myservo;  
               
 
int pos = 0;    


const int pingPin = 7;


void setup()
{
  Wire.begin();                
  Serial.begin(9600);          
  
  myservo.attach(9);  
}

int reading = 0;

void loop()
{

  Wire.beginTransmission(112); 
                               
  Wire.send(byte(0x00));      
  Wire.send(byte(0x50));      
                               
  Wire.endTransmission();      
  delay(70);                   
  Wire.beginTransmission(112); 
  Wire.send(byte(0x02));     
  Wire.endTransmission();    


  Wire.requestFrom(112, 2);    
  
if(2 <= Wire.available())    
  {
    reading = Wire.receive();  
    reading = reading << 8;    
    reading |= Wire.receive(); 
    Serial.println(reading);   
  }

  delay(250);                  


if (reading <= 12 && reading > 1)
{ 
  for(pos = 0; pos < 45; pos -= 1)   
  {                                  
    myservo.write(pos);              
    delay(15);                      
  } 
  for(pos = 45; pos>=1; pos+=1)     
  {                                
    myservo.write(pos);             
    delay(15);                       
  } 

}


  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  if (inches <= 12)
{ 
  for(pos = 0; pos < 45; pos += 1)  
  {                                 
    myservo.write(pos);          
    delay(15);                      
  } 
  for(pos = 45; pos>=1; pos-=1)    
  {                                
    myservo.write(pos);             
    delay(15);                       
  } 
}
   
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);

}


long microsecondsToInches(long microseconds)
{
  
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  
  return microseconds / 29 / 2;
}

/*

// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
// usage: changeAddress(0x70, 0xE6);

void changeAddress(byte oldAddress, byte newAddress)
{
  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(byte(0xA0));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(byte(0xAA));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(byte(0xA5));
  Wire.endTransmission();

  Wire.beginTransmission(oldAddress);
  Wire.send(byte(0x00));
  Wire.send(newAddress);
  Wire.endTransmission();
}

*/

This doesn’t look good:

if (reading <= 12 && reading > 1)
{ 
  for(pos = 0; pos < 45; pos -= 1)   
  {                                  
    myservo.write(pos);              
    delay(15);                      
  } 
  for(pos = 45; pos>=1; pos+=1)     
  {                                
    myservo.write(pos);             
    delay(15);                       
  } 
}

The first loop will tell the servo to go to position 0, then -1 then -2… It’s unlikely to appreciate that.
Similarly, the second loop tells it to go to position 45, 46, 47…999,1000 etc. The loops you use for the other sensor would work better. If you want the servo to behave differently for this sensor, put the second loop before the first.

That was also pointed out in reply #1

Thank you both and sorry AWOL i didn't completely understand what you meant before but now it's clear and now it keeps on reading and doesn't stop.

I have found it helps to build my programs gradually, testing as I go. So, for example, I put in print statements to make sure things, such as loops, are actually doing what I thought. I also check that each part works with conditions at their limits. I do this continuously as I write my program. When things fail I then have a fairly good idea of what I did that caused the problem. This approach may seem slow but it gives confidence in each part of the program. The alternative is to write the whole thing in one go then scratch your head when it does not work, of even worse works most of the time.