Combining Two Codes (need help)

Thread Starter

Zane Finner

Joined Jan 29, 2018
30
Okay, so what I want to do is allow my servo to rotate my ultrasonic range detector. Also, and LCD shows the range.

Here are my two codes:

Code:
/
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(8);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  delay(1000);
  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);
  }
  delay(1000);
  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
  }
}
Code:
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystalLCD(10,9,5,4,3,2);//Create Liquid Crystal Object called LCD

inttrigPin=13;//Sensor Trip pin connected to Arduino pin 13
intechoPin=11;//Sensor Echo pin connected to Arduino pin 11
intmyCounter=0;//declare your variable myCounter and set to 0
intservoControlPin=6;//Servo control line is connected to pin 6
floatpingTime;//time for ping to travel from sensor to target and return
floattargetDistance;//Distance to Target in inches
floatspeedOfSound=776.5;//Speed of sound in miles per hour when temp is 77 degrees.

voidsetup(){
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
LCD.begin(16,2);//Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);//Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");//Print Message on First Row
}

voidloop(){
digitalWrite(trigPin,LOW);//Set trigger pin low
delayMicroseconds(2000);//Let signal settle
digitalWrite(trigPin,HIGH);//Set trigPin high
delayMicroseconds(15);//Delay in high state
digitalWrite(trigPin,LOW);//ping has now been sent
delayMicroseconds(10);//Delay in high state
pingTime=pulseIn(echoPin,HIGH);//pingTime is presented in microceconds
pingTime=pingTime/1000000;//convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime=pingTime/3600;//convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance=speedOfSound*pingTime;//This will be in miles, since speed of sound was miles per hour
targetDistance=targetDistance/2;//Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance=targetDistance*63360;//Convert miles to inches by multipling by 63360 (inches per mile)
LCD.setCursor(0,1);//Set cursor to first column of second row
LCD.print(" ");//Print blanks to clear the row
LCD.setCursor(0,1);  //Set Cursor again to first column of second row
LCD.print(targetDistance);//Print measured distance
LCD.print(" inches");//Print your units.
delay(250);//pause to let things settle
}
Obviously, I don't know how to code and I don't have the patience yet. I just need to know how to combine these two codes for a singular outcome.
Please either tell me how or allow me to copy and paste a combined version that you post

Thanks, I'm still a noob so sorry for bothering you pro's
 
Last edited:
You may get annoyed at my response, and if you do, so be it.

"Obviously, I don't know how to code and I don't have the patience yet. I just need to know how to combine these two codes for a singular outcome."

It is not patience that you need, it is the realization that you will have to spend the time and effort to learn what you need to learn to get where you want to go.

"Please either tell me how or allow me to copy and paste a combined version that you post"

If those are my only two choices, I will pick the former.

1. Figure out exactly what you want to do and state it concisely.

2. Understand what each program is doing. Evaluate if they address point 1 above.,

3. Figure out what is being done in the setup() function of each program and learn how you can combine them as is, or whether you need to do some modifications.

4. Change each of the main() functions to separate functions.

5. Incorporate those new functions into your new main() according to 1. above.

6. Post again with the results of your time and effort if you encounter some problems. Also, state outright if this is homework.

I think you will find many people on here are very willing to go through a lot of time and effort to help you, but I'm not sure you will easily find someone that will do your work for you. Even I recently re-wrote a program for someone that, coincidentally, bears a striking similarity to the servo code in your post (see https://forum.allaboutcircuits.com/...o-motor-using-rtc-module.146877/#post-1250842). I regret my effort, because there was not even the courtesy of a response.

"Thanks, I'm still a noob so sorry for bothering you pro's"

You're welcome, no bother at all.
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,225
If you lack the patience to devote time and energy to learning, then it would be best for you to abandon this endeavor. Time and pressure are the only way to achieve results. There are no shortcuts, and no amount of having other people do the work for you is going to advance your cause. Take a deep breath, resign yourself to making a commitment, and get to work.
 
Top