Better Coding Technique?

I am in no ways a good code writer. What I have is a wii nunchuck attached to my arduino as well as and LCD attached to the arduino. I have modified the nunchuck library to use button c, z, and the joystick, and return as boolean for all.

The following are boolean: chuck.zPressed(); chuck.cPressed(); chuck.rightJoy(); chuck.leftJoy(); chuck.upJoy(); chuck.downJoy();

The problem is in my tortuous coding technique

  lcd.setCursor(0,0);
  if (chuck.cPressed()) {
    lcd.print("C");
  }
  else if (chuck.zPressed()) {
    lcd.print("Z");
  }
  else if (chuck.rightJoy()) {
    lcd.print("Right");
  }
  else if (chuck.leftJoy()) {
    lcd.print("Left");
  }
  else if (chuck.upJoy()) {
    lcd.print("Up");
  }
  else if (chuck.downJoy()) {
    lcd.print("Down");
  }
  else {
    lcd.clear();
  }

The above works just fine, but it's also possible to have button C, button Z, upJoy, and leftJoy all be true at the same time. In the future, I want to implement combo buttons.

Writing like this is beyond tortuous considering how many combinations there are

  if (chuck.cPressed() && chuck.zPressed() && chuck.upJoy() && chuck.leftJoy()) {
    lcd.print("C and Z and Up and Left");
  }
  else if (chuck.zPressed()) {
    lcd.print("Z");
  }

The number of else if branches would be very long but it would work. Is there a better method? If not, I'll just do all the combinations.

Lastly, when combo buttons are being pressed, they are not pressed all at the same time. Is there a way for me to say:

if (chuck.cPressed()) {
    wait for 1 second to check for more button presses
    do stuff
}

I'm sure I will learn alot! Thank you very much!

if (chuck.cPressed()) {
    lcd.print("C");
  }
  else if (chuck.zPressed()) {

The "else" implies that cPressed and zPressed are mutually exclusive, but you have told us that they are not:

if (chuck.cPressed()) {
    lcd.print("C");
}
if (chuck.zPressed()) {
    lcd.print("Z");
}

They are not. They can be used in combos or individually. I only used else if because I didn’t know better. I wanted the last else statement to be lcd.clear. Are you implying that I should use if statements and get rid of else if’s? That could be done. But I’m not sure if that would make it any simpler. :astonished:

Thanks

You could combine the button states into an integer. So, button C is bit 0, button Z is bit 1 etc. If you're not sure about bit manipulation, use "bitWrite"

I wasn't sure what you meant and was even more confused when I looked it up. Someone should add examples to the reference page. With guessing I managed to figure it out! This new code is much much more user-friendly! Thanks!

  lastTest = test;
  
  bitWrite(test,0,chuck.cPressed());
  bitWrite(test,1,chuck.zPressed());
  bitWrite(test,2,chuck.rightJoy());
  bitWrite(test,3,chuck.leftJoy());
  bitWrite(test,4,chuck.upJoy());
  bitWrite(test,5,chuck.downJoy());
  
  if(lastTest != test) {
    lcd.clear();
  } 
  lcd.setCursor(0,0);
  lcd.print(test);

It returns a unique number for every combo. The basic buttons are 1,2,4,8,16,32. The combos are the addition of them. With that, I could possibly use the simple switch case for designating what to do for each combo.

As for the last part, "wait for 1 second to check for more button presses", I haven't figured out a good method yet. I'm considering designating button C as the combo activator. Meaning that I have to hold down C to pause bitWrite and press all the botton combos and then release C to run bitWrite. That would prevent it from immediately executing one of the button's funtions when I haven't finished pressing my combos.

The bitWrite function certainly is interesting! Thanks alot!

For the button timeout, you can make a variable:
unsigned long Button_Timer;

Button_Timer = millis();

do {
// your button press monitoring goes here. When a button is pressed you put in a
Button_Timer = millis();

}
while (millis() - Button_Timer < 1000);

Once no buttons have been pressed for over 1000 milliseconds, it exits the DO WHILE loop.

In case any of you were wondering, I solved my button problem. Here is the flow chart:

And here the important part of the code. (The entire code is huge and unrelated)

void buttons() {
  switch (chuckBit) {           //chuckBit is the result of AWOLs excellent recommendation. Returns a number for every combo.
    case 1:
      comboCheck(); 
      if(switch_01) {            //switch as seen in flowchart
        wait = false;             //wait is boolean
        lcd.clear();
        lcd.print("Case 1");
        delay(100);
      }
      switch_01 = true;         //This is needed or else multiple presses wont work
      break;
    case etc...
}

void comboCheck() {
  unsigned long comboTime = millis();
  unsigned long comboWait = millis() + 500;
  while((comboTime < comboWait) && wait) {           //loop this whole while code for 500ms
    comboTime = millis(); 
    chuckdata();                                                    //refresh the incoming wii nunchuck data
    if(lastBit != chuckBit) {                                      //if the data is different, means a new button has been pressed or released
      switch_01 = false;                                           //do not execute the original plan
      break;                                                          // lets get outa this while loop immediately!
    }
    else {
      lcd.setCursor(0,0);
      lcd.print("Ready");                                          //same thing that was said on lcd before any buttons were pressed
    }
  }
}