digitalWrite not working

my digitalWrite and digitalMultiwrite are not working in my bolt iot java script version. the analog readings are perfect. bt i want the led to operate based on the analog values bt its not functioning

Hi,

Could you share the code that you have written, this will help use better understand the issue and help you fix it.

First, confirm the parameters that you have passed are correct and you are using these functions to a button with onclick event. Along with this its mandatory to include the script for it as below. Try it!!

{
sensor_value= analogRead(A0);
b=setMaxValue(50);
setKey(’{{ApiKey}}’,’{{Name}}’);
setChartLibrary(‘google-chart’);
setChartTitle(‘SMOKE DETECTION SENSOR’);
setAxisName(‘time’,‘smoke’);
setCrosshair(true);
setChartType(‘lineGraph’);
plotChart(“time_stamp”,“fire_alarm”);
if(sensor_value<=b)
{
digitalWrite(0,1);
}
else
{
digitalWrite(0,0);
}
}

digitalWrite function passing two parameters ,the GPIO pin number and Value of GPIO. Here value of GPIO will take only two value ‘HIGH’ or ‘LOW’.

Hi @kartikdesai14,

The analog read function in the js library does not return data when you call the function, you have to pass an HTML text element id to the function as the 2nd parameter for it to work properly.

For your code to work, you will have to setup a function which takes the analog data and as per the data does the work. Somewhat like the following.

function callback_func(sensor_value){
b=setMaxValue(50);
setKey(’{{ApiKey}}’,’{{Name}}’);
setChartLibrary(‘google-chart’);
setChartTitle(‘SMOKE DETECTION SENSOR’);
setAxisName(‘time’,‘smoke’);
setCrosshair(true);
setChartType(‘lineGraph’);
plotChart(“time_stamp”,“fire_alarm”);
if(sensor_value<=b)
{
digitalWrite(0,"HIGH");
}
else
{
digitalWrite(0,"LOW");
}
}

You will then need a modified version of the original analog read function to call this callback funciton.

function newAnalogRead(pin) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = JSON.parse(xmlhttp.responseText);
            if(obj.success=="1"){
                 callback_func(obj.value);
            }
        }
    };
    xmlhttp.open("GET",base_url+api_key+"/analogRead?pin="+pin+"&deviceName="+d_name,true);
    xmlhttp.send();
}

You can then just run the new analog read function where you want the work to be done.

newAnalogRead(A0);

The original analogRead function js source code is available in the link below.
https://cloud.boltiot.com/static/js/boltCommands.js

where do i put the second set of codes??

it would mean alot if u will help me solve this issue. i have been working on my project for 21 days with no avail. just tell me where do i put the second set of code

pls tell me everything step by step
instead of js should i make a html document for the same??

Hi @kartikdesai14,

Just replace the earlier code that you had posted with all of the code which I posted.

1 Like

i am working on javascript from bolt cloud only
still not working