Hi,
I am struggling with adding in another LED into the code as well as being able to change the code to get one led to blink in a specific pattern and another LED to fade
What I have currently
button 1 for turning LED 1 Off and ON
Button 2 to turn up the brightness of LED 1 and button 3 to turn down the brightness of LED 1
Overall I want to achieve;
button 1 for turning LED 1 & 2 off and on,
also, button 1 should be able to cycle through 3 different cases where LED 1 blink in different patterns and LED 2 fades in all of the cases. (ideally, I would like that if you held button 1 down for a long press that it will turn off no matter where it is in the cases - but not sure if that's possible)
and button 2 to turn up the brightness of LED 1 and with button 3 to turn down the brightness of LED 1 (button 2 & 3 do not interact with LED 2)
The code originally belongs to EEEnthusiast which I have been able to add an extra button into (so that I have 3 buttons) as well as more cases for that button. however, My understanding of ardunio and c++ isn’t great as just started learning on Wednesday! I have tried writing the code from scratch using debouncing and blink without delay after going through the basic tutorials and changing them however haven’t been able to get very far! This is the closest I’ve been able to get thanks to EEEnthusiast’s!
Here is my current code. Any help in how I can approve this, any overall advice, or simply, where am I going wrong?
const int numOfInputs = 3;
const int inputPins[numOfInputs] = {2,3,4};
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++;
}
}else if(input == 2) { // 7,8,9,10,11
if(LEDState == 0 || LEDState == 1 || LEDState > 10) {
LEDState = 7;
}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;
case 7:
analogWrite(outputPin, 255);
break;
case 8:
analogWrite(outputPin, 155);
break;
case 9:
analogWrite(outputPin, 100);
break;
case 10:
analogWrite(outputPin, 70);
break;
case 11:
analogWrite(outputPin, 30);
break;
default:
break ;
}
}