Sensor

I made a function to read a sensor and to adjust de light of a RGB LED. but it’s not working and I don’t find the problem

`
redPin <- hardware.pin1; // R of RGB
greenPin <- hardware.pin2; // G of RGB
bluePin <- hardware.pin5; // B of RGB
redPin.configure(PWM_OUT, 1.0/200, 0);
greenPin.configure(PWM_OUT, 1.0/200, 0);
bluePin.configure(PWM_OUT, 1.0/200, 0);
lightreading <- hardware.pin9
lightreading.configure(ANALOG_IN);

light <- 0

function setSensor(ledState)
{
if(ledState){
function readSensor()
{
light=lightreading.read();
local value = light / 65535.0;
redPin.write(value);
greenPin.write(value);
bluePin.write(value);
server.log(format(“Lightness is:” +value));

imp.wakeup(0.5, readSensor);

}
readSensor();
}else
redPin.write(ledState);
greenPin.write(ledState);
bluePin.write(ledState);
}

agent.on(“led”,setSensor);
`

Please Help!!!

Separating out readSensor() will make your code easier to follow - put it before senSensor(), which then becomes:

function setSensor(ledState) { if (ledState) { readSensor() } else { redPin.write(ledState); greenPin.write(ledState); bluePin.write(ledState); } }

and

`function readSensor()
{
light=lightreading.read();
local value = light / 65535.0;
redPin.write(value);
greenPin.write(value);
bluePin.write(value);
server.log(format(“Lightness is:” +value));

imp.wakeup(0.5, readSensor);

}`

Now, I’m not sure why you need the else code, because unless ledState is initially zero, it will never be called. If ledState is zero, then readSensor() will never be called, unless you are sending the “led” message from the agent repeatedly. If you are doing so, then the imp.wakeup() call is probably unnecessary.

As it stands, if ledState is not zero, readSensor() is called and then calls itself every half a second. Assuming your code is getting to this point, it will continue to read the analogue value and write it to the RGB pins every 0.5s. The RGB values are the same, so the LED will go from black to white.

Since the code is doing what it should, you need to check your circuits.

PS. I woul also change 1.0/200 to 0.005 to prevent Squirrel doing the sum and giving an integer as the answer.

I am sending a “led” message from the agent
I am doing a project, with a web interface, with some buttons, like turn on the light or turn off the light, and i want to activate the light sensor when i push the button sensor/on, and to dezactivate the sensor when i push the button sensor/off. when i push the button on it’s working, but when i push the button off it’s not turning off the sensor.

and if I’m not using " imp.wakeup(0.5, readSensor);" the code it’s not working, I don’t know why but in the device log it’s not showing the server.log(format(“Lightness is:” +value));, I use this line of code to see if the sensor it’s working and to see what value is reading.

Every time readSensor is called, it ends up by scheduling another call to readSensor. So if it’s been called once, there’s a perpetual chain of readSensor() calls set up. But then whenever setSensor(1) is called, readSensor() is called again. So now you’ve got two perpetual chains of readSensor() calls. Pretty soon the imp will be overwhelmed and will restart.

It’s more likely that what you want is to set a global variable in setSensor(), which is picked up whenever the next readSensor runs.

Peter

`
function setSensor(ledState)
{
status=ledState;
if (status)
{
readSensor();
}
else
{
redPin.write(0);
greenPin.write(0);
bluePin.write(0);
}
}

function readSensor()
{
light=lightreading.read();
local value = 1- (light / 65535.0);
redPin.write(value);
greenPin.write(value);
bluePin.write(value);
server.log(format(“Lightness is:” +light));
}
`

I modified the function but it’s not working when i try to turn off the sensor, the sensor read’s the light intensity

Peter how can i do a global variable, please give me an example. I have to finish a project and this is the final problem witch I have to resolve

Please help!!!

Global variables are:

[global variable name] <- [value]

Where is the value ledState coming from? I know it is being sent by the agent, but what is it? In the agent, you should have something like this:

device.send(“led”, some_value)

Where are you getting some_value?

The value of some_value is ultimately determining (through setSensor()) whether the sensor is read of the RGD LED turned off.

`
if (“sensor” in request.query) {
if ((request.query.sensor == “1”) || (request.query.sensor == “0”))
{
// convert the led query parameter to an integer
local sensorStatus = request.query.sensor.tointeger();

            // send "led" message to device, and send ledState as the data
            device.send("sensor", sensorStatus); 
        }
    }

`

`

status<-0;
function setSensor(ledState)
{
status=ledState;
if (status)
{
readSensor();
}
else
{
redPin.write(0);
greenPin.write(0);
bluePin.write(0);
}
}

function readSensor()
{
light=lightreading.read();
local value = 1- (light / 65535.0);
redPin.write(value);
greenPin.write(value);
bluePin.write(value);
server.log(format(“Lightness is:” +light));
}
`

this program is reading a single value from the sensor and then it’s not reading another value.

if I put imp.wakeup(0.5,readSensor) when i give the command sensor/off from the interface the program it’s not stoping from reading the light sensor, the program blocks the IDE and i can’t do nothing to resolve this problem. I don’t know how

help!!!

I don’t think you want to put an imp.wakeup function in there. That will loop the function every 500 milliseconds. I think you just want to change the color/brightness and take a reading when you send an HTTP request, right? Can you paste exactly what you have in the agent and the device right now so we can help troubleshoot?

BTW, if you have an iPhone or iPad, take a look at my app Pitchfork… it has a color picker that I made exactly for this. I also have some jquery slider code if you just want to use a browser.

this is what I have in agent
`

// At the start, print a message to say we’re online, and print the agent URL:
server.log("LED Web Control Agent Online: " + http.agenturl());

http.onrequest(function (request, response) {
response.header(“Access-Control-Allow-Origin”, “*”);
response.header(“Access-Control-Allow-Headers”,“Origin, X-Requested-With, Content-Type, Accept”);
response.header(“Access-Control-Allow-Methods”, “POST, GET, OPTIONS”);

try { // Try provides us with exception handling, in case a runtime error occurs

    // check if the user sent led as a query parameter
    if ("led" in request.query) {
        // if they did, and led=1.. set our variable to 1
        if ((request.query.led == "1") || (request.query.led == "0"))
        {
            // convert the led query parameter to an integer
            local ledStatus = request.query.led.tointeger();

            // send "led" message to device, and send ledState as the data
            device.send("led", ledStatus); 
        }
    }
    
    
         if ("sensor" in request.query) {

        if ((request.query.sensor == "1") || (request.query.sensor == "0"))
        {
           
            local sensorStatus = request.query.sensor.tointeger();

         
            device.send("sensor", sensorStatus); 
        }
    }
    // check if an "rgb" query was received:
    if ("rgb" in request.query) {
        // colors are sent as a string, we've got to do some work to convert
        // them to a number, which is eventually what we'll need to do
        // pwm on our RGB led pins.
        local color = request.query.rgb; // get the query into a variable
        if (color[0] == '#') { // The request should start with '#' (%23)
            // We'll construct a table with three parameters: r, g, and b
            // Do some work to convert r, g, and b from ASCII characters
            // to 0-255 values.
            local returnTable = {
                r = ASCIItoHex(color[1])*16 + ASCIItoHex(color[2])
                g = ASCIItoHex(color[3])*16 + ASCIItoHex(color[4])
                b = ASCIItoHex(color[5])*16 + ASCIItoHex(color[6])
            };
            device.send("rgb", returnTable); // send our color table to the imp
        }
    }
    // check if a "timer" query was received:
    if ("timer" in request.query) {
        // convert to an integer, and pass it out to the imp.
        device.send("timer", request.query.timer.tointeger());
    }
    // send a response back saying everything was OK.
    response.send(200, "OK");
} 
catch (ex) {
    response.send(500, "Internal Server Error: " + ex);
}

});

// This function converts an ASCII character to a number
function ASCIItoHex(colorNibble) {
if ((colorNibble >= ‘0’) && (colorNibble <= ‘9’)) {
return colorNibble - 48;
}
else if ((colorNibble >= ‘a’) && (colorNibble <= ‘f’)) {
return colorNibble - 87;
}
else if ((colorNibble >= ‘A’) && (colorNibble <= ‘F’)) {
return colorNibble - 55;
}
}

`

i need to know how do i make this program to work
`
status<-0;
light<-0;
function setSensor(ledState)
{
status=ledState;
if (status) //status=1
{
readSensor();
}
else // status=0
{
redPin.write(0);
greenPin.write(0);
bluePin.write(0);
}
}

function readSensor()
{
light=lightreading.read();
local value = 1- (light / 65535.0);
redPin.write(value);
greenPin.write(value);
bluePin.write(value);
server.log(format(“Lightness is:” +light));
}
`
if i use this, the light sensor reads a single value, how can i modify this program to make light sensor to stop reading values when status=0;
please i need some help!!!

and this is the device
`

imp.configure(“LED Web Control”, [], []); // Configure the imp

// Setup variables for our pins:
lightreading <- hardware.pin9
redPin <- hardware.pin1; // R of RGB
greenPin <- hardware.pin2; // G of RGB
bluePin <- hardware.pin5; // B of RGB

// Configure our pins:
lightreading.configure(ANALOG_IN);
redPin.configure(PWM_OUT, 0.005, 0); // PWM output 5ms clock, off
greenPin.configure(PWM_OUT, 0.005, 0); // PWM output 5ms clock, off
bluePin.configure(PWM_OUT, 0.005, 0); // PWM output 5ms clock, off

ledState <- 0; // off
ledColor <- { r = 255, g = 255, b = 255 }; // default to white for color
light <- 0;
status<-0;

function setLed(ledStatergb)
{
ledState = ledStatergb;
if (ledState) {
redPin.write(ledColor.r/255.0);
greenPin.write(ledColor.g/255.0);
bluePin.write(ledColor.b/255.0);

}else{
    redPin.write(0);
    greenPin.write(0);
    bluePin.write(0);
    status <-false;
    ledColor <- { r = 255, g = 255, b = 255 }; 
   
}

}

function setRGB(rgbValue)
{
if (ledState) {
ledColor.r = rgbValue.r;
ledColor.g = rgbValue.g;
ledColor.b = rgbValue.b;

    redPin.write(ledColor.r/255.0);
    greenPin.write(ledColor.g/255.0);
    bluePin.write(ledColor.b/255.0);
    
}

}

function setSensor(ledState)
{
status=ledState;
if (status)
{
readSensor();
}
else
{
redPin.write(0);
greenPin.write(0);
bluePin.write(0);
}
}

function readSensor()
{
light=lightreading.read();
local value = 1- (light / 65535.0);
redPin.write(value);
greenPin.write(value);
bluePin.write(value);
server.log(format(“Lightness is:” +light));
}

// setTimer will turn the LEDs off after a specified number of seconds
// This function will be called by the agent.
function setTimer(time)
{
if (time != 0)
imp.wakeup(time, ledOff); // Call ledsOff in ‘time’ seconds.

}

agent.on(“led”, setLed);
agent.on(“rgb”, setRGB);
agent.on(“timer”, setTimer);
agent.on(“sensor”, setSensor);

// ledsOff just turns all LEDs off.
function ledOff()
{
redPin.write(0);
greenPin.write(0);
bluePin.write(0);
}

`

I know your application, I saw it on youtube, it’s very interesting, I’ve just start learning to program, I have a lot to learn, but I don’t have a person were i can go if I need help and when I am stuck I don’t know what to do.