Let's Make Robots!

2WD Robot, ArduinoMega 1280, PanPing sensor problem

Hello everybody. 

First sorry for my English but not good. I want to ask the profis about my robotics problems. :) 
I have a 2wd mobile platform. I played with this so many and my old simple codes was working well. 

But I decided, I want to upgrade a little bit my "stupid" code which was very good with the fixed ping sensor and some IR sensor but too simple. Than I rebuilded all IR sensor first, and builded a little servo for the PING sensor. 

I modificated my code but I don´t understand how can I get a data from the PING sensor when the Servo turned to the left and right direction. ...Maybe I closed to my old code. 

Now If the PING is "feel" something than the right+left motor stopped and the pan servo turn left and right. But the data now from the ping sensor is only the last which was from the first time when the Robot stopped.  

The board is a DFRobot DFRduino Mega 1280, +2A motor shield, +Solar charger shield. The last is only for powering the logic with Li-Po accu. 

The code is made by Arduino v1.0 but nothing special inside. 
Here is the code: http://ielektros.de/uploads/robi1.txt

Old: http://ielektros.de/uploads/robi-fixping1.jpg

Some picture about the hardware:
New: http://ielektros.de/uploads/robi1.jpg

http://ielektros.de/uploads/robi2.jpg

http://ielektros.de/uploads/robi3.jpg

Thanks for every help and spent minutes. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ChuckCrunch's picture

pulseIn instead of digital read

 

void PING()                     //from the arduino exampls
 {
    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);     <<<<<<<<<<<<<<<<<<<<<<<< look hear

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
 
 
 }
  
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;
}

birdmun's picture

you are having issues with saving data from your PING sensor. My suggestion would be to look into arrays, instead of trying to use simple variables to hold your PING responses. An array is a named variable that holds a number of the same type of information and can be called by the same variable name with the difference being that each data point has its own subscript.

EX:

char greeting[5] = {"Hello"};
for (int i = 0; i < 5; ++i) {
    serial.print(greeting[i]);
}

The above 'should' , if I know enough arduiino, print H e l l o to a terminal. I failed to mention earlier that the subscript of arrays start with 0 and end at 1 less than the total. So, even in the above example the subscripts for each character are 0 through 4 and not 1 through 5 or 0 through 5. Watch that or you will run into errors.

As I said earlier, an array variable should allow you to store your data points in a named variable and make it easy to access them. I hope my information has helped, otherwise, someone with a better understanding will have to step in.