user70397: I found this project and it seemed simple but now I don’t know. Please help. I tried changing the code and the wires but nothing happened.
int photoSensorPin = A0;
int redLedPin = 3;
int greenLedPin = 5;
int blueLedPin = 6;
int redLedVal = 0;
int greenLedVal = 0;
int blueLedVal = 0;
boolean isLedOn = false;

int brightness = 1023;
int darkThreshold = 50;
int changeColorTime = 900000;

long lastUpdate=0;

void setup()
{
    randomSeed(analogRead(1));
}

void loop()
{
    getBrightness();
    if (brightness < darkThreshold) {
        if (!isLedOn) {
            LedOn();
            isLedOn = true;
            lastUpdate = millis();
        } else if (millis() > lastUpdate + changeColorTime) {
            LedOn();
            isLedOn = true;
            lastUpdate = millis();
        }
    } else {
        LedOff();
        isLedOn = false;
    }
    delay(1000);
}

int getBrightness()
{
    brightness = analogRead(photoSensorPin);
    return brightness;
}

void LedOn()
{
    redLedVal = random(1,128);
    greenLedVal = random(1,128);
    blueLedVal = random(1,128);

    analogWrite(redLedPin,redLedVal);
    analogWrite(greenLedPin,greenLedVal);
    analogWrite(blueLedPin,blueLedVal);
}

void LedOff(){
    analogWrite(redLedPin,0);
    analogWrite(greenLedPin,0);
    analogWrite(blueLedPin,0);
}

1
2
3
4

user7508: You forgot to set set the pins to output mode. In setup(), add:
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);

If that doesn't work, then you will need some debugging. Add also this
to setup():
Serial.begin(9600);

and this to loop():
Serial.println(brightness);

Look at the values printed. Do they vary significantly when the lighting
conditions change? If not, check the wiring of the LDR (it is fine in
your schematic). Do they go below and above darkThreshold? If not,
then change your threshold.

70397: <p>I found this project and it seemed simple but now I don’t know. Please help. I tried changing the code and the wires but nothing happened.</p>
<pre class="lang-cpp prettyprint-override"><code>int photoSensorPin = A0;
int redLedPin = 3;
int greenLedPin = 5;
int blueLedPin = 6;
int redLedVal = 0;
int greenLedVal = 0;
int blueLedVal = 0;
boolean isLedOn = false;

int brightness = 1023;
int darkThreshold = 50;
int changeColorTime = 900000;

long lastUpdate=0;

void setup()
{
    randomSeed(analogRead(1));
}

void loop()
{
    getBrightness();
    if (brightness &lt; darkThreshold) {
        if (!isLedOn) {
            LedOn();
            isLedOn = true;
            lastUpdate = millis();
        } else if (millis() &gt; lastUpdate + changeColorTime) {
            LedOn();
            isLedOn = true;
            lastUpdate = millis();
        }
    } else {
        LedOff();
        isLedOn = false;
    }
    delay(1000);
}

int getBrightness()
{
    brightness = analogRead(photoSensorPin);
    return brightness;
}

void LedOn()
{
    redLedVal = random(1,128);
    greenLedVal = random(1,128);
    blueLedVal = random(1,128);

    analogWrite(redLedPin,redLedVal);
    analogWrite(greenLedPin,greenLedVal);
    analogWrite(blueLedPin,blueLedVal);
}

void LedOff(){
    analogWrite(redLedPin,0);
    analogWrite(greenLedPin,0);
    analogWrite(blueLedPin,0);
}
</code></pre>
<p><a href="https://i.stack.imgur.com/JYiLk.png" rel="nofollow noreferrer">1</a>
<a href="https://i.stack.imgur.com/O2Rqe.jpg" rel="nofollow noreferrer">2</a>
<a href="https://i.stack.imgur.com/wLxSo.jpg" rel="nofollow noreferrer">3</a>
<a href="https://i.stack.imgur.com/ajrxT.jpg" rel="nofollow noreferrer">4</a></p>
 7508: <p>You forgot to set set the pins to output mode. In <code>setup()</code>, add:</p>
<pre class="lang-cpp prettyprint-override"><code>pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
</code></pre>
<p>If that doesn't work, then you will need some debugging. Add also this
to <code>setup()</code>:</p>
<pre class="lang-cpp prettyprint-override"><code>Serial.begin(9600);
</code></pre>
<p>and this to <code>loop()</code>:</p>
<pre class="lang-cpp prettyprint-override"><code>Serial.println(brightness);
</code></pre>
<p>Look at the values printed. Do they vary significantly when the lighting
conditions change? If not, check the wiring of the LDR (it is fine in
your schematic). Do they go below and above <code>darkThreshold</code>? If not,
then change your threshold.</p>
