Arduino UNO & Arrduino MEGA

Hi guys, I'm kindah stuck in figuring out.
So I have a Ultrasound Ping Sensor that's connected and programmed on Arduino Uno. I've programmed it so if an object is egual or less than 5 inches, it will then send a "HIGH" signal to the MEGA board. if it doesn't, it will then a "LOW" signal to the MEGA board. What I want the Mega board, to do is if the signal that it received is "HIGH", the motor will function but if the signal is "LOW". the motor will stop. The problem is though that the MEGA board doesn't keep on reading the signal from the UNO board. It only reads it once and stays in the "while-loop".

MegaMotor.ino (1.57 KB)

sketch_apr02b.ino (1.88 KB)

It would be much better to include your code within a code block instead of as attachments.

I've only looked at the MegaMotor.ino script. I do see why it gets stuck in the loop. You start the motor loop with a "while (ping == 1)", yet nowhere in the actual loop do you provide a way to change the value of ping. You might want to change that while to an if so you move the motor 1 step, check your ping value, move the motor another step, check your ping value, etc...

BTW... the type-o in your subject makes me think your MEGA is a pirate MEGA. :wink:

Sorry about that....here it is an thank you!!!

UNO:

const int pingPin = 7;          // Input pin of PingPin
const int OutputpingPin = 2;
int inches = 0;
int duration = 0;
int cm = 0;
int val = 0;  

void setup()

{
  Serial.begin(9600);
}

void loop()
{
    
  // 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);
   
  pinMode(OutputpingPin, OUTPUT);
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  
  Serial.print(inches);
  Serial.println("in");
//  Serial.print(cm);
//  Serial.println("cm");
  
  delay(100);
   
  if (inches > 5)
  {
    digitalWrite(OutputpingPin, HIGH);
  }
  else
  {
    digitalWrite(OutputpingPin, LOW);
  }

}

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

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

MEGA:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CONSTANTS

const int OutputLPin = 9;      // Left output to motorshiled
const int InputLPin = 3;   // Left input from RCVR
const int OutputRPin = 8;      // Right output to motorshiled
const int InputRPin = 2;   // Right input from RCVR
int InputPing = 30;

//int Lval = 0;         // variable to store the read value for Left
//int Rval = 0;         // variable to store the read value For Right

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SETUP

void setup()

{
  pinMode(InputPing, INPUT);
  pinMode(OutputLPin, OUTPUT);   // sets the pin as output for Left
  pinMode(OutputRPin, OUTPUT);   // sets the pin as output for Right

// initialize serial communication:
  Serial.begin(9600);

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//INITIAL LOOP


void loop()

{
  int ping = 0;
  ping = digitalRead(InputPing);
  
   
  Serial.print(ping);
  Serial.println(" ");
  
  while (ping == 1)
  {
    int Lval = 0;                    // variable to store the read value for Left
    int Rval = 0;                    // variable to store the read value For Right
    Lval = digitalRead(InputLPin);   // read the input pin
    Rval = digitalRead(InputRPin);   // read the input pin
   
    digitalWrite(OutputLPin, Lval);  
    digitalWrite(OutputRPin, Rval); 
  }
}

Moderator edit: CODE TAGS. Grrrr.

while (ping == 1)
  {
    int Lval = 0;                    // variable to store the read value for Left
    int Rval = 0;                    // variable to store the read value For Right
    Lval = digitalRead(InputLPin);   // read the input pin
    Rval = digitalRead(InputRPin);   // read the input pin
   
    digitalWrite(OutputLPin, Lval);  
    digitalWrite(OutputRPin, Rval); 
  }

The only way you will exit the "while" loop is if "ping" is not equal to one.
But you never read "ping" inside the while loop, so it never exits.