const int IR = 12;
const int red = 11;
const int UV = 13;
const int input = A0;
int cc = 0;
int cc2 = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(IR, OUTPUT);
pinMode(UV, OUTPUT);
pinMode(red, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(cc%3 == 0) {
digitalWrite(IR, LOW);
digitalWrite(red, LOW);
digitalWrite(UV, LOW);
if(cc2 == 1) {
digitalWrite(UV, HIGH);
Serial.println(analogRead(input));
cc2++;
}
else if(cc2 == 2) {
digitalWrite(red, HIGH);
Serial.println(analogRead(input));
cc2++;
}
else if(cc2 == 3) {
digitalWrite(IR, HIGH);
Serial.println(analogRead(input));
cc2 = 1;
}
}
else if(cc%2 == 0) {
if(analogRead(input) > 160) {
Serial.println("Recieving....");
}
else {
Serial.println("....");
}
}
delay(1000);
cc++;
}
Welcome to the forum. Try doing proper schematics which will be much easier to see and understand than the Fritzing images. Fritzing I think provide basic schematic functionality but other free software like easyeda or kicad are good or just use a pencil and paper.
Well done for posting the code in tags
Doing this will help you get more help with your projects
Where do you set the pinmode for your sensor?
Look up state machines and switch case for your code
The other leg of the resistor should be connected to GND, not VCC. Please connect the circuit according to the diagram shown here: https://create.arduino.cc/projecthub/electronicsfan123/interfacing-arduino-uno-with-ldr-8760ba
You will be lucky if you did not kill the pins or the LEDs by using the LEDs without current limit resistors.
You can simplify the wiring of the LDR by using the internal pullup resistor available on most pins. Wire the LDR to an analog input and to ground. This works, for me, for most uses. If the sensitivity is not right, then use an external resistor.
Here is simple test code:
const byte ldrPin = A0;
void setup()
{
Serial.begin(115200);
pinMode(ldrPin, INPUT_PULLUP);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 500;
if (millis() - timer >= interval)
{
timer = millis();
int ldrValue = analogRead(ldrPin);
Serial.print("ldr value = ");
Serial.println(ldrValue);
}
}
Output (low light, higher values):
ldr value = 935
ldr value = 922
ldr value = 864
ldr value = 569
ldr value = 189
ldr value = 166
ldr value = 371
ldr value = 243
ldr value = 236
ldr value = 388
Welcom to the forum.
Is that Tinkercad ?
I tried to make your sketch in Wokwi: https://wokwi.com/projects/342234572699730514.
That makes it also easier if others want to test a sketch
After starting the simulation, click on the LDR module to change the value.
Wokwi can not simulate a analog circuit, but they do have a LDR module. By routing the "analog" signal directly into the microcontroller, Wokwi can simulate the "analog" signal.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.