I'm new to arduinos and have not been able to find/create a working bit of code for my project. I'd like to control several different relays (standard 5v optical relays) with push buttons. I want one pushbutton1 to turn on/off relay1, pushbutton2 to turn on/off relay 2, etc.
I ran into troubles using delay statements as debouncers because I guess it locked out the remainder of the code while it ran? I then tried to use a flagging approach covered here:
const int numOfInputs = 2;
const int inputPins[numOfInputs] = {2,3};
const int outputPin = 10;
int LEDState = 0;
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW};
int inputCounters[numOfInputs];
long lastDebounceTime[numOfInputs] = {0,0};
long debounceDelay = 50;
void setup() {
for(int i = 0; i < numOfInputs; i++) {
pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
}
void loop() {
setInputFlags();
resolveInputFlags();
resolveOutputs();
}
void setInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
void resolveInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
if(inputFlags[i] == HIGH) {
// Input Toggle Logic
inputCounters[i]++;
updateLEDState(i);
printString(i);
inputFlags[i] = LOW;
}
}
}
void printString(int output) {
Serial.print("Input ");
Serial.print(output);
Serial.print(" was pressed ");
Serial.print(inputCounters[output]);
Serial.println(" times.");
}
void updateLEDState(int input) {
// input 0 = State 0 and 1
if(input == 0) {
if(LEDState == 0) {
LEDState = 1;
}else{
LEDState = 0;
}
// input 1 = State 2 to 6
}else if(input == 1) { // 2,3,4,5,6,2,3,4,5,6,2,
if(LEDState == 0 || LEDState == 1 || LEDState > 5) {
LEDState = 2;
}else{
LEDState++;
}
}
}
void resolveOutputs() {
switch (LEDState) {
case 0:
digitalWrite(outputPin, LOW);
break;
case 1:
digitalWrite(outputPin, HIGH);
break;
case 2:
analogWrite(outputPin, 30);
break;
case 3:
analogWrite(outputPin, 70);
break;
case 4:
analogWrite(outputPin, 100);
break;
case 5:
analogWrite(outputPin, 155);
break;
case 6:
analogWrite(outputPin, 255);
break;
default:
break;
}
}
But was unable to alter the code to get it to work... could someone help me write a bit of code which will allow me to do what I want it to do? I'd rather not buy a matrix-style shield to accomplish this, but I will if I have to.
What I want to do:
- Turn on relay1 by pressing momentary pushbutton1
- Turn off relay1 by pressing momentary pushbutton1
- Turn on relay2 by pressing momentary pushbutton2
- Turn off relay2 by pressing momentary pushbutton2
- etc.
- etc.
Thank you in advance!