Problem trying to add button input to imp

just starting on an imp. apologies if this is newbie-ish.

here’s the device code. something in the button code is conflicting with the exisiting code. both work fine by themselves.

any ideas?

thanks in advance.

`// create a global variabled called led,
// and assign pin9 to it
led <- hardware.pin9;
lcd <- hardware.uart12;

button <- hardware.pin5;

button.configure(DIGITAL_IN_PULLUP);

// configure led to be a digital output
led.configure(DIGITAL_OUT);
lcd.configure(9600, 8, PARITY_NONE, 1, NO_RX);
lcd.write(254);
lcd.write(0x01);
lcd.write(124);
lcd.write(155);

/* function buttonPress() {
local state = button.read();
if (state == 1) {
// when the button is released
server.log(“release”);
} else {
// when the button is pressed
server.log(“press”);
}

}*/

// function to turn LED on or off
function setLed(ledState) {
server.log("Set LED: " + ledState);
lcd.write(254);
lcd.write(0x01);

lcd.write(ledState);
//led.write(ledState);
}
`
// register a handler for “led” messages from the agent
agent.on(“led”, setLed);

Here are a couple of comments. I think you want to set a callback for your button pin so that when you press it, it triggers buttonPress().

button.configure(DIGITAL_IN_PULLUP, buttonPress);

Also, in your agent.on function, are you trying to call setLed()? The second parameter passes data, it doesn’t call a function. I think you might want to do:

agent.on("led" , function(ledState) { setLed(ledState); )};

This assumes that you passed an ledState value down from the agent.

Could you explain more about what you are doing?
Also, if you wrap your code in tags with the “C” it make it easier to read.

Hey thanks.

I’m trying to use the imp to create an alert: a combination of an led that lights and text on a lcd. I wanted to use the button to turn it off.

I can get the button to work by itself, and get the led and lcd combo to work by itself. But not together : (

I would echo @MakeDeck’s suggestion. Un-comment buttonPress() and add a state-change function to button.configure():

button.configure(DIGITAL_IN_PULLUP, buttonPress);

I would add a global variable - say, lcd_flag - which this function changes. Then you can have your LED/LCD alert function check lcd_flag and if the flag is true (set by a button press), then no alert is presented (and vice versa).

That’s if you want to switch alerting on and off. If you just want to cancel the alert after the LED has been lit, just get buttonPress() to call setLed() with an appropriate ledState value to turn off the light/write the correct message on the LCD.

PS, @dcdenison, I added a couple of code tags to your post to make the code more easy for other readers to scan.

thanks for the advice. This sounds very promising.

And thanks for adding the

code

tags.

Did I mention I’m a newbie at this? : )

Just wanted to say that the suggestions from MakeDeck and Smittytone worked perfectly! Thank you so much for your help.