Go Down

Topic: code conflict trouble LCD and analog read? (Read 113 times) previous topic - next topic

azuredrakeATM

hello all,

I was hoping someone could help me with a bit of trouble with a project I've been working on. I am attempting to use a 16x2LCD with a 595 shift register and  an analog four pad button (each button has a resistor so each button push give a different reading). I worked through the logic to get the buttons to adjust the two values I wish to control in the manner I liked, however now that I am trying to add the LCD commands I am seeing the code stop working. The LCD commands that I have inserted seem to work but the code with the if and while statements for the buttons seem to no longer perform as they did before inserting the code. Any help would be greatly appreciated.

thank you
Code: [Select]
/*
  AnalogButton_Combos
  Version 0.1

  Connection more then one button to a single analog pin. Utilizing
  software debounce to prevent registering multiple button press
  while allow for 2 button combos to be registered.

  The Circuit:
  Most other analog buttons circuits call for the resistors to be
  lined up in series from ground. The analog pin and each button
  connect off one of the resistors. My cuicuit requires that the
  resistors tie in from +5 to the buttons. The buttons all connect
  to the analog pin which is tied to ground threw a 1k resistor as
  seen in the diagram below.

         Analog pin 5
            |
  Ground--1K--|--------|--------|-------|-------|
            |        |        |       |       |
           btn1     btn2     btn3    btn4    btn5
            |        |        |       |       |
         220 Ohm  390 Ohm  680 Ohm   2.2K    4.7K
            |--------|--------|-------|-------|-- +5V

  Created By: Michael Pilcher
  February 24, 2010

*/
#include <LiquidCrystal595.h>
LiquidCrystal595 lcd(2, 3, 4);
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and there high and low values
int Button[4][3] = {
  {1, 150, 180}, // button 1
  {2, 500, 530}, // button 2
  {3, 680, 710}, // button 3
  {4, 220, 240}
}; // button 4
/*  {5, 178, 179}, // button 5
  {6, 91, 92}, // button 6
  {7, 896, 897}, // button 1 + button 2
  {8, 877, 878}, // button 1 + button 3
  {9, 851, 852}, // button 1 + button 4
  {10, 844, 845}, // button 1 + button 5
  {11, 840, 841}, // button 1 + button 6
  {12, 821, 822}, // button 2 + button 3
  {13, 769, 770}, // button 2 + button 4
  {14, 753, 754}, // button 2 + button 5
  {15, 745, 746}, // button 2 + button 6
  {16, 674, 675}, // button 3 + button 4
  {17, 643, 644}, // button 3 + button 5
  {18, 627, 627}, // button 3 + button 6
  {19, 408, 409}, // button 4 + button 5
  {20, 363, 364}, // button 4 + button 6
  {21, 243, 243}}; // button 5 + button 6
*/
int analogpin = 0; // analog pin to read the buttons
int label = 0;  // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0;  // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0;  // the debounced input value

int ButtonVal;
int mode = 0;
boolean loopmode = false;
int leftval = 0;
int rightval = 0;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.blink();
}

void loop()
{
  buttonTest();

  if (mode != 0) {
    lcd.setCursor(0, 0);// these lines cause problems
    lcd.print("leftval ");//problem
    lcd.setCursor(0, 1);//problem
    lcd.print("riteval ");// problem
   

    if (mode == 2) {

      while (loopmode == true || mode == 2) {
        loopmode = true;
        Serial.println("leftmode");
        mode = 0;
        /*
          lcd.setCursor(0, 0);
          lcd.print("leftval ");
          lcd.print(leftval);
          lcd.setCursor(0, 1);
          lcd.print("riteval ");
          lcd.print(rightval);
        */
        buttonTest();
        if (mode == 1) {
          break;
        }
        if (mode == 2) {
          loopmode = false;
          mode = 0;
          lcd.clear();
          break;
        }
        if (mode == 3) {
          leftval --;
          Serial.print(leftval);
          mode = 0;
        }
        if (mode == 4) {
          leftval ++;
          Serial.print(leftval);
          mode = 0;
        }

      }

    }

    if (mode == 1) {
      while (loopmode == true || mode == 1) {
        loopmode = true;
        Serial.println("rightmode");
        mode = 0;
        /*
           lcd.setCursor(0, 0);// the code causes problems here too
           lcd.print("leftval ");
           lcd.print(leftval);
           lcd.setCursor(0, 1);
           lcd.print("riteval ");
           lcd.print(rightval);
        */
        buttonTest();
        if (mode == 2) {
          break;
        }
        if (mode == 1) {
          loopmode = false;
          mode = 0;
          lcd.clear();
          break;
        }
        if (mode == 3) {
          rightval --;
          Serial.print("rightval");
          Serial.print(rightval);
          mode = 0;
        }
        if (mode == 4) {
          rightval ++;
          Serial.print("rightval");
          Serial.print(rightval);
          mode = 0;
        }
      }
    }
    if (mode > 2) {
      mode = 0;
      lcd.clear();
    }
  }

  Serial.println(mode);


}

void buttonTest(){
 // If we have gone on to the next millisecond
  if (millis() != time)
  {
    // check analog pin for the button value and save it to ButtonVal
    ButtonVal = analogRead(analogpin);
    if(ButtonVal == current_state && counter >0)
    {
      counter--;
    }
    if(ButtonVal != current_state)
    {
      counter++;
    }
    // If ButtonVal has shown the same value for long enough let's switch it
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = ButtonVal;
      //Checks which button or button combo has been pressed
      if (ButtonVal > 0)
      {
        ButtonCheck();
      }
    }
    time = millis();
  }


 
}


void ButtonCheck()
{
  // loop for scanning the button array.
  for(int i = 0; i <= 4; i++)
  {
    // checks the ButtonVal against the high and low vales in the array
    if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
    {
      // stores the button number to a variable
      label = Button[i][0];
      Action();     
    }
  }
}

void Action()
{
  if(label == 1)
  {
    mode = label;
//   Serial.println("right Button");
  }
  if(label == 2)
  {
    mode = label;
 // Serial.println("left Button");
  }
  if(label == 3)
  {
    mode = label;
 //  Serial.println("down Button");
  }
  if(label == 4)
  {
    mode = label;
 //  Serial.println("up Button");
  }
  /*
  if(label == 5)
  {
    Serial.println("Action Button #1");
  }
  if(label == 6)
  {
    Serial.println("Action Button #2");
  }
  if(label == 8)
  {
    Serial.println("Left and Up Buttons");
  }
  if(label == 9)
  {
    Serial.println("Right and Up Buttons");
  }
  if(label == 12)
  {
    Serial.println("Left and Down Buttons");
  }
  if(label == 13)
  {
    Serial.println("Right and Down Buttons");
  }
  if(label == 21)
  {
    Serial.println("Action Buttons #1 and #2");
  }   
       */
  //Serial.println("Button =:");
  //Serial.println(label);
  //delay(200);
 
 
}




DrDiettrich

A4 and A5 also are used for I2C communication. Try a different pin.

If you get unexpected analog values, find out whether these are stable or random. Then either update your table or your circuit.

Go Up