/*
Arduino | coding-help
小兔子 [KOSM], — 9:51 PM 9/7/26
hii im really new to coding and i wanted to know how i
could inprove/ fix this code?
im trying to get the ldr to activate the 2 leds when its dark
but im not sure im doing this right
*/
const int LED_CAT_ONE = 11;
const int LED_CAT_TWO = 10;
const int LED_RED_LIGHT = 9;
const int BTN_PIN = 2;
int oldBtnValue = true; // INPUT_PULLUP idles HIGH
int oldPwmVal = -1;
void setup() {
Serial.begin(115200);
pinMode(LED_CAT_ONE, OUTPUT);
pinMode(LED_CAT_TWO, OUTPUT);
pinMode(LED_RED_LIGHT, OUTPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
}
void loop() {
// check button
int btnValue = digitalRead(BTN_PIN);
if (btnValue != oldBtnValue) {
if (btnValue == LOW) {
Serial.println("Button was pressed");
digitalWrite(LED_RED_LIGHT, HIGH);
//digitalWrite(LED_CAT_ONE, LOW);
//digitalWrite(LED_CAT_TWO, LOW);
} else {
Serial.println("Button was released");
digitalWrite(LED_RED_LIGHT, LOW);
//digitalWrite(LED_CAT_ONE, LOW);
//digitalWrite(LED_CAT_TWO, LOW);
}
delay(20); // short debounce delay
oldBtnValue = btnValue;
}
// check pot
int potVal = analogRead(A0);
int pwmVal = map(potVal, 0, 1023, 0, 255);
analogWrite(LED_CAT_ONE, pwmVal);
analogWrite(LED_CAT_TWO, pwmVal);
if (pwmVal != oldPwmVal) {
Serial.print("PWM: ");
Serial.println(pwmVal);
oldPwmVal = pwmVal;
}
}