Fireworks controller LED sketch HELP!!!

Hello everybody!
I have a few questions for you guys! To start off, I am building a firework controller (for detonating a fireworks display safely). I have put this code together:

*int buttonState = 0; *
*int keyswitchPin = 1; *
int ledPin = 10;
void setup() {
* pinMode(ledPin, OUTPUT); *
* pinMode(keyswitchPin, INPUT); *
}
void loop() {
* buttonState = digitalRead(keyswitchPin);*
* if (buttonState == HIGH) { *
* digitalWrite(ledPin, HIGH);*
* delay(300); *
* digitalWrite(ledPin, LOW); *
* delay(300); *
* }*
* else {*
* digitalWrite(ledPin, LOW);*
* }*
}

The code will be put on an At-tiny85 chip using an Arduino-Uno as isp. The purpose of this code is that when the controller is turned on (with a key-switch), an LED will blink for 4 times, indicating the power-up of the system. The only problem is that the code does the opposite (it blinks the led when the switch is off, not on) and the led blinks continuously, not only 4 times . How do I fix this?

Secondly, could I use a 555 timer chip. In other words, is it possible to make the 555 chip blink the led for only a determined amount of times, then stop?

Finally, if none of the above is possible, are there other ways of doing this.

Thanks a lot!

 if (buttonState == LOW)

Would do it, or you could wire the button up the other way round.

Q2 - no.

Read the how to use this forum sticky and post the code corectly next time.

Tell us where in your code are you telling the LED to blink only 4 times?
You could label the switch to Off/On rather than On/Off :wink:

Please use code tags.

Fixing the code means writing it conforming to your plans.
If you want to have a number of blinks, you should check to see that number of blinks has occured yet.
I can't seem to find you do that in this code.
You are checking to see pin 1 gets HIGH.
Are you sure it gets LOW ?

Use pull ups or pull downs.
If you use the internal pull up, saving yourself some external hardware, you should wire the switch to GND, and check for the pin to go LOW.

You do not need a 555 to do this, that would only complicate things.

Ok, I have fixed the code! Thanks you everyone!

Just one more question: The switch controlling the unit is using 12 volts DC. Can I pass 2 wires directly from the switch to the arduino (kind of in parallel with the circuit)? Can I wire-up a 5 volt voltage regulator so the voltage is only 5 volts instead of 12 going to the switch pin? If not, what can I do?

You can use a transistor switch configuration.
Also might be an example where a 6 volt zener diode could be used.
(Some NPN base emitters have a Zener voltage of 6 volts, but that's another story)

The switch controlling the unit is using 12 volts DC. Can I pass 2 wires directly from the switch to the arduino

No.

Can I wire-up a 5 volt voltage regulator so the voltage is only 5 volts instead of 12 going to the switch pin?

No.

LarryD:
You can use a transistor switch configuration.

How do I make one of those? And how does it work?

How do I make one of those?

You don't make a transistor you buy them.

And how does it work?

Ah, ok. I thought I had to solder stuff together some way.
Thanks

But how do I wire it up? And what type of transistor do I use?

+12 switched---1K---Base of NPN transistor eg 2N2222---Emitter to GND of Arduino and GND of 12 supply.
Then
+5 Arduino---10K---Collector and a Arduino digital input pin.

Note when the switch is closed +12 switched turns on the transistor. The collector goes LOW and you look for a LOW in software which indicates the switch is closed.

Frank-duino:
But how do I wire it up? And what type of transistor do I use?

You are requiring a lot of spoon feeding for something that you could find out from google.
Here is a more "basic" tutorial.
http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads
Your switch is put in place of the motor or load and you do not need the diode.

Thank you very much!

Oh and for the transistor switch thing, can I use the 2N3904 NPN transistor?

Yes

Is this how the transistor switch should look like?

Here is the schematic: MEGA

From left to right: Switch with the power it controls/ Transistor (NPN 2N3904)/ Arduino board pin and ground.

Anyone?

Where is the load ?

If you make it like this, things get blown up.

Well the thing is, there are technically two loads. I want to use the transistor kind-of as a relay, so the arduino can read the switch's state without getting damaged (because the switch is connected to 12 volts). So the load is the switch's power (12volts).