Hi I have 2 separate codes that I want to combine!
First code if for toggle button to turn LEDs on/off. Second code is for LEDs to turn on when LDR senses darkness.
I wish for my code to be able to turn LEDs on when its dark but can also be turned on/off with toggle button. Any help would be appreciated!
const int buttonPin = 2; // button is connected to pin
bool buttonState = 0; // variable to hold the button state
bool Mode = 0; // What mode is the light in?
void setup()
{
pinMode(buttonPin, INPUT_PULLUP); // Set the switch pin as input
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
if (!digitalRead(buttonPin)) // cheks if button is pressed (cheks if it's LOW, becouse PULLUP inverts signal: LOW = button pressed)
{
if (!buttonState) // same as: (buttonState == false) ---I made it a bit compact, but it may be confusinf to you
{
buttonState = true;
Mode = !Mode; // this inverts button mode: If Mode was True - it will make it False and viseversa
}
}
else buttonState = false;
// blink LED
digitalWrite(LED_BUILTIN, Mode); // remember: TRUE = HIGH = 1 and FALSE = LOW = 0 (You can use whatever You like)
delay(5);
}
//set pin numbers
//const won't change
const int ledPin = 13; //the number of the LED pin
const int ldrPin = A0; //the number of the LDR pin
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //initialize the LED pin as an output
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
}
void loop() {
int ldrStatus = analogRead(ldrPin); //read the status of the LDR value
//check if the LDR status is <= 10
//if it is, the LED is HIGH
if (ldrStatus <=10) {
digitalWrite(ledPin, HIGH); //turn LED on
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(ledPin, LOW); //turn LED off
Serial.println("---------------");
}
}
How would I possibly combine these? Thank you in advance!