Indicators

Hi,
for my project im trying to make a set of indicators, by basically pressing a button, and the led starts blinking, then pressing the button again, so they stop.

This is my first project with arduino and was wanting some help with coding.

This is what i have so far, but it only kind of works

Any help or guidance is much appreciated!

int button = 3;
int ledPin=13;
int state = 0;
int count = 0;

void setup(){
Serial.begin(9600);
pinMode(button, INPUT);
pinMode (ledPin, OUTPUT);
}

void loop(){
state = digitalRead(button);
if(state == HIGH){
count = count + 1;
Serial.println(count);
delay(500);
}

if(count == 1){
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}

if (count == 2){
digitalWrite(ledPin, LOW);
}

if (count == 3){
count = 1;
}

}

In loop() you increment count as long as the button pin is HIGH. I.e. count is constant only while the pin is LOW, and this is not what you want.

Furthermore make sure that the button pin is LOW when the button is not pressed. Buttons normally are connected from the pin to Gnd (LOW), and the pin is in HIGH state by use of a pullup resistor (to Vcc) or by mode INPUT_PULLUP.

Did you already try a simple example, showing how to debounce an button?

Use button library for debouncing

#include<Button.h>

boolean blinking = false;
int ledpin = 13;
Button button = Button(3,PULLUP);

void setup() {
  pinMode(ledpin,OUTPUT);
}
http://playground.arduino.cc/code/Button
void loop() {
  if(button.uniquePress()) {
    blinking = !blinking;
  }
  if(blinking){
   digitalWrite(ledpin, HIGH);   
    delay(1000);                 
    digitalWrite(ledpin, LOW);   
    delay(1000);
  }
}

http://playground.arduino.cc/code/Button

Have a look at the demo several things at a time.

...R

Right... What Robin is trying to tell you is that you can't read the button (or do anything else) during the delay() time.