Saturday, July 21, 2012

Switches and Arduino

Both pins are digital pin 3. The right one goes to the EL Wire
and the left one can go to whatever you want!
I was wiring up momentary switches to control some EL Wire when I discovered something interesting. The El Escudo Dos (Electroluminescent Wire sequencer) has double digital pins after you solder in the headers that allow it to stack onto an Arduino. What that means is you can assign one pin to multiple functions. If you connect a LED to the header pin and write it high you will see both the EL Wire on that pin and the LED light up. In my project linked above I unwittingly assigned the header pins as input for the switches and the same pins as output for the EL Wire, effectively programming them into a circuit so pressing the switch would automatically light the wire. I only figured this out because I wanted to program functions for different flashing sequences to each switch and they wouldn't work, just turn the wire on and off. I finally moved the switches to pins 6, 7, and 8 and everything worked. So it turns out all the code you need to make the EL Wire light up with a switch press is the following which simply assigns the input and output in setup:
int wirePin[] = {3,4,5};                // create array values corresponding to wire pinOuts
int switchPin[] = {3,4,5};              // same with switch pins

void setup() {
  for(int i = 0;i<3;i++) {
    pinMode(wirePin[i], OUTPUT);    // Set the wire pins as output
    pinMode(switchPin[i], INPUT);    // Set the switch pins as input
  }
}

void loop() {
//nothing needs to loop!
}
In the project linked above I initially had all that extra code because I was using the Lady Ada tutorial on programming switches with the Arduino and since on an Arduino each pin can only do one thing they used pin 2 for input and pin 12 for output. Their code had to use conditional statements to get the switch to work the LED.
Here is another program version using functions to assign different patterns to the 3 switches:

int wirePin[] = {3,4,5};                // create array values corresponding to wire pinOuts
int switchPin[] = {6,7,8};              // same with switch pins
int val[3];                        // variable for reading the pin status

void setup() {
  for(int i = 0;i<3;i++) {
    pinMode(wirePin[i], OUTPUT);    // Set the wire pins as output
    pinMode(switchPin[i], INPUT);    // Set the switch pins as input
    val[i] = 0;                    // Set val array to 0
  }
}

void loop(){
  for(int i = 0;i<3;i++) {
    val[i] = digitalRead(switchPin[i]);   // read input values and store it in val array
    if (val[0] == HIGH) {
      sequence(5);
    }
    if (val[1] == HIGH) {
      flash(5);
    }
    if (val[2] == HIGH) {
      alternate(2);
    }
    // check if buttons are not pressed
    if (val[i] == LOW) {
      digitalWrite(wirePin[i], LOW);    // turn wires off
    }
  }
}
  
void sequence(int times) {
  for(int i = 0;i<times;i++) {
    digitalWrite(wirePin[0], HIGH);
    delay(90);
    digitalWrite(wirePin[0], LOW);
    digitalWrite(wirePin[1], HIGH);
    delay(90);
    digitalWrite(wirePin[1], LOW);
    digitalWrite(wirePin[2], HIGH);
    delay(90);
    digitalWrite(wirePin[2], LOW);
  }
}

void flash(int times) {
  for(int i = 0;i<times;i++) {
    for(int j = 0;j<3;j++) {
      digitalWrite(wirePin[j], HIGH);
    }
    delay(100);
    for(int j = 0;j<3;j++) {
      digitalWrite(wirePin[j], LOW);
    }
    delay(100);
  }
}

void alternate(int times) {
  for(int i = 0;i<times;i++) {
      digitalWrite(wirePin[0], HIGH);
      digitalWrite(wirePin[1], HIGH);
      delay(170);
      digitalWrite(wirePin[0], LOW);
      digitalWrite(wirePin[1], LOW);
      digitalWrite(wirePin[2], HIGH);
      delay(170);
      digitalWrite(wirePin[2], LOW);

      digitalWrite(wirePin[2], HIGH);
      digitalWrite(wirePin[1], HIGH);
      delay(170);
      digitalWrite(wirePin[2], LOW);
      digitalWrite(wirePin[1], LOW);
      digitalWrite(wirePin[0], HIGH);
      delay(170);
      digitalWrite(wirePin[0], LOW);

      digitalWrite(wirePin[0], HIGH);
      digitalWrite(wirePin[2], HIGH);
      delay(170);
      digitalWrite(wirePin[0], LOW);
      digitalWrite(wirePin[2], LOW);
      digitalWrite(wirePin[1], HIGH);
      delay(170);
      digitalWrite(wirePin[1], LOW);
  }
}


No comments :