6 button code for el-wire

Hello Again!

I made a circuit with an Arduino, 6 pushbuttons, an interface card, driver and 2 EL-wires.
See picture:


(in future it will become 12 wires, but I stick to 2 in this exploring fase)

These wires need to be controlled by the pushbuttons via the Arduino acoording this scheme:
buttons: 1 2 3 4 5 6
wire 1 ON ON OFF OFF ON OFF
wire 2 ON OFF ON OFF OFF ON

The wires must stay on until the same or another button is pushed

The code below does not work. Both wires stay on, no matter what button I push...

What am I doing wrong???

// INPUTS
int switch1Pin = 14;
int switch2Pin = 15;
int switch3Pin = 16;
int switch4Pin = 17;
int switch5Pin = 18;
int switch6Pin = 19;

// OUTPUTS
int wire1Pin = 0;
int wire2Pin = 1;

// VARIABLES
boolean last1Button = LOW;
boolean last2Button = LOW;
boolean last3Button = LOW;
boolean last4Button = LOW;
boolean last5Button = LOW;
boolean last6Button = LOW;
boolean current1Button = LOW;
boolean current2Button = LOW;
boolean current3Button = LOW;
boolean current4Button = LOW;
boolean current5Button = LOW;
boolean current6Button = LOW;
boolean wire1On = false;
boolean wire2On = false;
boolean wire1Off = true;
boolean wire2Off = true;

// VOID SETUP
void setup ()
    {
    pinMode(switch1Pin, INPUT);
    pinMode(switch2Pin, INPUT);
    pinMode(switch3Pin, INPUT);
    pinMode(switch4Pin, INPUT);
    pinMode(switch5Pin, INPUT);
    pinMode(switch6Pin, INPUT);
    pinMode(wire1Pin, OUTPUT);
    pinMode(wire2Pin, OUTPUT);
    }
  boolean debounce1(boolean last1)
    {
    boolean current1 = digitalRead(switch1Pin);
    if (last1 != current1)
      {
      delay(5);
      current1 = digitalRead(switch1Pin);
      }
    return current1;
    }
  boolean debounce2(boolean last2)
    { 
    boolean current2 = digitalRead(switch2Pin);
    if (last2 != current2)
      {
      delay(5);
      current2 = digitalRead(switch2Pin);
      }
    return current2;
    }
  boolean debounce3(boolean last3)
    { 
    boolean current3 = digitalRead(switch3Pin);
    if (last3 != current3)
      {
      delay(5);
      current3 = digitalRead(switch3Pin);
      }
    return current3;
    }
  boolean debounce4(boolean last4)
    { 
    boolean current4 = digitalRead(switch4Pin);
    if (last4 != current4)
      {
      delay(5);
      current4 = digitalRead(switch4Pin);
      }
    return current4;
    }
  boolean debounce5(boolean last5)
    { 
    boolean current5 = digitalRead(switch5Pin);
    if (last5 != current5)
      {
      delay(5);
      current5 = digitalRead(switch5Pin);
      }
    return current5;
    }
  boolean debounce6(boolean last6)
    { 
    boolean current6 = digitalRead(switch6Pin);
    if (last6 != current6)
      {
      delay(5);
      current6 = digitalRead(switch6Pin);
      }
    return current6;
    }


// VOID LOOP
void loop ()
  {
  current1Button = debounce1(last1Button);
  if (last1Button == LOW && current1Button == HIGH)
    {
    wire1On = !wire1On;
    wire2On = !wire2On;
    wire1Off = !wire1Off;
    wire2Off = !wire2Off;
    }
  last1Button = current1Button;
  digitalWrite(wire1Pin, wire1On);
  digitalWrite(wire2Pin, wire2On);

  current2Button = debounce2(last2Button);
  if (last2Button == LOW && current2Button == HIGH)
    {
    wire1On = !wire1On;
    wire2On = !wire2On;
    wire1Off = !wire1Off;
    wire2Off = !wire2Off;
    }
  last2Button = current2Button;
  digitalWrite(wire1Pin, wire1On);
  digitalWrite(wire2Pin, wire2Off);

  
  current3Button = debounce3(last3Button);
  if (last3Button == LOW && current3Button == HIGH)
    {
    wire1On = !wire1On;
    wire2On = !wire2On;
    wire1Off = !wire1Off;
    wire2Off = !wire2Off;
    }
  last3Button = current3Button;
  digitalWrite(wire2Pin, wire2On);
  digitalWrite(wire1Pin, wire1Off);


  current4Button = debounce4(last4Button);
  if (last4Button == LOW && current4Button == HIGH)
    {
    wire1On = !wire1On;
    wire2On = !wire2On;
    wire1Off = !wire1Off;
    wire2Off = !wire2Off;
    }
  last4Button = current4Button;
  digitalWrite(wire1Pin, wire1Off);
  digitalWrite(wire2Pin, wire2Off);


  current5Button = debounce5(last5Button);
  if (last5Button == LOW && current5Button == HIGH)
    {
    wire1On = !wire1On;
    wire2On = !wire2On;
    wire1Off = !wire1Off;
    wire2Off = !wire2Off;
    }
  last5Button = current5Button;
  digitalWrite(wire1Pin, wire1On);
  digitalWrite(wire2Pin, wire2Off);

  current6Button = debounce6(last6Button);
  if (last6Button == LOW && current6Button == HIGH)
    {
    wire1On = !wire1On;
    wire2On = !wire2On;
    wire1Off = !wire1Off;
    wire2Off = !wire2Off;
    }
  last6Button = current6Button;
  digitalWrite(wire1Pin, wire1Off);
  digitalWrite(wire2Pin, wire2On);
  }

Please.... push me in the right direction, or correct me were I am going wrong!!!

Thank you!

correct me were I am going wrong!!!

Oh, dear. Where to begin?

int switch1Pin = 14;
int switch2Pin = 15;
int switch3Pin = 16;
int switch4Pin = 17;
int switch5Pin = 18;
int switch6Pin = 19;

should be:

int switchPin[] = {14, 15, 16, 17, 18, 19};
int wire1Pin = 0;
int wire2Pin = 1;

Why are you connecting wires to the Serial port pins?

boolean wire1On = false;
boolean wire1Off = true;

A pin can either be HIGH/LOW, on/off, true false. Can you explain when both of these meaningfully can be true or false?

  boolean debounce1(boolean last1)
    {
    boolean current1 = digitalRead(switch1Pin);
    if (last1 != current1)
      {
      delay(5);
      current1 = digitalRead(switch1Pin);
      }
    return current1;
    }

You have 6 functions like this. The only difference between them is the pin number. Dump 5 of them, and make pin number an argument.

  current1Button = debounce1(last1Button);
  if (last1Button == LOW && current1Button == HIGH)
    {
    wire1On = !wire1On;
    wire2On = !wire2On;
    wire1Off = !wire1Off;
    wire2Off = !wire2Off;
    }
  last1Button = current1Button;
  digitalWrite(wire1Pin, wire1On);
  digitalWrite(wire2Pin, wire2On);

You have 6 blocks like this. Again, a waste of effort. Create a function that does this, and call it 6 times, if that makes sense.

Not that it appears to. Why do all 6 buttons currently control the same two pins?

Thanks PaulS! :slight_smile:
First of all I want to say I am sorry for the number of mistakes. You must know that 2 weeks ago I had never seen an Arduino before and that I had never heart of a language called C or C++.... I am just a simple architect...

So, why am I trying to do this? In February there is a big costume party and I am making a "stick man" suit like this:

6 buttons control 12 different pieces of EL-wire combining them to 6 different face/body expressions. :%

Why are you connecting wires to the Serial port pins?

Because I need 12 outputs for 12 wires, I need 6 inputs for 6 buttons. I thought I would keep it clear for myself if I would use one side of the Arduino for the output (pin 0-11) and the other side for the input (pin 14-19).
I wired the whole circuit according to this idea.

A pin can either be HIGH/LOW, on/off, true false. Can you explain when both of these meaningfully can be true or false?

This I did because the program gave me errors saying that 'wire1Off" and "wire2Off" was not declared.
The "wire1Off" and "wire2Off" was added to the digitalWrite configuration because I wanted to make sure that:
if button 1 is active and I would press button 2, that some wires that are supposed to be off in button 2 state, are turned off. To prevent that I always first need to switch off button 1, before button 2 state will be correct.
This fluent/fast way of controlling/switching is important to blink with an eye or something. :wink:

You have 6 functions like this. The only difference between them is the pin number. Dump 5 of them, and make pin number an argument.

The way you say it, it sounds simple. But how and where do I make that argument. I actually don't know what an argument is.

You have 6 blocks like this. Again, a waste of effort. Create a function that does this, and call it 6 times, if that makes sense.

That function I can not find in this setup for multiple buttons. That is why I copied the function for one button 6 times... My total lack of knowledge in coming up with my own functions, forces me to find examples and try to copy them...

Not that it appears to. Why do all 6 buttons currently control the same two pins?

The reason is because I wanted to test my code on just two outputs before making a really long and difficult code. The longer the code, the worse my overview and more possibilities to make mistakes.

I really appreciate your help. You also helped me yesterday and the result was that I got at least the two wires on and off! Before I didn't get anything out of it!
I hope I am not a huge pain in the ass! :slight_smile:
If it would be possible I would find an pay someone to write this code for me, but Arduino and EL-Wire is very unknown here in The Netherlands so I can't find one.....

So, why am I trying to do this? In February there is a big costume party and I am making a "stick man" suit like this:

Looks like fun. I guess.

I thought I would keep it clear for myself if I would use one side of the Arduino for the output (pin 0-11)

That makes sense, but you should use 2 to 13, so that 0 and 1 are available as the serial port, for debugging purposes.

This I did because the program gave me errors saying that 'wire1Off" and "wire2Off" was not declared.

You shouldn't be using names you haven't defined. Anywhere where you think you need wireNOff, use !wireNOn. The ! means not. So, !wire1On means that wire 1 is not on.

To prevent that I always first need to switch off button 1, before button 2 state will be correct.

What kind of switches are you using? I would have expected you to be using momentary contact pushbutton switches, so that when you quit pressing, the spring inside unpresses the switch. In this case, there is no need to switch off switches (please, buttons are for holding shirts and pants closed).

The way you say it, it sounds simple. But how and where do I make that argument. I actually don't know what an argument is.

It is. The data in parentheses after a function name is referred to as the function's arguments. You are calling the debounce1() function now with one argument:

  current1Button = debounce1(last1Button);

The function is defined as taking one argument:

  boolean debounce1(boolean last1)

The function should take two arguments:

byte debounce(byte prevState, byte pinNumber)
{
    byte currState = digitalRead(pinNumber);
    if (prevState != currState)
    {
      delay(5);
      currState = digitalRead(pinNumber);
    }
    return currState;
}

Now, you can use this function to debounce the switch on any pin.

Note that boolean and byte are the same size, but that byte implies a number from 0 to 255, which includes HIGH and LOW, whereas boolean implies true or false. The key word in that sentence is implies. In reality, a boolean is a byte and can have the value 27 assigned to it, but people get confused by that, even though the computer doesn't.

That function I can not find in this setup for multiple buttons.

It isn't there. You need to create it. A function is just a named block of code.

void setPinStates(byte switchNumber, byte wireNumberOne, byte wireNumberTwo)
{
  currState[switchNumber] = debounce(prevState[switchNumber], switchNumber);
  if (currState == LOW && prevState == HIGH)
  {
    wire[wireNumberOne] = !wire[wireNumberOne];
    wire[wireNumberTwo] = !wire[wireNumberTwo];
  }
  prevState[switchNumber] = currState[switchNumber];
  digitalWrite(wireNumberOne, wire[wireNumberOne]);
  digitalWrite(wireNumberTwo, wire[wireNumberTwo]);
}

This is where having arrays, instead of individual variables, is extremely helpful.

The longer the code, the worse my overview and more possibilities to make mistakes.

Absolutely. That's why you create functions. Looking at the one above, it is pretty easy to determine whether it is working right, or not. If it is, then calling it 6 times means that if one switch/set of lights isn't behaving right, the contents of an array, or the calling arguments, are wrong, not the logic of the function. If none of the switches produces the desired behavior, then the problem is in the function - either syntactically or logically. But, there is one place to fix the problem, not 6 or 12.

If it would be possible I would find an pay someone to write this code for me, but Arduino and EL-Wire is very unknown here in The Netherlands so I can't find one.....

I don't have any EL-wire, but I have dozens of LEDs and resistors and 6 Arduinos. PM me. I'm sure we can get code working in less than a week.