Help with merging two codes please

Hello Arduino World :slight_smile:

Please need some help as i'm just newbie and dosen't know what to do

in my university i'm working in a wireless automative car ..

we use adruino, Sensors, Compass, And SkyNav GPS

untill now we have done the code for the Sensors and The compass but seperatelly .. we need now to merge the two codes together but till now we can't

so please anyone help US :~

The first code is

#include <Wire.h>
const int pingPin1 = 3;
const int pingPin2 = 4;
const int pingPin3 = 5;
const int pingPin4 = 6;
int i, headingValue;
int HMC6352Address = 0x42;
int slaveAddress;
boolean ledState = false;
byte headingData[2];
int ledPin = 13;
int M1 = 7;
int M2 = 8;
int E1 = 9;
int E2 = 10;
int E3 = 11;
int E4 = 12;
long cm1,cm2,cm3,cm4;
void setup()
{
  Serial.begin(9600);
  pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
// Shift the device's documented slave address (0x42) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)
slaveAddress = HMC6352Address >> 1;   // This results in 0x21 as the address to pass to TWI
Serial.begin(9600);
pinMode(ledPin, OUTPUT);      // Set the LED pin as output
Wire.begin();
}

void loop()
{
   compass();
  delay(0);
  ping1();
  delay(0);
  ping2();
  delay(0);
  ping3();
  delay(0);
  ping4();
  delay(0);
 
  
  h();
 
 
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
void ping1()
{
  long duration1;
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin1, LOW);
  pinMode(pingPin1, INPUT);
  duration1 = pulseIn(pingPin1, HIGH);
  cm1 = microsecondsToCentimeters(duration1);
  Serial.print("sensor 1 = ");
  Serial.print(cm1);
  Serial.print("cm");
  Serial.println();
}

void ping2()
{
  long duration2;
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);
  cm2 = microsecondsToCentimeters(duration2);
  Serial.print("sensor 2 = ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
}

void ping3()
{
  long duration3;
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);
  pinMode(pingPin3, INPUT);
  duration3 = pulseIn(pingPin3, HIGH);
  cm3 = microsecondsToCentimeters(duration3);
  Serial.print("sensor 3 = ");
  Serial.print(cm3);
  Serial.print("cm");
Serial.println();
}
  void ping4()
{
  long duration4;
  pinMode(pingPin4, OUTPUT);
  digitalWrite(pingPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin4, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin4, LOW);
  pinMode(pingPin4, INPUT);
  duration4 = pulseIn(pingPin4, HIGH);
  cm4 = microsecondsToCentimeters(duration4);
  Serial.print("sensor 4 = ");
  Serial.print(cm4);
  Serial.print("cm");
  Serial.println();
}
void h()
{

if(cm1>100&&cm2>100)
{
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 175); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
delay(0);
}
else 
{
if(cm3>50)
{digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 150); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
analogWrite(E3, 0); //PWM Speed Control
analogWrite(E4, 255); //PWM Speed Control
}
else 
{

The Second one

// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()
{
  // Initialize the serial port.
  Serial.begin(9600);

  Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
  compass = HMC5883L(); // Construct a new HMC5883 compass.
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
  
  Serial.println("Setting measurement mode to continous.");
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
}

// Our main program loop.
void loop()
{
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: 2? 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.0523;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  Output(raw, scaled, heading, headingDegrees);

  // Normally we would delay the application by 66ms to allow the loop
  // to run at 15Hz (default bandwidth for the HMC5883L).
  // However since we have a long serial out (104ms at 9600) we will let
  // it run at its natural speed.
  // delay(66);
}

// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
   Serial.print("Raw:\t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   Serial.print("   \tScaled:\t");
   
   Serial.print(scaled.XAxis);
   Serial.print("   ");   
   Serial.print(scaled.YAxis);
   Serial.print("   ");   
   Serial.print(scaled.ZAxis);

   Serial.print("   \tHeading:\t");
   Serial.print(heading);
   Serial.print(" Radians   \t");
   Serial.print(headingDegrees);
   Serial.println(" Degrees   \t");
}

Please :expressionless:

Moderator edit: CODE TAGS, please.

Please

please, yourself. You have not identified what the combined program is to do, nor have you (apparently) made any attempt to merge them yourself.

Why have you got so many ping functions?

PaulS:

Please

please, yourself. You have not identified what the combined program is to do, nor have you (apparently) made any attempt to merge them yourself.

very sorry PaulS, The Combined programe should let the car move depend on the reading of the sensors and give me the compass reading as well ... i tried to combine it my self but get some errors as you see

sorry again :frowning:

AWOL:
Why have you got so many ping functions?

we have 4 sensor in the car if is that what you ask for

You can have a variable named compass OR a function named compass. You can not have both.

we have 4 sensor in the car if is that what you ask for

So? Functions can take arguments, such as which pin to read from. Functions can return values, instead of writing to global variables. You need ONE ping function that takes an argument and returns a value.

PaulS:
You can have a variable named compass OR a function named compass. You can not have both.

we have 4 sensor in the car if is that what you ask for

So? Functions can take arguments, such as which pin to read from. Functions can return values, instead of writing to global variables. You need ONE ping function that takes an argument and returns a value.

sorry about that PaulS, The problem that the guy who wrote the software for the sensor passed away in car accident and i'm just trying to finish his job .. i search the internet for the compass code and i get it and tested it in the arduino and worked now i hope to try to merge it with the Sensors code to make the car move in specific direction using the compass and the sensor reading in the same time, Any help please with that and sorry for my bad english :frowning: