Putting three codes together

That was my teammate, we are working on the same project, sorry for the confusion.

MariaJimjees:
That was my teammate, we are working on the same project, sorry for the confusion.

Same IP address, same domain name.

No confusion here.

Ok well never mind that, do you know something else we can do to make it work.

lukiakat:
Ok well never mind that, do you know something else we can do to make it work.

Haven't you been following this thread?

Yes, I have been following this thread and we weren't able to detect a problem using the debug prints. Also, I went on the sites we were reffered to and...no luck.

lukiakat:
Yes, I have been following this thread and we weren’t able to detect a problem using the debug prints. Also, I went on the sites we were reffered to and…no luck.

I don’t know what to say - you didn’t share the output of the debug prints, so I’ll have to wish you the good luck I don’t believe in. Out.

Hi,

I still can't get it to work.

You still have not explained exactly what it is SUPPOSED to do.

Or what it DOES that it's not supposed to do.

Or what it does NOT do that it's supposed to do.

Or how the Three Functions you sort-of-explained are supposed to coexist. Especially how a hand clap is supposed to be equivalent to two buttons.

Try again?

lukiakat:
Yes, I have been following this thread and we weren't able to detect a problem using the debug prints. Also, I went on the sites we were reffered to and...no luck.

That suggests that you have a newer version of your program that you have not posted for us to see.

Post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different.

...R

lukiakat:
I am new to Arduio and I've been trying to merge three sketches in one main code. I've been working on it for hours and I still can't get it to work. The first code consists of two buttons that control the servo motor and LEDs light up when the servo reaches the correct position, the second is the same but instead of buttons I used two light sensors, and the third is the same but with a sound sensor(I clap). Here is an attempt.

Try changing this line const byte analog_pin = A4; to const byte analog_pin = A6; as you have used Ch-4, 5 (A4, A5) for some other purposes in your program. Also change UNO by NANO as UNO supports only: Ch-0 to Ch-5 and NANO supports Ch-0 to Ch-7.

Did you write those three programs yourself or did you just find them?

Do you know and understand what each one does?

Will you tell us. The point is that you are not cooperating with us to help find your problem. This is either because of a very bad attitude on your part or total lack of code skills on your part. As there are two of you working on this then it looks to me like a school assignment, is that right?

It sounds like you are trying to use a pin for both programs, You must first rewrite each program so that you don't have common pins. That means that each code will run on the same breadboard setup without changing anything.

Once you have done this then my advice would be to just merge two of the programs, and try and understand what you have done.

Finally merge the last code into that.

My tutorial on this is here Merging Code but the key to being successful is in understanding what your code does and how you want it to behave.

Remember using delay in any one code will affect when the other functions start to work. You might even have to write all the three programs again as a single state machine. See my:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

I pulled this from your post of the merged sketches
added serial printing to help debug.
the delay in each line is the sum total of all the delays you have in the sketch.
I also added the note of what revision it matches on the forum threads.
you might want to add more Serial.print( ) and the tab, to see things changing

// revision from post #4  CHANGE THIS as you alter the code

#include <Servo.h>

Servo myservo;  // name servo

int angle = 90;   // initial angle  for servo
int angleStep = 5; //speed
int redLED = 4;
int greenLED = 5;
int servoh = 90;
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
const byte analog_pin = A2;
const byte threshold = 200;
int servo_pin = 9;
int lamp_state = 0;
#define greenButton 2   // pin 2 is connected to green button
#define redButton  3  // pin 3 is connected to red button

void setup() {

 Serial.begin(115200); // need to set the serial monitor for match
 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(greenButton, INPUT_PULLUP); // assign pin 12 as input for Left button.
  pinMode(redButton, INPUT_PULLUP); // assing pin 2 as input for right button.
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  myservo.write(angle);// send servo to the middle at 90 degrees (initial angle)
  pinMode(analog_pin, INPUT);
  myservo.attach(servo_pin);

}

void loop() {
  int analog_value = analogRead(analog_pin);
  
  Serial.print(analog_value); // ++++++++++++++++++++++++++ prints the analog value
  Serial.print("\t");      // prints a tab
  
  int lt = analogRead(ldrlt); // top left
  int rt = analogRead(ldrrt); // top right
  int ld = analogRead(ldrld); // down left
  int rd = analogRead(ldrrd); // down rigt

  int dtime = analogRead(4) / 20; // read potentiometers
  int tol = analogRead(5) / 4;

  int avt = (lt + rt) / 2; // average value top
  int avd = (ld + rd) / 2; // average value down
  int avl = (lt + ld) / 2; // average value left
  int avr = (rt + rd) / 2; // average value right


  int dhoriz = avl - avr;// check the diffirence og left and rigt
  if (digitalRead(redButton) == LOW) {

    if (angle > 20 && angle <= 100) {
      angle = angle - angleStep;
      if (angle < 20) {
        angle = 20;
      } else {
        myservo.write(angle); // move the servo to desired angle
      }
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
    }  delay(5); // waits for the servo to get there
  }
  else if (-1 * tol > dhoriz || dhoriz > tol) { // check if the diffirence is in the tolerance else change horizontal angle
    if (avl > avr) {
      servoh = --servoh;
      if (servoh < 0) {
        servoh = 0;
      }
    }
    else if (avl < avr) {
      ;
      if (servoh > 180) {
        servoh = 180;
      }
    }
    myservo.write(servoh);
  }
  delay(dtime);

  if (digitalRead(greenButton) == LOW) {

    if (angle >= 20 && angle <= 100) {
      angle = angle + angleStep;
      if (angle > 100) {
        angle = 100;
  Serial.print(angle); // +++++++++++++++++++++   prints the angle value     
  Serial.print("\t");      // prints a tab   
  
      } else {
        myservo.write(angle); // move the servo to desired angle
      }
      digitalWrite(greenLED, LOW);
      digitalWrite(redLED, HIGH);
    }
    delay(5);
  }
  else if (analog_value > threshold) {
    lamp_state = !lamp_state;
    if (!myservo.attached()) {
      myservo.attach(9);
      if (lamp_state == 1) {
        delay(500);
        myservo.write(100);      //lamp ON
        delay(250);
        myservo.detach();
        digitalWrite(greenLED, LOW);
        digitalWrite(redLED, HIGH);
      }

      else {
        myservo.write(20);      //lamp OFF
        delay(250);
        myservo.detach();
        digitalWrite(greenLED, HIGH);
        digitalWrite(redLED, LOW);
      }
    }

  }
Serial.println("");      // +++++++++++++++++++++++++++++++++prints a new line
}