LED control using LDR

I want to control a LED using the value of the LDR.
**Any way I can do that? **

I tried to modify the code >> About using LED sensor to depict result of proximity sensor

but it doesnt work.
Any help is useful.

Hmm… :worried: That code isn’t very intuitive. Could you share what you’ve written? If necessary the with comments to tell us what you were trying to do.

In the mean time here’s something you could use this:

/*Circuit: .*1 LED, cathode connected to LOW and anode on PIN_4 (PIN_A2 if you want brightness control) .*LDR connected as per tutorial (Assuming input pin is PIN_A3.) .*/ var LDR_State = function( ) { ....return analogRead( "A3" ); } var SetLED = function( ) { ....//value current just switches the LED on and off. No brightness control ....var value; ....if ( LDR_State() == 0 ) { value = 'LOW'; } ....else { value = 'HIGH'; } ....digitalWrite( "4", value ); /* Use the following code for brightness control. ....analogWrite( "A2", LDR_State() );
My JavaScript’s gone a bit rusty, so there might be a bug or two in the above.:sweat_smile: But I’ll check back again tomorrow. So feel free to ask.:grin:

    <html>
    <head>
    <title>BOLT</title>
    <script type="text/javascript" src="/serveFile?filename=bolt.js"> setDebug(true);  </script>	
    <script>

        var output;
        var sensor_output;
        var sensor_output_array;
        var sensor_val;

        function start(){
    //the format of the digital or analog read provided in bolt.js is "Pin Value=x"
	// so instead of just writing my own ajax request or editing the js file like a normal person
	// i decided to do it like coz i was lazy
            digitalRead(3,"hidden");
            sensor_output= document.getElementById("hidden").innerHTML;
            sensor_output_array = sensor_output.split("=");
            sensor_val=sensor_output_array[1];

            if(sensor_val==0){
                document.getElementById("light").innerHTML ="something detected";
                digitalWrite(5,HIGH);
            }
            else{
                document.getElementById("light").innerHTML ="all clear";
                digitalWrite(5,LOW);

            }
            
    document.getElementById("output").innerHTML =sensor_val;

    //anything less than 2.5 seconds causes alot of problems for me.
// it hangs the board and i gotto restart it.
    //im assuming its because it take that long to handle one request but i might be wrong.
    //if anyone has a reason for this i would like to know
            setTimeout(start,3000);
    }


    </script>

    </head>
    <body bgcolor="#01FFFF" > 
    <center>
    <h3> Sensor data</h3>
    <span id="hidden" style="display: none"> </span>
    <span id="output"> </span>
    <p id="light"></p></center>
    <button onclick="start();">ON</button>
    </body> 
    </html>

Umm…Where do I use that code? Should I include it in the javascript part?
And how do I use this code with buttons?
Sorry i`m new to Javascript

That’s the direct .htm file that you need to upload. No fooling around, it means business.

You use buttons line this <button onclick="name_of_JS_function()">NAME_OF_BUTTON</button>

And lastly, I’d recommend 'Codecademy and W3Schools’ or ‘The Mozilla Developer Network’ to learn the basis of web programming.