Go Down

Topic: Combine two examples (Read 856 times) previous topic - next topic



 I am very new to this (Arduino, Pi and Programming.).

 Here is what I would like to do. I would like to combine two examples. The Ping (http://www.arduino.cc/en/Tutorial/Ping) and the Sweep (http://arduino.cc/en/Tutorial/Sweep) examples into one sketch and one for the Processing side. I have not made it far enough to know if that side works or not.

 I have the Uno, Mini Bread Board, Servo (5MAX ES09A) and the Radio Shack
ultrasonic Range Sensor setup as in the examples. Individuly, They work and I get output as well as movement.

How can I get both of these to work together?

Thank You,




Mar 07, 2015, 10:48 am Last Edit: Mar 07, 2015, 10:49 am by Peter_n
Hi, welcome to the forum.

The ping example has a delay of 0.1 seconds : delay ( 100 ) ;
You can do other things instead of just waiting.

Did you try to combine them ?



Thank you! Here is what I have so far:

/* Fade Blink
  an exercise in combining code

#include <Servo.h>

Servo myservo; 
int pos = 0;   

const int pingPin = 7;

  void setup(){

  void loop(){
void setupSWEEP(){

void setupSENSOR(){

void loopSWEEP(){
  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 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 = 180; pos>=0; pos-=1)     // goes from 180 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

void loopSENSOR(){
  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);
  digitalWrite(pingPin, HIGH);
  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("in, ");


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;


Use code tags!!!

And what happens?

Go Up