Hi,
Let me start of by saying that I'm an absolute beginner when it comes to Arduino, coding, logic and whatnot. I've tried to stay in my analog domain as long as possible, but now it looks like I have to surrender...
I've been trying to come up with a solution for controlling a whole bunch of relays with a whole bunch of switches. This is the plan:
-
32 switches each turning a relay on/off, switching audio between 2 destinations. These should act like a "solo" function, meaning only one will be active at any time.
-
32 additional switches doing the same thing, although these should work independently of each other.
-
Each switch are momentary/non-latching and has a corresponding LED which should light up when the corresponding relay is on.
I've managed to do a version of this in very small scale with my Arduino Uno with 4 pushbuttons, a Darlington-array (ULN2003), 4 relays and 4 LEDs. So while I consider myself to have some basic knowledge of what's going on inside, scaling it up to 64 inputs and 64 outputs throws me off. Here's the code:
/* 4 switches selecting 1 output to be high, driving a ULN2003 with relay+LED
*/
int inPin1 = 2;Â Â Â Â // the number of the input pin
int outPin1 = 8;Â Â Â // the number of the output pin
int inPin2 = 3;Â Â Â Â // the number of the input pin
int outPin2 = 9;Â Â Â // the number of the output pin
int inPin3 = 4;Â Â Â Â // the number of the input pin
int outPin3 = 10;Â Â Â // the number of the output pin
int inPin4 = 5;Â Â Â Â // the number of the input pin
int outPin4 = 11;Â Â Â // the number of the output pin
int state1 = LOW;Â Â Â // the current state of the output pin
int reading1;Â Â Â Â Â // the current reading from the input pin
int previous1 = HIGH;Â Â // the previous reading from the input pin
int state2 = LOW;Â Â Â // the current state of the output pin
int reading2;Â Â Â Â Â // the current reading from the input pin
int previous2 = HIGH;Â Â // the previous reading from the input pin
int state3 = LOW;Â Â Â // the current state of the output pin
int reading3;Â Â Â Â Â // the current reading from the input pin
int previous3 = HIGH;Â Â // the previous reading from the input pin
int state4 = LOW;Â Â Â // the current state of the output pin
int reading4;Â Â Â Â Â // the current reading from the input pin
int previous4 = HIGH;Â Â // the previous reading from the input pin
long time = 0;
long debounce = 200;Â // the debounce time, increase if the output flickers
void setup()
{
 pinMode(inPin1, INPUT);
 pinMode(outPin1, OUTPUT);
 pinMode(inPin2, INPUT);
 pinMode(outPin2, OUTPUT);
 pinMode(inPin3, INPUT);
 pinMode(outPin3, OUTPUT);
 pinMode(inPin4, INPUT);
 pinMode(outPin4, OUTPUT);
}
void loop()
{
{Â reading1 = digitalRead(inPin1);
 if (reading1 == HIGH && previous1 == LOW && millis() - time > debounce) {
  if (state1 == HIGH)
   state1 = LOW;
  else
   state1 = HIGH;
   state2 = LOW;
   state3 = LOW;
   state4 = LOW;
  Â
  time = millis();
 Â
 }
 digitalWrite(outPin1, state1);
 previous1 = reading1;
}Â
{Â reading2 = digitalRead(inPin2);
 if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce) {
  if (state2 == HIGH)
   state2 = LOW;
  else
   state2 = HIGH;
   state1 = LOW;
   state3 = LOW;
   state4 = LOW;
  Â
  time = millis(); Â
 }
 digitalWrite(outPin2, state2);
 previous2 = reading2;
}Â
{Â reading3 = digitalRead(inPin3);
 if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) {
  if (state3 == HIGH)
   state3 = LOW;
  else
   state3 = HIGH;
   state1 = LOW;
   state2 = LOW;
   state4 = LOW;
  Â
  time = millis(); Â
 }
 digitalWrite(outPin3, state3);
 previous3 = reading3;
}
{Â reading4 = digitalRead(inPin4);
 if (reading4 == HIGH && previous4 == LOW && millis() - time > debounce) {
  if (state4 == HIGH)
   state4 = LOW;
  else
   state4 = HIGH;
   state1 = LOW;
   state2 = LOW;
   state3 = LOW;
  time = millis(); Â
 }
 digitalWrite(outPin4, state4);
 previous4 = reading4;
}
}
I've been trying to read up on shift registers and i2c port expanders, but it's unclear to me how to actually implement them in my code. Any tips or ideas on how to easily do that? I was hoping to be able to use an Arduino Mega from start, since the project started with just 16 ins/outs, but as everything doubled in size the pins on the Mega wasn't enough. That sucks, everything would have been very easy for me to look at and work with. I've looked around the forum but since I'm an idiot I'm having a hard time translating other peoples project into mine. So for a complete newbie, would you recommend going with shift registers or port expanders?
Thanks.