Assigning variable to hardware.pin.read()

I connected an Adafruit ADXL335 accelerometer breakout to an imp002 to check it out, and am a little confused by the following.
If I declare a global value to read the pin, and then try to log it, I get a 0 or a 1. In either of these ways:
local xRaw = hardware.pin9.read();
yRaw <- hardware.pinA.read();
server.log("X value: " + xRaw);
server.log("Y value: " + yRaw);

But, if i create a local variable inside a function and log it, I get the correct voltage reading.
I’m a bit confused on what would be causing the difference?

Interesting that you can do it with a local; http://devwiki.electricimp.com/doku.php?id=writingefficientsquirrel says (and Peter generally knows best here) that you need to do the binding in two steps, eg:

pin9 <- hardware.pin9; pin9reader <- pin9.read.bindenv(pin9); yRaw <- pin9reader();

Could we see the rest of the code? Are you sure that the pin9.configure() is happening before the pin9.read()? Also, the ADXL335 datasheet suggests that it may take 1ms or so to come ready after power-on, though of course if it’s powered from the same 3v3 as the imp, that’s less than the imp itself takes.

(Hugo, man, “pin9.read()”, not “pin9.read”.)

Peter

This works:
`
supplyVoltage <- hardware.voltage();
//xRaw <- hardware.pin9.read();
//yRaw <- hardware.pinA.read();
//zRaw <- hardware.pinB.read();

function readADXL335() {
server.log(supplyVoltage);
server.log("X value: " + hardware.pin9.read()/65535.0);
server.log("Y value: " + hardware.pinA.read()/65535.0);
server.log("Z value: " + hardware.pinB.read()/65535.0);
readADXL335b();
imp.wakeup(2.00, readADXL335);
}
function readADXL335b() {
local xRaw = hardware.pin9.read();
local yRaw = hardware.pinA.read();
local zRaw = hardware.pinB.read();
server.log(supplyVoltage);
server.log("X value: " + xRaw/65535.0);
server.log("Y value: " + yRaw/65535.0);
server.log("Z value: " + zRaw/65535.0);
server.log("X value: " + xRaw);
server.log("Y value: " + yRaw);
server.log("Z value: " + zRaw);
}

hardware.pin9.configure(ANALOG_IN);
hardware.pinA.configure(ANALOG_IN);
hardware.pinB.configure(ANALOG_IN);
imp.configure(“ADXL 335”, [], []);
readADXL335();
This doesn't:
supplyVoltage <- hardware.voltage();
xRaw <- hardware.pin9.read();
yRaw <- hardware.pinA.read();
zRaw <- hardware.pinB.read();

function readADXL335() {
server.log(supplyVoltage);
server.log("X value: " + hardware.pin9.read()/65535.0);
server.log("Y value: " + hardware.pinA.read()/65535.0);
server.log("Z value: " + hardware.pinB.read()/65535.0);
readADXL335b();
imp.wakeup(2.00, readADXL335);
}
function readADXL335b() {
//local xRaw = hardware.pin9.read();
//local yRaw = hardware.pinA.read();
//local zRaw = hardware.pinB.read();
server.log(supplyVoltage);
server.log("X value: " + xRaw/65535.0);
server.log("Y value: " + yRaw/65535.0);
server.log("Z value: " + zRaw/65535.0);
server.log("X value: " + xRaw);
server.log("Y value: " + yRaw);
server.log("Z value: " + zRaw);
}

hardware.pin9.configure(ANALOG_IN);
hardware.pinA.configure(ANALOG_IN);
hardware.pinB.configure(ANALOG_IN);
imp.configure(“ADXL 335”, [], []);
readADXL335();
`

The first function works… just referring to the second one.

Ahhhh, I see what you’re doing now.

Assigning the globals does not cause the pin to be read every time the global variable is referenced - it gets read once at initialization and never again.

This is why the local stuff works, because that assignment is done every time the readADXL335b() function is called.

You can use globals (so the value is available from the entire program), but you’ll need to re-read the data to freshen them when required. ie, xRaw = hardware.pin9.read(); in the read function, which will do the read then store the result in the previously-created global.

In the non-working one, the xRaw, yRaw, and zRaw values are read before the pins are configured as analogue inputs. You should move all the pin.configure() calls to the top of the program.

Peter

Got it. Thanks for the help! Still trying to figure out something really interesting to do with this accelerometer. Is there any way to trigger a callback using an analog input? I’ve found callbacks to be extremely useful in my current projects.

Callbacks for analog input are on the to-do list, but not yet on the to-done list. The power saving isn’t as big as you’d hope, because leaving the ADC itself running consumes a reasonable amount of power.

Peter

That’s understandable. My first thoughts on using the accelerometer have been around using it as a wireless sensor.

For that you should probably use a digital accelerometer… Nora can wake on accelerometer events, for example.