Imp Sleeping & Waking for Sensor Reading

Im currently trying to set the imp to only take a reading once every 4 seconds. At present it seems to be taking readings a few times every seconds. I have a contact sensor connected to pin1 and set to show high or low. I have looked over some previous worked examples but cant seem to find the correct way to write the code. Any help would be great.

Current Code:

local output = OutputPort(“Alarm”, “number”);

function sensor1() {
local sensor1state =;“1,” + sensor1state);
server.log(“1,” + sensor1state);
output.set(“1,” + sensor1state);


hardware.pin1.configure(DIGITAL_IN, sensor1);

server.log(“Hardware Configured”);

imp.configure(“Sensor Test”, [], [output]);

Switch de-bouncing?

Configure the pin1 like this:

hardware.pin1.configure(DIGITAL_IN, pin1changed);

When the state of that pin changes, it will execute the function called “pin1changed”

function pin1changed(){
if ( {
sensor1(); // execute your sensor1 function
imp.sleep(0.5); // debounce a bit!