NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » lcd input text with buttons

February 05, 2011
by oxey
oxey's Avatar

Hello, Iv been experimenting with trying to use a few buttons to select numbers or text. Iv used some code from another project to get started. what I have so far is two button's that go back and forth and try and change a number. It appears to work accept the character on the screen is some random gibberish. Anyone know why I might be having problems?

<pre><code> #define F_CPU 14745600 #include <stdio.h> #include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <inttypes.h> #include "../libnerdkits/delay.h" #include "../libnerdkits/lcd.h" // PIN DEFINITIONS: // PC3 -- MENU DOWN PUSHBUTTON (pin 26) // PC4 -- MENU UP PUSHBUTTON (pin 27) int main() { // fire up the LCD lcd_init(); FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE); lcd_home(); // Set the 4 input pins (sinking inputs) DDRC &= ~(1<<PC3); // set PC3 as input (pin 26) DDRC &= ~(1<<PC4); // set PC4 as input (pin 27) // turn on the internal resistors for the input pins PORTC |= (1<<PC3); // turn on internal pull up resistor (pin 26) PORTC |= (1<<PC4); // turn on internal pull up resistor (pin 27) // declare the variables to represent each pushbutton input uint8_t menu_up; uint8_t menu_down; uint8_t j; // for selection indicator char str_buf[10][1] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."}; j=0; // start position selection indicator @ "output one" while(1) { menu_up = (PINC & (1<<PC4)) >> PC4; if (menu_up == 0) { if(j==0) j=10; if(j>0) //set 0 as lowest allowed count j = j - 1; // if count down pressed, lower count by one delay_ms(300); // switch "debounce" } menu_down = (PINC & (1<<PC3)) >> PC3; if (menu_down == 0) { j = j + 1; //if button pressed, increase count by one if (j >= 10 ) //set 0 as lowest allowed count, and if i is < 5, j = 0; //count loops to five delay_ms(300); // switch "debounce" } // Pushing pin 26 pushbutton advances to selection option(s). // Pushing pin 27 pushbutton backs up to selection option(s). lcd_line_one(); fprintf_P(&lcd_stream, PSTR("%c"),str_buf[j]); } return 0; } </code></pre>

Has anyone done anything like this before? I also need to figure out after selecting the text to hit a button. move over and redo the selection process and finally, a third button to return or to save the whole thing to string. Any help would be greatly appreciated.

February 05, 2011
by oxey
oxey's Avatar
#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

// PIN DEFINITIONS:

// PC3 -- MENU DOWN PUSHBUTTON (pin 26)
// PC4 -- MENU UP PUSHBUTTON (pin 27)

int main() {
// fire up the LCD
lcd_init();
FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
lcd_home();

// Set the 4 input pins (sinking inputs)
DDRC &= ~(1<<PC3); // set PC3 as input (pin 26)
DDRC &= ~(1<<PC4); // set PC4 as input (pin 27)

// turn on the internal resistors for the input pins 
PORTC |= (1<<PC3); // turn on internal pull up resistor (pin 26)
PORTC |= (1<<PC4); // turn on internal pull up resistor (pin 27)

// declare the variables to represent each pushbutton input
uint8_t menu_up;
uint8_t menu_down;
uint8_t j; // for selection indicator

char str_buf[10][1] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."};

j=0; // start position selection indicator @ "output one"

while(1) {

menu_up = (PINC & (1<<PC4)) >> PC4;
if (menu_up == 0)

{   if(j==0)
    j=10;
    if(j>0)         //set 0 as lowest allowed count
    j = j - 1;      // if count down pressed, lower count by one
    delay_ms(300);   // switch "debounce"
}

menu_down = (PINC & (1<<PC3)) >> PC3;
if (menu_down == 0)

{   j = j + 1;      //if button pressed, increase count by one
    if (j >= 10 )    //set 0 as lowest allowed count, and if i is < 5,
    j = 0;           //count loops to five
    delay_ms(300);   // switch "debounce"
}

// Pushing pin 26 pushbutton advances to selection option(s).
// Pushing pin 27 pushbutton backs up to selection option(s).

lcd_line_one(); 
fprintf_P(&lcd_stream, PSTR("%c"),str_buf[j]);

}

return 0;

}
February 06, 2011
by Rick_S
Rick_S's Avatar

It very well may have to do with your array.

See this LINK for some good discussion on what works and what won't in arrays.

Plus, even if it would work, you dimensioned it as [10][1], then had 11 characters in it.

If you are just raising and lowering a number 0 thru 9, why not just use j to display instead of an array[j]. Since j has the value you want anyway.

fprintf_P(&lcd_stream, PSTR("%d"),j);

Just a thought

Rick

February 07, 2011
by SpaceGhost
SpaceGhost's Avatar

oxey, I tried the code the way you posted out of curiosity. It appears to call up some Greek letters and other symbols... Very interesting!

Removing line #37 from your code, and changing line #66 to what Rick suggested above will give you a simple 0-9 up/down counter.

But obviously you are wanting to accomplish more than just that.

In the code that you posted there are two inputs used to move your count up or down ("menu_up, menu_down"). You mentioned a "need to figure out after selecting the text to hit a button (a third button?) move over and redo the selection process and finally, a third button (do you mean fourth?) to return or to save the whole thing to string."

I'm not clear exactly about what you're wanting to do. How many inputs ("buttons") do you figure your project would entail, and could you give a description of how these inputs would function? How do you wish to "select" these "numbers or text"? And then do what?

Could you give more of a description of what exactly you want your project to be (or do)?

Dave

February 09, 2011
by oxey
oxey's Avatar

I am attempting to have a system for entering or changing text on the lcd. I was thinking of a three button system. One for cursory over, one for shifting up through individual text and a final one for a select or enter key. I was thinking I would create a menu structure as well. I have already done a basic one but I am stuck with the entering and editing or text in the screen. Perhaps I need more buttons? I am open to any suggestions. Thanks

February 09, 2011
by Ralphxyz
Ralphxyz's Avatar

oxey, I believe Rick's button routine in his DS3232 RTC code is exactly what you are looking for.

Even without a DS3232 you can load his code to/and use the buttons:

"One for cursory over, one for shifting up through individual text and a final one for a select or enter key".

Ralph

Post a Reply

Please log in to post a reply.

Did you know that NerdKits make a great gift? Learn more...