Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button problem
#21
I tried a code with detecting single and long presses - it works.

Code:
//// from miniE.ino

#include <LiquidCrystal.h>
#define LCD_BACKLIGHT_PIN   10
#define LCD_KEY_PIN         A0  
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define  KEY_NONE            255
#define  KEY_RIGHT           0
#define  KEY_UP              1
#define  KEY_DOWN            2
#define  KEY_LEFT            3
#define  KEY_SELECT          4
#define  KEY_MENU            5

#define  key_0_val           50    
#define  key_1_val           200  
#define  key_2_val           400  
#define  key_3_val           600  
#define  key_4_val           800

////  from mE_keys.ino

#define  key_initial_delay   500    
#define  key_rate            150

byte lcd_key;
byte key = KEY_NONE;
byte oldkey = KEY_NONE;
int analogValue;

unsigned long key_rate_dynamic = key_rate;
unsigned long key_count;
unsigned long key_press_start;


int lcd_key_pressed() {
  analogValue = analogRead(LCD_KEY_PIN);
  delay(4);
  if (5 < abs(analogRead(LCD_KEY_PIN) - analogValue)) {
    return KEY_NONE;
  }
  key = lcd_get_key(analogValue);


  // no key pressed
  if (key == KEY_NONE) {
    oldkey = KEY_NONE;
    return KEY_NONE;
  }

  // single press detected
  if (key != oldkey) {
    key_count = 0;
    key_rate_dynamic = key_rate;    

    oldkey = key;  
    if (key >=0) {
      return key;
    }    
  }  

  // long-key-press
  if (key == oldkey) {
    if ( (key_press_start + key_initial_delay + (key_count * key_rate_dynamic)) < millis()) {          
      key_count++;
      if (key_count > 2) {
        key_rate_dynamic = round(key_rate - (key_count / 3));
        if (key_rate_dynamic < 0) {
          key_rate_dynamic = 0;
        }
      }          
      return key;
    }
    else {
      key_press_start = millis();
    }
  }
  return KEY_NONE;  
}


int lcd_get_key(unsigned int input) {

  if (input < key_0_val) {
    return 0;
  }
  else if (input < key_1_val) {
    return 1;          
  }
  else if (input < key_2_val) {
    return 2;  
  }
  else if (input < key_3_val) {
    return 3;  
  }
  else if (input < key_4_val) {
    return 4;  
  }
  else {
    return KEY_NONE;    
  }
}

void setup() {
  lcd.begin(16, 2);
  lcd.clear();
}

void loop() {
  analogWrite(LCD_BACKLIGHT_PIN, 150);
  
/// from mE_ui.ino


  lcd.setCursor(5,0);

  lcd_key = lcd_key_pressed();  

  if (lcd_key == KEY_NONE) {
    lcd.print("NONE  ");
  }
  if (lcd_key == KEY_RIGHT) {
    lcd.print("RIGHT ");
  }
  if (lcd_key == KEY_UP) {
    lcd.print("UP    ");
  }
  if (lcd_key == KEY_DOWN) {  
    lcd.print("DOWN  ");
  }
  if (lcd_key == KEY_LEFT) {
    lcd.print("LEFT   ");
  }
  if (lcd_key == KEY_SELECT) {
    lcd.print("SELECT");
  }
}

I try to find where in a code operation of buttons is slowed down.

The most interesting that my first acquaintance to arduino was in 2012 when I incidentally got on a site openmoco.org and saw your miniE project.
I made lcd shild, bought arduino uno and easydriver and established on my dolly - code was compiled and everything worked perfectly!
https://www.youtube.com/watch?v=oSk9LT6QtMc

Thank you for your big and useful work!

And now two days sit the computer and I can't understand a problem why buttons don't want to work with a code.
I established for test older arduino 1.05 version - any changes.
I made new shield according to dfrobot scheme - any changes.

I sit and look on the miniE main screen and it is very sadSad
Reply
#22
Ok, as I still don't know your custom-shield and your changes to the code, it is impossible to help directly.
But I can give you some general hints on how to track down code-errors on the Arduino:

Begin with this segment of code and check if it returns correct values:

Code:
analogValue = analogRead(LCD_KEY_PIN);
  delay(4);
  if (5 < abs(analogRead(LCD_KEY_PIN) - analogValue)) {
    return KEY_NONE;
  }

just change it to this and get some output on the serial port when pressing a key:

Code:
analogValue = analogRead(LCD_KEY_PIN);
  delay(4);
  if (5 < abs(analogRead(LCD_KEY_PIN) - analogValue)) {
    return KEY_NONE;
  }

  Serial.print("analog key read: ");
  Serial.println(analogValue);

If there is no output, you need to check why. If there is output, you need to track it further down in the code and try to find where it all goes wrong.

/Airic
Reply
#23
Thanks Airic.
I added
Serial.print("analog key read: ");
Serial.println(analogValue);
for control of pressing of buttons.

Serial monitor doesn't show any changes of analogValue.

But if I reset arduino with pressed and hold any button Serial monitor show real analogValue.

When I pressed and hold the button RIGHT and reset arduino on the lcd open menu of miniE.
When I pressed and hold the button SELECT and reset arduino on the main screen OFF changed on ON.

Where now to go?
Reply
#24
...sounds like your code does not go back to the key functions for any reason. Make sure that the function is really called regularly.
Reply
#25
I received today from ebay arduino Mega 2560.
I loaded miniE code through IDE 1.05 (old LiquidCrystal library) and now
SELECT button ON and OFF miniE.
But when press RIGHT button on the display there are hieroglyphs.
If I loaded through IDE 1.5 there are hieroglyphs at once.

There can be a problem in library?
Reply
#26
(07-03-2014, 09:04 PM)vvadim Wrote: I received today from ebay arduino Mega 2560.
I loaded miniE code through IDE 1.05 (old LiquidCrystal library) and now
SELECT button ON and OFF miniE.
But when press RIGHT button on the display there are hieroglyphs.
If I loaded through IDE 1.5 there are hieroglyphs at once.

There can be a problem in library?

I really don't think that there is a problem in the library - otherwise a lot more people would have the same problem. Could it be that your electrical connections are a little bit wacky?
Reply
#27
Hurrah, miniE work!!!!!!!!!!!!!!!!!!

I tested the old display 20х4 and loaded miniE code through IDE 1.05 (old LiquidCrystal library).
The problem appeared in the display and LiquidCrystal library.
Now it is necessary to pick up the display 16х2 and library.

Airic , excuse me for troubling.

Thanks for the help.
Thanks for miniE!!!!!!!!!!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)