Pages: [1]   Go Down
Author Topic: 2 codes, want them to work together  (Read 384 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 2 separate sketches (which are both working well), however they are supposed to be in just one sketch. Don’t know how to combine them together.

I’m using: 1 LED, 1 light sensor, 1 push button, 1 servo motor.

What I’m trying to do is:   push button to turn servo from 0 to 180,  and at the same time turning the LED on. Then push button again to turn servo back from 180 to 0, and at the same time turn LED off.  Then, whenever the LED is off , the light sensor has to work. The light sensor turns LED on when there’s an object close to it.

So I have the code for the (push.button+servo+LED) on one file, and the code for the (light sensor+LED) on another.
Here are the two sketches, they’re working fine separately, but how can I combine them into one?

Code:
#include <Servo.h>

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

const int buttonPin = 2;      // the pin that the pushbutton is attached to
const int ledPin    = 5;      // the pin that the LED is attached to

int buttonState     = 0;      // current state of the button
int lastButtonState = 0;      // previous state of the button
int ledState        = 0;      // remember current led state
int pos             = 0;      // variable to store the servo positions

void setup()
{
  myservo.attach(8);          // attaches the servo on pin 8 to the servo object
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
}

void loop()
{
  buttonState = digitalRead(buttonPin); // read the pushbutton input pin
    if (buttonState != lastButtonState) // compare buttonState to previous state
    {
      if (buttonState == 1)
      {
        if(ledState == 1)
          {
            ledState = 0;
            for(pos = 0; pos <= 180; pos += 5)    // goes from 0 degrees to 180 degrees
            {                                    // in steps of 5 degrees
            myservo.write(pos);                  // tell servo to go to position 'pos'
            delay(50);        // waits 50ms for the servo to reach the position
            }
          }
        else 
          { 
            ledState = 1;
            for(pos = 180; pos >= 1; pos -= 5)    // goes from 180 degrees to 0 degrees
            {                               
            myservo.write(pos);                  // tell servo to go to position 'pos'
            delay(50);                           // waits 50ms for the servo to reach the position
            }
          }
      }
     
    lastButtonState = buttonState; // remember the current state of the button
    }
  // turns LED on if the ledState =1 or off if ledState = 0
  digitalWrite(ledPin, ledState);
  myservo.write(pos);          // goes from 0 degrees to 180 degrees

  delay(20);
}


Code:
int LDR = 0;                                //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;                    //that’s a variable to store LDR values
int light_sensitivity = 500;  //This is the approx value of light surrounding your LDR
 
void setup()
  {
    Serial.begin(9600);            //start the serial monitor with 9600 buad
    pinMode(5, OUTPUT);     //we mostly use13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
  }
 
void loop()
  {
    LDRValue = analogRead(LDR);          //reads the ldr’s value through LDR which we have set to Analog input 0 “A0″
    Serial.println(LDRValue);                  //prints the LDR values to serial monitor
    delay(50);                                                //This is the speed by which LDR sends value to arduino
 
    if (LDRValue < light_sensitivity)
      {
        digitalWrite(5, LOW);
      }
    else
      {
        digitalWrite(5, HIGH);
      }
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but how can I combine them into one?
You start with defining the requirements for the combined sketch. Then, you determine which parts of sketch A contribute to those requirements, and you copy them into sketch C. Then, you determine which parts of sketch B contribute to those requirements, and you copy them into sketch C.
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 82
Posts: 3865
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And you will need to rationalise in places... in the top sketch you refer to pin 5 by a name and in the bottom one by number. You'll need to tidy that kind of thing. Are those two pin 5's even meant to be the name pin in the final sketch, or for example would one need to become pin 6?

I'm an advovate of simple flowcharts to map out what a program should do, and that would help you bolt the two sketches together.
Logged

The Elders of the Internet know who I am.....

Pages: [1]   Go Up
Jump to: