1

So I have to get this function done but I'm not so bright and got confused. (excuse me! I'm total noob)

The function is basically inputing text using five buttons, directional buttons and an ok button. The up and down buttons will be used to cycle through letters (a-z), and the left and right buttons will be used to control which array to cycle letters.

The control will be:

  • up button (up): ASCII-1 (z, y, x...)
  • down button (dw): ASCII+1 (a, b, c...)
  • right button (ri): array[i] ~> i+1 (go to next character/array)
  • left button (le): array[i] ~> i-1 (go to previous character/array)
  • ok button (ok): confirm text and changes the limit to stop function looping

I can't get it work with this code I write.

void intext()
{
  int lim=16;
  lcd.setCursor(0,1);
  lcd.print(input);
  for(i=0; i<lim ; )
  {
    if(digitalRead(dw)==HIGH)
    {
      if(input[i]<'a')
        input[i]='z';
      if(input[i]>'z')
        input[i]='a';
      input[i]=input[i]+1;
      delay(1000);
    }
    if(digitalRead(up)==HIGH)
    {
      if(input[i]<'a')
        input[i]='z';
      if(input[i]>'z')
        input[i]='a';
      input[i]=input[i]-1;
      delay(1000);      
    }
    if(digitalRead(ri)==HIGH)
    {
      delay(1000);    
      i=i+1;
    }
    if(digitalRead(le)==HIGH)
    {
      delay(1000); 
      i=i+1;
    }
    if(digitalRead(ok)==HIGH)
    {
      lim=i+1;
      delay(1000); 
    }
  }
}

void loop()
{
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("Type your text:");
  intext();
  lcd.setCursor(0,0);
  lcd.print("Success!!");
  lcd.setCursor(0,1);
  lcd.print(input);
}

The result I wanted it to be is like this: first you got the "type your text:" on the first row and blank in the second row. our cursor is in the first column of the second row, so we can cycle through the alphabet letters. if you press right you can edit the next column of second row. you can get to the previous column in second row by pressing left. you can input up to 15 characters. after done typing pressing ok will stop the typing and you will see "Success!!" in the first row and typed text in the second row.

Please help. I'm stumped. Thank you.

2
  • 1
    Could you clarify the current behavior (you just said it does not work but that does not give any leads where to investigate)? Also, it would be good to see the setup() code and the variables declaration. Finally, a description if your wiring would be nice too, in particular you did not mention if you use pullup or pulldown resistors.
    – jfpoilpret
    Jan 9, 2015 at 12:29
  • Thank you and sorry for not making things clear, I'm really clueless about things. I'm not very keen either so I just kind of trying to rewrite codes from scratch or trying to change how things will work. I guess you can check the complete code here pastebin.com/knbXsen4 ;; My design... I'm not too sure if this good or bad, but after I tested it, and it does need debouncing. Here's the picture i.imgur.com/KHn3zzh.png again, thank you for your points. I'll try to be more informative and clear next time. Jan 10, 2015 at 7:15

1 Answer 1

1

Several issues in the code you posted:

  • Every change to the text you do inside the for loop, will not be reflected to the LCD until the for loop will end (when i<limit will no longer be a true statement). You might want to add an additional code, inside the for loop and outside any of its conditional statements, that will repeatedly set the cursor and print input to the LCD. Something similar to the code you have prior to the for loop:

    void intext() 
    {
      ... // rest of the code until the for loop
    
      for (int i; i<limit; ) 
      {
        ... // all the conditions for up/down/left/right/ok
    
        lcd.setCursor(0,1);
        lcd.print(input);
      }
    }
    
  • Another problem is that when the user clicks left, the counter should be decremented, not incremented, and you should check for left boundary:

    if(digitalRead(le)==HIGH && i>0) // left boundary check added
    {
       delay(1000); 
       i=i-1; // and not i=i+1;
    
    }
    
  • After clicking Ok, and since the for loop does not increment i, the condition in the for loop (i<limit) will stay true, and the loop will not end until the user will click Right once more. To mitigate this, change the following

    if(digitalRead(ok)==HIGH)
    {
      lim=i; // and not: lim=i+1;
      delay(1000); 
    }
    

Otherwise, the user will have to click Right for completion, to increment i once more.

  • jfpoilpret comment about pull up/down is very important. Make sure you use pull up/pull down resistors in order to prevent input debounce (more details inside this link).
1
  • You're my light. Thank you. Sorry for not giving a quality comment. I'm just... in awe.. Jan 10, 2015 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.