HOW DO I COMBINE TWO DIFFERENT SKETCHES???

FIRST CODE:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
}

void loop()
{
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);

lcd.setCursor(0,1);
lcd.print("Voltage ");
lcd.print(voltage);

lcd.setCursor(0,0);
lcd.print("Time ");
lcd.print(millis()/1000);
delay(1000);

}

SECOND CODE:

//Motor Control Direction Values
// Direction A (left motor) tells motor A which way to rotate, LeftMotorPower sends the power level for the motor
// Power is between 0-255 (one byte) 0 is off, 255 is max
//CHANGE MOTOR POWER
int LeftMotorDir = 12, LeftMotorPower = 3;
int RightMotorDir = 13, RightMotorPower = 11;

// Decleration for two analog input light sensors
int left_light = A0, right_light = A1;

// Light Sensor Calibration Values
// These you need to set manually (use the LCD to read the values). Make sure to give yourself a buffer
// If line reads 999, may use something like ~925 for your dark value (so you don't miss it).
//CHANGE LIGHT SENSOR CALIBRATION VALUES
int dark = 870;

// Motor maximum and minimum speeds
// Adjust these within the 0-255 range to try and make your bot faster
//CHANGE MOTOR MIN AND MAX SPEEDS
int motor_max = 255, motor_min = 0;

//DON"T CHANGE ANYTHING BELOW THIS LEVEL

int ms_per_cycle = 10; // Milliseconds per cycle -> 10ms would yield loop speed of 100Hz
unsigned long previous_time = 0; // This is updated each time the motors are updated to make sure everything runs exactly at desired loop speed

void setup() {
pinMode(LeftMotorDir, OUTPUT);
pinMode(RightMotorDir, OUTPUT);
pinMode(LeftMotorPower, OUTPUT);
pinMode(RightMotorPower, OUTPUT);
}

void loop() {
if (millis() - previous_time >= ms_per_cycle) // if time since last motor update is >= desired delta t then update motors
{
previous_time = millis(); // Update the cycle timer

int right_reading = analogRead(right_light); // Read from the two light sensors
int left_reading = analogRead(left_light);

if (right_reading >= dark && left_reading < dark) // See the line on the right side of the robot.
{ // Need to turn the robot right to keep the line over the robot.
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_min); // Max power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_max); // Minimum power for right motor
}
else if (right_reading < dark && left_reading >= dark) // See line on the left side of the robot
{
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_max); // Min power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_min); // Max power for right motor
}
else if (right_reading < dark && left_reading < dark) // Neither sensor sees line, must be straddled... Go straight
{
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_max); // Max power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_max); // Max power for right motor
}
else
{
// Both sensors must see black or there is a problem. Turning motors off
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, 0); // Turning motor off
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, 0); // Turning motor off
}
} // End of cycle timer if statement

// If the if statement is false (hasn't been enough time since last motor update) then the program will simply skip the if statement
// and loop around very very quickly.
}

In principle, you put everything above setup() from both programs above setup() in the combined program. Then everything inside the 2x setups() into the combined setup(), and everything inside the 2x loops() into a combined loop().

You need to check that you haven't duplicated any variable names in both sketches, and make changes as appropriate. Similarly, check you haven't use any pins twice.

JimboZA:
In principal, you put everything above setup() from both programs above setup() in the combined program. Then everything inside the 2x setups() into the combined setup(), and everything inside the 2x loops() into a combined loop().

You need to check that you haven't duplicated and variable names in both sketches, and make changes as appropriate. Similarly, check you haven't use any pins twice.

Ive done so and it did infact compile, but the line sensors didn't seem to function.

Here:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);

// SCE101 HW#11 - Line Following Automated Car (Dr. Travis Fields and Jessica Zhang)

//Motor Control Direction Values
// Direction A (left motor) tells motor A which way to rotate, LeftMotorPower sends the power level for the motor
// Power is between 0-255 (one byte) 0 is off, 255 is max
//CHANGE MOTOR POWER
int LeftMotorDir = 12, LeftMotorPower = 3;
int RightMotorDir = 13, RightMotorPower = 11;

// Decleration for two analog input light sensors
int left_light = A0, right_light = A1;

// Light Sensor Calibration Values
// These you need to set manually (use the LCD to read the values). Make sure to give yourself a buffer
// If line reads 999, may use something like ~925 for your dark value (so you don't miss it).
//CHANGE LIGHT SENSOR CALIBRATION VALUES
int dark = 870;

// Motor maximum and minimum speeds
// Adjust these within the 0-255 range to try and make your bot faster
//CHANGE MOTOR MIN AND MAX SPEEDS
int motor_max = 255, motor_min = 0;

//DON"T CHANGE ANYTHING BELOW THIS LEVEL

int ms_per_cycle = 10; // Milliseconds per cycle -> 10ms would yield loop speed of 100Hz
unsigned long previous_time = 0; // This is updated each time the motors are updated to make sure everything runs exactly at desired loop speed

void setup() {
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
}
{
pinMode(LeftMotorDir, OUTPUT);
pinMode(RightMotorDir, OUTPUT);
pinMode(LeftMotorPower, OUTPUT);
pinMode(RightMotorPower, OUTPUT);
}
}

void loop() {

{
int sensorValue = analogRead(A2);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);

lcd.setCursor(0,1);
lcd.print("Voltage ");
lcd.print(voltage);

lcd.setCursor(0,0);
lcd.print("Time ");
lcd.print(millis()/1000);
delay(1000);

}
if (millis() - previous_time >= ms_per_cycle) // if time since last motor update is >= desired delta t then update motors
{
previous_time = millis(); // Update the cycle timer

int right_reading = analogRead(right_light); // Read from the two light sensors
int left_reading = analogRead(left_light);

if (right_reading >= dark && left_reading < dark) // See the line on the right side of the robot.
{ // Need to turn the robot right to keep the line over the robot.
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_min); // Max power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_max); // Minimum power for right motor
}
else if (right_reading < dark && left_reading >= dark) // See line on the left side of the robot
{
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_max); // Min power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_min); // Max power for right motor
}
else if (right_reading < dark && left_reading < dark) // Neither sensor sees line, must be straddled... Go straight
{
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, motor_max); // Max power for left motor
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, motor_max); // Max power for right motor
}
else
{
// Both sensors must see black or there is a problem. Turning motors off
digitalWrite(LeftMotorDir, HIGH); // Left motor forward
analogWrite(LeftMotorPower, 0); // Turning motor off
digitalWrite(RightMotorDir, HIGH); // Right motor forward
analogWrite(RightMotorPower, 0); // Turning motor off
}
} // End of cycle timer if statement

// If the if statement is false (hasn't been enough time since last motor update) then the program will simply skip the if statement
// and loop around very very quickly.
}

LiquidCrystal lcd(12,11,5,4,3,2);

int LeftMotorDir = 12, LeftMotorPower = 3; 
int RightMotorDir = 13, RightMotorPower = 11;

You use the same pins for different purposes? I doubt that will work.

dannable:

LiquidCrystal lcd(12,11,5,4,3,2);

int LeftMotorDir = 12, LeftMotorPower = 3;
int RightMotorDir = 13, RightMotorPower = 11;




You use the same pins for different purposes? I doubt that will work.

I am a beginner in programming, how can I resolve this issue?

You need to work out which ones you can move, probably the lcd ones, and select other pins instead. The amend your sketch to reflect the new pin numbers and move your wires across.

dannable:
You need to work out which ones you can move, probably the lcd ones, and select other pins instead. The amend your sketch to reflect the new pin numbers and move your wires across.

dannable:
You need to work out which ones you can move, probably the lcd ones, and select other pins instead. The amend your sketch to reflect the new pin numbers and move your wires across.

I got it to work! Thanks a lot!