N00B mode select button help

Hi this is my first post in the forum but i have been reading for a while now!

I have done a few projects with the arduino and have now started to write my own code for a project i would like to do

A simple LED Flasher with a mode select button i have written the code by looking at other projects and this is what i have come up with so far

// Mode 1 White leds on 
// Mode 2 Flashing Red and blue + White Leds on
// Mode 3 Flashing Red and blue
// Mode 4 All flashing

int counter = 0; // set counter at 0 to start
int switchPin = 9; // define button pin

//define LED Pins
int LEDB = 10;
int LEDW = 11;
int LEDR = 12;


void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDW, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  //Handle input
  int switchVal = digitalRead(switchPin);
  if(switchVal == HIGH)
  {
    delay(500);  
    counter ++;
    //Reset count if over max mode number
    if(counter == 5)
    {
      counter = 0;
    }
  }

  else
    //Change mode
    switch (counter) {
    case 1:
      digitalWrite(LEDW, HIGH);   // set the LED on
      break;
    case 2:
      digitalWrite(LEDW, HIGH);   // set the LED on
      digitalWrite(LEDR, HIGH);   // set the LED on
      delay(500);              // wait for half a second
      digitalWrite(LEDB, HIGH);   // set the LED on
      digitalWrite(LEDR, LOW);    // set the LED off
      delay(500);              // wait for half a second
      digitalWrite(LEDB, LOW);    // set the LED off      
      break;      
    case 3:
      digitalWrite(LEDW, HIGH);   // set the LED on
      delay(500);              // wait for half a second
      digitalWrite(LEDW, LOW);    // set the LED off
      digitalWrite(LEDR, HIGH);   // set the LED on
      delay(500);              // wait for half a second
      digitalWrite(LEDB, HIGH);   // set the LED on
      digitalWrite(LEDR, LOW);    // set the LED off
      delay(500);              // wait for half a second
      digitalWrite(LEDB, LOW);    // set the LED off      
      break;       
    case 4:
      digitalWrite(LEDR, HIGH);   // set the LED on
      delay(500);              // wait for half a second
      digitalWrite(LEDB, HIGH);   // set the LED on
      digitalWrite(LEDR, LOW);    // set the LED off
      delay(500);              // wait for half a second
      digitalWrite(LEDB, LOW);    // set the LED off      
      break; 
      }
}

The problem I'm having is that it randomly changes modes doesn't stay in one mode but also does not display all modes it kinda jumps from all on or all flashing also i would like to run this on an Attiny13 in the end using the arduino IDE attiny13 core will this also work?

any help would be awesome

thanks

Jamie!

P.S sorry if in the wrong place?

You really need to compare the current state of the switch to the previous state of the switch, and only increment the counter when the switch transitions from not pressed to pressed.

You are not using the internal pullup resistor, so you MUST have an external pullup or pulldown resistor. The test for HIGH meaning that the switch is pressed suggests that the resistor must be wired as a pulldown resistor. How is your switch wired?

I have the switch wired like this but to pin 9 as code suggests

Sorry I havn't updated sooner but i have fixed the problems and its all working perfectly!

heres the working code i have it all running on a Attiny13

//JSM's Attiny13 Police Bike Light 
// 4 Modes 
// Mode 1 White leds on (head light)
// Mode 2 Police flashing Red and blue + Head light
// Mode 3 Police flashing Red and blue only
// Mode 4 Everything flashing
int counter = 0; // set counter at 0 to start
int switchPin = 3; // define button pin
//define LED Pins
int LEDB = 0;
int LEDW = 1;
int LEDR = 2;
int previous = LOW;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDW, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  //Handle input
  int switchVal = digitalRead(switchPin);
  if(switchVal == HIGH && previous == LOW)
  {
    delay(200);  
    counter ++;
    //Reset count if over max mode number
    if(counter == 5)
    {
      counter = 0;
    }
  }
  else
    //Change mode
    switch (counter) {
    case 1:
      digitalWrite(LEDW, HIGH);   // set the LED on
      digitalWrite(LEDR, LOW);    // set the LED off
      digitalWrite(LEDB, LOW);    // set the LED off
      break;
    case 2:
      digitalWrite(LEDW, HIGH);   // set the LED on
      digitalWrite(LEDR, HIGH);   // set the LED on
      delay(50);              // wait for half a second
      digitalWrite(LEDB, HIGH);   // set the LED on
      digitalWrite(LEDR, LOW);    // set the LED off
      delay(50);              // wait for half a second
      digitalWrite(LEDB, LOW);    // set the LED off      
      break;      
    case 3:
      digitalWrite(LEDW, LOW);
      digitalWrite(LEDR, HIGH);   // set the LED on
      delay(50);              // wait for half a second
      digitalWrite(LEDB, HIGH);   // set the LED on
      digitalWrite(LEDR, LOW);    // set the LED off
      delay(50);              // wait for half a second
      digitalWrite(LEDB, LOW);    // set the LED off      
      break;       
    case 4:
      digitalWrite(LEDW, HIGH);   // set the LED on
      delay(50);              // wait for half a second
      digitalWrite(LEDW, LOW);    // set the LED off
      digitalWrite(LEDR, HIGH);   // set the LED on
      delay(50);              // wait for half a second
      digitalWrite(LEDB, HIGH);   // set the LED on
      digitalWrite(LEDR, LOW);    // set the LED off
      digitalWrite(LEDW, LOW);    // set the LED off
      delay(50);              // wait for half a second
      digitalWrite(LEDB, LOW);    // set the LED off      
      break; 
      }
}