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?
#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);
}
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);
}
}