I am using this code to light the LED depending upon signals from proximity sensor . Please help.
BOLTSensor data
Read Sensor on PIN-4if (output == 0 ) {} else {}
I am using this code to light the LED depending upon signals from proximity sensor . Please help.
BOLTAre you using bolt or ARDUINO cause the program
u use are not of HTML but of ARDUINO IDE
this is a very quick and dirty method of doing this but it works
oh and theres a nice 3 second delay before you can see an effect
<!doctype html>
<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>
You can either put this in your file or in the bolt.js file`
function newDigitalRead(pin) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","/digitalRead?pin="+pin,true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
//document.getElementById(element_id).innerHTML = xmlhttp.responseText;
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//document.getElementById("javascript_response").innerHTML = "Javascript Response : "+xmlhttp.responseText;
var obj = JSON.parse(xmlhttp.responseText);
if(obj.success=="1")
{
console.log(obj.value);
newDigitalOutput=obj.value;
}
else{
newDigitalOutput=xmlhttp.responseText;
}
}
};
}`
To use it
newDigitalRead(3);
sensor_val=newDigitalOutput;
The easiest method is to use connect the +ve terminal of the LED to the output terminal of the proximity sensor.
So your setup might look like this:
LED -ve pin - ground LED +ve pin - 4 Prx output pin - 4