Mode switch changing without input

I'm trying to use a pushbutton as mode switch to choose between two patterns to blink 5 LEDs. I had some help from this previous forum topic: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1275018531/7

The code uses a variable 'counter', and everytime the switch pulls the input pin high, it advances the counter by one. The hardware is working, it turns on the built in LED on pin 13.

The problem is, that the mode is changing on its own. It cycles once through one pattern, then once through the other. And repeats. Pushing the button doesn't seem to change anything. Either the counter variable is advancing itself, or the software is ignoring the if/else statements that choose the pattern.

Have a gander:

int counter = 0;
int val = 0;
int buttonPin = 13;
int x;
int ledPins[] = {2,3,4,5,6};
int ledTime = 150;
int sampleTime = 0;
int knightRider[] = {2,3,4,5,6,5,4,3};
int randPattern[] = {2,4,2,4,3,5,3,6,2,6,4,5,4,5,3,6};

void setup()
{
  for(int i = 0; i < 5; i++){
      pinMode(ledPins[i],OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}

void loop()
{
  val = digitalRead(buttonPin);
  if(val = HIGH)
  {
    counter++;
    if(counter == 2)
    {
      counter = 0;
    }
  }
  if(counter == 0)
  {
    x = analogRead(0);
    if (x+5 < 300)
      {
        allOn();
        delay(sampleTime);
        return;
      }
      else
      {
        allOff();
      }
  
    for(int j = 0; j < 8; j++){
      digitalWrite(knightRider[j], HIGH);
      x = analogRead(0);
      if (x+5 < 300)
      {
        allOn();
        delay(sampleTime);
        return;
      }
      else
      {
        delay(ledTime);
        digitalWrite(knightRider[j], LOW);
      }
    }
  }
  else if(counter == 1)
  {
    x = analogRead(0);
    if (x+5 < 300)
      {
        allOn();
        delay(sampleTime);
        return;
      }
      else
      {
        allOff();
      }
  
    for(int j = 0; j < 16; j++){
      digitalWrite(randPattern[j], HIGH);
      x = analogRead(0);
      if (x+5 < 300)
      {
        allOn();
        delay(sampleTime);
        return;
      }
      else
      {
        delay(ledTime);
        digitalWrite(randPattern[j], LOW);
      }
    }
  }
}

void allOff(){
  digitalWrite(ledPins[0], LOW);
  digitalWrite(ledPins[1], LOW);
  digitalWrite(ledPins[2], LOW);
  digitalWrite(ledPins[3], LOW);
  digitalWrite(ledPins[4], LOW);
}

void allOn(){
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[4], HIGH);
}

Your help is appreciated.

  if(val = HIGH)

This assigns the value HIGH to the variable val. Was this what you wanted? Or did you want ==, to compare the value in val to HIGH?

Paul, thank you.

It's always something so small I'm missing.

Tried that, button is now responsive.

Once of these days I'll learn how the comparison operators work.

Thanks again!

A very common though in my opinion awkward recommendation is to write the constant first when comparing anything

if (4711 == value)

This indeed has helped to avoid a lot of "mistakes".

Alas, there is complot of the C-compiler designers, who will not issue a warnung when "=" is used inside a condition (which is in 99% of all cases a "mistake")

Alas, there is complot of the C-compiler designers, who will not issue a warnung when "=" is used inside a condition (which is in 99% of all cases a "mistake")

if((val = Serial.read()) == -1)

This is a perfectly valid use of = in a condition. It shouldn't be too hard to detect the = is an assignment operator inside ( and ) and that there is an equality operator that forms the basis for the if test, though.

In a classroom setting, the instructor carefully emphasizes the difference between = and ==. When people are trying to learn C/C++ programing on an Arduino, without having taken a basic C programming course, just by modifying existing code is generally where people get into trouble.

Having the compiler issue a warning in the case of an = (it isn't a fatal error, and should not stop compilation) would not be terribly useful in the Arduino IDE, since warnings are disabled.

Not to get involved in the argument...but.

The compiler never threw out an error in my original code, the LEDs behaved oddly. So regardless of how the compiler may have behaved, I don't think it would've kicked out an error either way.

I have taken no C programming class at all, beginner or otherwise. I came from the world of PHP, HTML, and Javascript. I come here to learn from my mistakes.

Arduino has been good to me.

I don't like = and ==

I think Pascal had it right with assignment:

a := 10;

pronounced 'a becomes equal to 10'

I come here to learn from my mistakes.

The more mistakes I make, the more I learn.