Second project problems (Power and code)

Hello everyone..I've got some problems with my little project..Which is a piezo that lights up a LED.

Now, my problems are:

  • Will a 9V battery power my arduino..I'm going to use a piezo (link here) and a 3mm LED
  • Also, what can i do in the coding side for a variable-like thing..I want to have something like..If i knocked once..It stills turned on and when i knock again..It turns off..I was going to use the code in the manual/example (Knock)..But i've found that it won't work

And thanks.

Will a 9V battery power my arduino..

If you are referring to a small rectangular 9V transistor battery, it will for a little while. Those batteries are only rated for about 300mA hours.

I was going to use the code in the manual/example (Knock)..But i've found that it won't work

Did you change the ledPin variable to the pin with your LED?

Show the code that you actually used and a schematic of your setup for better assistance.

Well, yep..I was going to use a small (Energizer) 9V rectangular one..but, would this battery work better? link

About the code..I didn't try it yet. As some part aren't bought yet..But, i guess that this code link is just for lighting the LED when you touch the piezo and disable the LED when you leave the piezo..I did want to have something different..Like an on/off button using the piezo..Hope you understand. Thanks.

would this battery work better?

That depends on your definition of "better". It will last longer (maybe about twice as long). How long do you want the battery to last? How much current does the project require? What is the required battery voltage? With those pieces of information, you can then choose the proper battery.

I think that the Knock example does what you want. Each detected knock that is over the threshold will toggle the LED. First knock, LED on. Next knock, LED off. Next on...

Ah, okay...About the battery, i need something that lasts for a night as an example..As it's something like a light source with knocks

i need something that lasts for a night as an example..As it's something like a light source with knocks

Simplest may be a 6 AA battery case with Arduino plug like THIS:

Or you could use 4 AA in a case like THIS: with rechargeable NiMH AA at 1.25V each that can be directly connected to the arduno +5 and Gnd.

Look at low-power strategies.. Maybe the Arduino can sleep and wake on an interrupt IF the piezo would work for that... See THIS: from Nick Gammon.

DISCLAIMER: Mentioned stuff from my own shop...