What do I need to combine this two programs?

Hello,

How can I combine 2 programs one after the other? Should I take into account the ({, }) ? I share the two programs:

Conveyor belt program:

void setup() {

  pinMode(2,INPUT); //Definir pin del sensor IR
  Serial.begin(9600); //Posem en marcha el monitor serie
  pinMode(13,OUTPUT);//Definir pin del motor DC (aleatori)
  pinMode(4,OUTPUT);//Definir pin del MOSFET

}

void loop() {
  if(digitalRead(2)==1)
  {
    Serial.println(digitalRead(2)==1);
    digitalWrite(4,HIGH);
    digitalWrite(13,LOW);
    }
    else{
      digitalWrite(13,LOW);
      digitalWrite(4,LOW);
     
    }

}    

Robotic arm program:

#include <Servo.h>

Servo servo_base;
Servo servo_hombro;
Servo servo_codo;
Servo servo_pinza;



void setup()
{
  servo_base.attach(9);
  servo_hombro.attach(6);
  servo_codo.attach(5);
  servo_pinza.attach(11);

}


void loop()
{
     
   
    servo_codo.write(20);
    delay(1000);
    servo_pinza.write(45);
    delay(0);
    servo_hombro.write(165);
    delay(800);
    servo_pinza.write(45);
    delay(1000);
    servo_pinza.write(90);
    delay(500);
    servo_hombro.write(100);
    delay(500);
    servo_codo.write(90);
    delay(500);
    servo_base.write(175);
    delay(1000);

   
    servo_codo.write(20);
    delay(1000);
    servo_hombro.write(145);
    delay(100);
    servo_hombro.write(151);
    delay(100);
    servo_hombro.write(157);
    delay(100);

   
    servo_hombro.write(151);
    delay(500);
    servo_pinza.write(45);
    delay(1500);


    servo_hombro.write(100);
    delay(500);
    servo_codo.write(90);
    delay(500);
    servo_pinza.write(90);
    delay(500);
    servo_base.write(90);
    delay(4000);

}

Start by describing what each sketch does and what you want the combined sketch to do

By the way, what do you think that this line of code does ?

Serial.println(digitalRead(2)==1);

Your Robotic arm program is incomplete

Serial.println(digitalRead(2)==1);

It print on the monitor (1) when there is no object.

Why is my robotic arm incomplete?

Thanks mate!

Before even starting to combine these two sketches, you have to prepare the robotic arm sketch in such away as to eliminate all the delay() statements. While is is a delay() it has no chance of executing any part of the Conveyor belt program.

I beg your pardon, my mistake, but take note of what @6v6gt says about delays and describe what the combined sketch should do

As to

Serial.println(digitalRead(2)==1);

When that line of code is executed you already know that digitalRead(2) is true so why bother reading it again ? That line of code will not be reached unless digitalRead(2) is true anyway

Ohh, so how would that code line be?

Serial.println("whatever you want to print");


I dont understand the error and i dont know how to compact that part of the program. :pensive:

Hello zakboza
Don´t you know the different between read and write, do you?

@zakboza
Please. No more pictures of code

waiting.......

Yes.

I didn't recognize you, but I do remember your conveyor belt!

I took a look through your other posts on this project, and it does not seem you've ever shared any kind of wiring disgram.

Also your questions and reactions indicate that you will need to know more about programming than you already do, like by a factor of 20, before doing this in the one day you have left

  Hello, I am in a project that I must deliver in 4 days.

(from three days ago) would be a reasonable goal.

I suggest you file for an extension of your deadline, relax and get more familiar with some of the concepts being thrown at you here in your effort to combine multiple sketches.

a7

I don't know why the deadline, but you aren't going to meet it. Guaranteed.

Merging two programs into a single sketch is fairly straightforward. Just remember that there can only be one setup() and one loop() function.

The problem that I see is that I have absolutely no description how your program is supposed to work when merged. Your verbose comments are no help either. (/sarcasm).

And as others have said (6v6gt), the delay statements could upset your plans. (Maybe- see my guess below). If you are expecting the belt to stop (or start- the comments aren't clear) in the presence (or absence, based on your well documented comments) of an object. It won't if the arm is in a delay(). As your well descriptive comments show, I don't know what the servo program is doing- I am being sarcastic because without comments we have no clue what you are trying to do. And in a year, neither will you.

I am just guessing, emphasis on guess since you haven't said what the project does, is that you are demonstrating an automatic conveyor. Placing the block on the belt is detected by the IR sensor to turn on the motor. What turns it off? You want to combine two sketches- which one runs first? I am guessing (again) that the arm runs first and the belt runs second. If this is a correct guess, then the delays won't hurt anything now and you just might be able to merge the programs in two days.

If you absolutely can't move your deadline at least a week, then just get another Arduino and run the two sketches independent of each other.

Actually finished :slightly_smiling_face::nerd_face:

Great!

Let's have a look at your code. There are many people lurking for every one posting, it is possible your efforts would be instructive or meaningful to others.

a7

Maybe February 14th :revolving_hearts: is the deadline and it's a very good thing it is going to be met.

a7

Exacto​:grin::pinched_fingers: