Combining 2 working sketches together ( components used : dc motor, push button, i2c display, rotary encoder)



#include <Servo.h> // start of first code for dc motor

Servo myservo;  // create servo object to control a servo


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

void setup() {
  pinMode(2, INPUT_PULLUP);  // enable internal pull-up
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.writeMicroseconds(1500);
  Serial.println("started!");
}

void loop() {

  if (digitalRead(4) == 0) //front
  {
    Serial.println("Forward Pushed!");
    pulse = 2000;
    while (digitalRead(4) == 0);
    Serial.println("Forward Released!");
    delay(100);
  }

  if (digitalRead(3) == 0) //back
  {
    Serial.println("Stop Pushed!");
    pulse = 1500;
    while (digitalRead(3) == 0);
    Serial.println("Stop Released!");
    delay(100);
  }

  if (digitalRead(2) == 0) //back
  {
    Serial.println("Back Pushed!");
    pulse = 1000;
    while (digitalRead(2) == 0);
    Serial.println("Back Released!");
    delay(100);
  }

  else
  {
    myservo.writeMicroseconds(pulse); 
  }    // end of first code

// below is the second code for rotary encoder and i2c display

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
Encoder myEnc(11, 10);
void setup()
{
  Serial.begin(9600);
  Serial.println("Encoder Test");
  pinMode(12, INPUT);
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);
  lcd.print("Tether spool v1.0");
  lcd.setCursor(1, 1);
  lcd.print("Hi Gavin!");
  delay(500);
  lcd.clear();
}

long oldPosition  = -999;
long newPosition = 0;

void loop()
{

  long newPosition = myEnc.read() / 4;

  if (newPosition != oldPosition)
  {
    oldPosition = newPosition;
    Serial.println(newPosition);
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("wire length:");


    lcd.setCursor(1, 1);
    lcd.print(newPosition);
    lcd.setCursor(12, 1);
    lcd.print("mtr");
  }

}  // end of second code

edit the post and remove the explanation from the code tags, please.

Most likely you'll need to talk to your teacher or boss and get a project completion extension.

Two codes with delay() lines and while's like that:

while(digitalRead(4) == 0);

cant be combining without rewriting

Very difficult (read as, impossible) to read your question inside the code tags, so here it is in a more "normal" view:

Did you try the simple things first, and combine the two by copying both setup()s into setup() of a third sketch, ditto the loop()s, and ditto all the stuff outside of both into the are outside both in the third sketch?

For what it's worth, here is what I get when I do that, with the major caveat that I can't even compile it since I don't have that LCD library installed. And if I did, It couldn't test it since I don't have a continuous servo with an encoder.

But maybe it's a start? It's literally a copy/paste from your sketches into a third one. Heed what b707 said, though.

// https://forum.arduino.cc/t/combining-2-working-sketches-together-components-used-dc-motor-push-button-i2c-display-rotary-encoder/1011687
// combined code from above forum thread

// uncompiled (I don't have the LCD library
// and even if it was compiled, it would be untested due to not having servo and encoder

// but maybe it's a start?

#include <Servo.h> // start of first code for dc motor

Servo myservo;  // create servo object to control a servo

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
Encoder myEnc(11, 10);

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

void setup() {
  pinMode(2, INPUT_PULLUP);  // enable internal pull-up
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.writeMicroseconds(1500);
  Serial.println("started!");
  Serial.println("Encoder Test");
  pinMode(12, INPUT);
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);
  lcd.print("Tether spool v1.0");
  lcd.setCursor(1, 1);
  lcd.print("Hi Gavin!");
  delay(500);
  lcd.clear();
}

long oldPosition  = -999;
long newPosition = 0;

void loop() {

  if (digitalRead(4) == 0) //front
  {
    Serial.println("Forward Pushed!");
    pulse = 2000;
    while (digitalRead(4) == 0);
    Serial.println("Forward Released!");
    delay(100);
  }

  if (digitalRead(3) == 0) //back
  {
    Serial.println("Stop Pushed!");
    pulse = 1500;
    while (digitalRead(3) == 0);
    Serial.println("Stop Released!");
    delay(100);
  }

  if (digitalRead(2) == 0) //back
  {
    Serial.println("Back Pushed!");
    pulse = 1000;
    while (digitalRead(2) == 0);
    Serial.println("Back Released!");
    delay(100);
  }

  else
  {
    myservo.writeMicroseconds(pulse);
  }

  long newPosition = myEnc.read() / 4;

  if (newPosition != oldPosition)
  {
    oldPosition = newPosition;
    Serial.println(newPosition);
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("wire length:");


    lcd.setCursor(1, 1);
    lcd.print(newPosition);
    lcd.setCursor(12, 1);
    lcd.print("mtr");
  }

}// loop
1 Like

2 things to think about when writing code that is going to be merged

  1. encapsulation: make the code run in a complete way without global variables trying to keep variables una tight scope. This means one code will less likely interfere with another when merged by eg having two variables with the same name in the same scope.
  2. speed; let the code loop as fast as possible. Remove delays. Rely on a fast loop to check states regularly rather than to wait for a state to change. This means a delay in one code will not prevent another from working properly.

A couple of how-to's

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

https://arduinoinfo.mywikis.net/wiki/CombiningArduinoSketches

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.