Help with coding. Turning on one led at a time with respective buttons.

Hello Guys, new "noob" here =).

I´m doing this project for college and i need some help. The project consist on a midi controller, a.k.a the device that i made.

This device has 5 buttons and 5 leds,plus 5 potentiometers. The potentiometers are all ok, they´re doing what they should.
Here is the problem. I want every button to turn on a single led, the led is positioned on the side. I´ve used 5 digital inputs for leds and 5 for the buttons. The trick part (for me at least) is that i want that whenever a button is pressed and the respective led turned on, every other led will remain off, so every time i preesed a button just the led that correspond to that button will be lighted up. This is like a selector actually. On the other hand, i have a programmed patch in Ableton live. There are 4 different sound effects, so the point of the device is to select only one of the effects. Thie fifth button is for a bypass, asuming everyone know what a bypass is, i need one button to function as such, and whenever pressed, only one led will be turned on and the other ones will be turned off.

Hope i make myself clear. I´m from chile so my english may be not that good (it´s not good i think). I´ll post a photo of the device. The red button is for the bypass, and the led corresponding is in the corner.

I´ll post my starting point code. I have every led turning on with their respective button, but in order to turn off the led, i have to push the button again.

Thanks in advantage for every man/woman/reptilian/alien who give themselve time to help a newbie like me. Meanwhile, i´m goin to try to find the answer.

thanks Guys.

const int LED12 = 12;
const int LED11 = 11;
const int LED10 = 10;
const int LED9  = 9;
const int LED8  = 8;

const int BUTTON6 = 6;
const int BUTTON5 = 5;
const int BUTTON4 = 4;
const int BUTTON3 = 3;
const int BUTTON2 = 2;

int STATE2 = 0;
int STATE3 = 0;
int STATE4 = 0;
int STATE5 = 0;
int STATE6 = 0;


int OLD_VALUE2 = 0;
int OLD_VALUE3 = 0;
int OLD_VALUE4 = 0;
int OLD_VALUE5 = 0;
int OLD_VALUE6 = 0;

long pot0;
long pot1;
long pot2;
long pot3;
long pot4;


void setup() {

  Serial.begin(9600);
  Serial.println("inicio de sketch - valores del potenciometro");
  
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
  digitalWrite(LED10, LOW);
  digitalWrite(LED11, LOW);
  digitalWrite(LED12, LOW);
  
  pinMode(LED12, OUTPUT);
  pinMode(LED11, OUTPUT);
  pinMode(LED10, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(LED8, OUTPUT);
  
  pinMode(BUTTON6, INPUT);
  pinMode(BUTTON5, INPUT);
  pinMode(BUTTON4, INPUT);
  pinMode(BUTTON3, INPUT);
  pinMode(BUTTON2, INPUT);}



void loop() { 

  pot0 = analogRead(A0)/8;
  pot1 = analogRead(A1)/8;
  pot2 = analogRead(A2)/8;
  pot3 = analogRead(A3)/8;
  pot4 = analogRead(A4)/8;

  Serial.print("El valor es = ");
  Serial.println(pot0);
  Serial.println(pot1);
  Serial.println(pot2);
  Serial.println(pot3);
  Serial.println(pot4);
  delay(50);

     
  int val2 = digitalRead(BUTTON2);
  if ((val2 == HIGH) && (OLD_VALUE2 == LOW)){
    STATE2 = 1-STATE2;
    delay(10); }
    OLD_VALUE2 = val2;
    if (STATE2 == 1) {
      digitalWrite(LED8, HIGH);}
      else { digitalWrite(LED8, LOW);}

  
      

 int val3 = digitalRead(BUTTON3);
  if ((val3 == HIGH) && (OLD_VALUE3 == LOW)){
    STATE3 = 1-STATE3;
    delay(10); }
    OLD_VALUE3 = val3;
    if (STATE3 == 1) {
      digitalWrite(LED9, HIGH);}
      else { digitalWrite(LED9, LOW);}

      int val4 = digitalRead(BUTTON4);
  if ((val4 == HIGH) && (OLD_VALUE4 == LOW)){
    STATE4 = 1-STATE4;
    delay(10); }
    OLD_VALUE4 = val4;
    if (STATE4 == 1) {
      digitalWrite(LED10, HIGH);}
      else { digitalWrite(LED10, LOW);}

      int val5 = digitalRead(BUTTON5);
  if ((val5 == HIGH) && (OLD_VALUE5 == LOW)){
    STATE5 = 1-STATE5;
    delay(10); }
    OLD_VALUE5 = val5;
    if (STATE5 == 1) {
      digitalWrite(LED11, HIGH);}
      else { digitalWrite(LED11, LOW);}

      int val6 = digitalRead(BUTTON6);
  if ((val6 == HIGH) && (OLD_VALUE6 == LOW)){
    STATE6 = 1-STATE6;
    delay(10); }
    OLD_VALUE6 = val6;
    if (STATE6 == 1) {
      digitalWrite(LED12, HIGH);}
      else { digitalWrite(LED12, LOW);}
}

Hi,
Welcome to the forum.

Karma for using code tags.

Instead of lots of if statements, look at this function. switch case.

Also google arduino switch case

Tom.. :slight_smile:

Hey tom!

Thanks a lot man =).

I´m gonna try that out and i´ll post the progress.

Cheers!.

I don't think a switch case is what you need.

I think you need to learn to use arrays. This will shrink your code down considerably.

To achieve your "selector" idea, you need to replace your STATE2 ... STATE6 variables (which currently can only have values 0 or 1) with a single STATE variable which can have several values like 0 to 5.

Hey paul.

Maybe you are right. I´ve read a couple of docs and view examples and in every one of them they use switch case for a single variable. I dont know exactly if i can achieve the desire effect having more than one. Kinda looks like i have to use a switch case sceneario for every led/button duo, probably i´m wrong.

In the array situation, i´ve already test that option, giving me no more than a headache jajaj. But not for the difficulty, but for my lack of knowledge on the matter. Though i do wanna try everything i can.

What do you guys think i should do? to keep it short and simplest as posible?

Also i didn´t quite understand what you meant about the STATE variables, couldyou give some begginer example?

Hi,
You can assign binary values to your buttons.
Button1 = 1
Button2 = 2
Button3 = 4
Button4 = 8
Button5 = 16

Add the value of the PRESSED buttons, each total will be unique to any button press combination.

But arrays may be easier.

Tom... :slight_smile:

What state should the device be in at startup? Bypass? Which button pin and which led pin is for bypass?

What do you guys think i should do? to keep it short and simplest as posible?

Simpler is best. Shorter would be simpler, in this case, as in most real cases.

Thanks to both of you guys.

I don´t know if i can make the shortest and simplest sketch, but i´m trying to. Right now i´m using both "switch case" and arrays.

Hope this is the way to go, otherwise, i´ll keep trying until i make it.

Thanks a lot.

I´ll post the progress soon.

Try this:

#define POTS 5

const byte ledPin[POTS]    = {12, 11, 10,  9,  8};
const byte buttonPin[POTS] = { 6,  5,  4,  3,  2};
const byte potPin[POTS]    = {A0, A1, A2, A3, A4};

byte state = 0;
int potValue[POTS];

void setup() {

  Serial.begin(9600);
  Serial.println("inicio de sketch - valores del potenciometro");

  for (byte i = 0; i < POTS; i++) {
    pinMode(ledPin[i], OUTPUT);
    digitalWrite(ledPin[i], LOW); //Not neccessary, pins default to LOW
    pinMode(buttonPin[i], INPUT); //Not neccessary, pins default to INPUT 
  }

  digitalWrite(ledPin[state], HIGH);

}

void loop() {

  Serial.print("El valor es = ");
  
  for (byte i = 0; i < POTS; i++) {
    
    potValue[i] = analogRead(potPin[i]) / 8;
    Serial.println(potValue[i]);

    if (digitalRead(buttonPin[i]) == HIGH) {
      if (state != i) {
        digitalWrite(ledPin[state], LOW);
        state = i;
        Serial.print("State is now ");
        Serial.println(state);
        digitalWrite(ledPin[state], HIGH);
      }      
    }
  }

  delay(50);

}

Dude!

You are amazing! It worked perfectly! That was all i needed.
Man i wish i could do this kind of things that fast. I don´t know how to thank you.

Can you give me a tip on how to improve? or where should i look the info? Maybe you study, how can i get to your level? haha

really, thanks a lot. Please tell me how can i improve, and i let you go ajjaja

Thanks!!

As with everything in life: study and practice. I began coding at school, when the school received its very first computer in the early 1980's. I studied coding at university, among other things. I spent long periods of my career as a programmer. I am now over half a century old and I still code as part of my work role today, using a language called Scala, which I have been learning for over a year now, and still have more to learn. But I have colleagues who are half my age who are as good at coding as I am, if not better, although I can still teach them a thing or two from time to time.

It is also very helpful to have a mentor to review your work, both when you are stuck and when you think you have done things perfectly. There are many experts on this forum who will be quite willing to help in this way.

It is important to me, having spent a few minutes fixing your code, that you understand every line that I changed. So please study it carefully and ask any questions you cannot figure out for yourself.

I cannot thank you enough for this.

As you say, a mentor may be the best thing to have when learning something new. I´ve spent more hours in my college library than in my own home studying several topics of my interests. Sometimes the learning part comes really easy, but sadly, most of the time it´s kinda hard to pull off the auto teaching concept. Anyway, thanks for the advise. I consider myself a motivated nerd hahah, so i´ll keep up the hard work to be able to do this things faster. Actually, i´ve noticed that i didn´t tell you what i´m studying haha. I´m finishing my technic proffesional sound ingeneering career, it has nothing to do whit programming. But i´ve been always draged by the mazing world of electronics and computer stuff. Since i was a little in fact.

I´m going to study the code that you gave me. I thank you again. If there´s anything i can´t understand, this topic wilñl have another post soon hahah.

Thank you Paul. Have a nice holydays you and your family.

Cheers.