[SOLVED]keypad function not working after using dtostrf, itoa

Hi guys. I have a strange problem.

I use this piece of code for a "wait" loop, the user has to press '#' to continue (with some message on LCD display).

lcd.clear();
   lcd.printIn ("To continue");
   lcd.cursorTo(2,0);
   lcd.printIn ("press #");
   char enter = kpd.getKey();
   while(enter != '#'){
        switch (enter)
        {
    case NO_KEY:
    break;
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
    case '*':
    break;
       }
     enter = kpd.getKey();
   }
   lcd.clear();
   lcd.printIn("Continue");

It works well, also after each loop of the program, but suddenly stops when i somewhere else use dtostrf or itoa. The first time it runs just fine, then it proceeds with some of the mentioned functions and after beginning of the second loop, the "wait" code does not work (if I press #, nothing happens).

Does samobody have a advice? Thank you

Does samobody have a advice?

Yes, post all your code.

Well, ok then :slight_smile:
It’s now just a sketch, for testing purposes, the final version will be a bit cleaner.
I drive a DAC (three of them) that sets some power MOSFETS, a PSU is then loaded with current and actual voltage on the outputs is read through analog inputs.

I’ve removed the part with itoa, so only dtostrf here:

http://dl.dropbox.com/u/6998529/code/code.pde

#include <stdlib.h>
#include <Keypad.h>
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2); 
#define BIT0 46
#define BIT1 47
#define BIT2 48
#define BIT3 49
#define BIT4 50
#define BIT5 51
#define BIT6 52
#define BIT7 53
#define WR12 45
#define WR5 44
#define WR3 43

char a[1];
char charBuf[4];
const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = { 
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = { 22, 23, 24, 25 }; 
byte colPins[COLS] = { 26, 27, 28 }; 
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // vytvorenie klávesnice

void setup()
{
  pinMode(BIT0,OUTPUT);
  pinMode(BIT1,OUTPUT);
  pinMode(BIT2,OUTPUT);
  pinMode(BIT3,OUTPUT);
  pinMode(BIT4,OUTPUT);
  pinMode(BIT5,OUTPUT);
  pinMode(BIT6,OUTPUT);
  pinMode(BIT7,OUTPUT);
  pinMode(WR12,OUTPUT);
  pinMode(WR5,OUTPUT);
  pinMode(WR3,OUTPUT);
  digitalWrite(WR12,HIGH);
  digitalWrite(WR5,HIGH);
  digitalWrite(WR3,HIGH);
  lcd.init();                             //inicializácia displeja
  lcd.commandWrite(0x0F);                 //zapnutie kurzora a jeho blikanie
  lcd.clear();                            //premazanie displeja
  Serial.begin(9600);                     //inicializácia sériovej komunikácie
}

void loop()
{

   nastav(1); 
     delay(1);
     digitalWrite(WR12,LOW);
     delay(1);
     digitalWrite(WR12,HIGH);
     delay(1);
   nastav(1); 
     delay(1);
     digitalWrite(WR5,LOW);
     delay(1);
     digitalWrite(WR5,HIGH);
     delay(1);
   nastav(1); 
     delay(1);
     digitalWrite(WR3,LOW);
     delay(1);
     digitalWrite(WR3,HIGH);
     delay(1);

   lcd.clear();
   lcd.printIn ("Zapnite zdroj");
   lcd.cursorTo(2,0);
   lcd.printIn ("a stlacte #");
   char enter = kpd.getKey();
   while(enter != '#'){
        switch (enter)
        {
    case NO_KEY:
    break;
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
    case '*':
    break;
       }
     enter = kpd.getKey();
   }
   lcd.clear();

   lcd.printIn("Zadajte hodnotu");
   lcd.cursorTo(2, 0);
   lcd.printIn("pre 12V:");
   int prud12 = citaj();
   if (prud12 > 100) prud12=99;   //podmienka pre obmedzenie maximalneho zadaneho prudu
   if (prud12 < 1) prud12=1;    // podmienka pre zadanie minimalneho prudu
   lcd.clear();
   lcd.printIn("Zadajte hodnotu");
   lcd.cursorTo(2, 0);
   lcd.printIn("pre 5V:");
   int prud5 = citaj();
   lcd.clear();
   lcd.printIn("Zadajte hodnotu");
   lcd.cursorTo(2, 0);
   lcd.printIn("pre 3.3V:");
   int prud3 = citaj();
   lcd.clear();
   

   
   lcd.printIn("Zacinam testovat");
   lcd.cursorTo(2, 0);
   lcd.printIn("o 3");
   delay(1000);
   lcd.clear();
   lcd.printIn("Zacinam testovat");
   lcd.cursorTo(2, 0);
   lcd.printIn("o 2");
   delay(1000);
   lcd.clear();
   lcd.printIn("Zacinam testovat");
   lcd.cursorTo(2, 0);
   lcd.printIn("o 1");
   delay(1000);
   lcd.clear();
     

   nastav(1); 
     delay(1);
     digitalWrite(WR12,LOW);
     delay(1);
     digitalWrite(WR12,HIGH);
     delay(1);
   nastav(1); 
     delay(1);
     digitalWrite(WR5,LOW);
     delay(1);
     digitalWrite(WR5,HIGH);
     delay(1);
   nastav(1); 
     delay(1);
     digitalWrite(WR3,LOW);
     delay(1);
     digitalWrite(WR3,HIGH);
     delay(1);

   
   lcd.printIn("Testujem");
   nastav(prud12); 
     delay(1);
     digitalWrite(WR12,LOW);
     delay(1);
     digitalWrite(WR12,HIGH);
     delay(1);
   nastav(prud5); 
     delay(1);
     digitalWrite(WR5,LOW);
     delay(1);
     digitalWrite(WR5,HIGH);
     delay(1);
   nastav(prud3); 
     delay(1);
     digitalWrite(WR3,LOW);
     delay(1);
     digitalWrite(WR3,HIGH);
     delay(1);
      
   delay(5000); 
   

   
   float volty12=analogRead(8);
   delay(20);
   float volty5=analogRead(9);
   delay(20);
   float volty3=analogRead(10);
   delay(20);
   float zvlnenie12=analogRead(11);
   delay(20);
   float zvlnenie5=analogRead(12);
   delay(20);
   float zvlnenie3=analogRead(13);
   

   
   nastav(0.5); 
     delay(1);
     digitalWrite(WR12,LOW);
     delay(1);
     digitalWrite(WR12,HIGH);
     delay(1);
   nastav(0.2); 
     delay(1);
     digitalWrite(WR5,LOW);
     delay(1);
     digitalWrite(WR5,HIGH);
     delay(1);
   nastav(0.1); 
     delay(1);
     digitalWrite(WR3,LOW);
     delay(1);
     digitalWrite(WR3,HIGH);
     delay(1);
   

   
   lcd.clear();
   lcd.printIn("Zdroj mozete");
   lcd.cursorTo(2, 0);
   lcd.printIn("vypnut");
   delay(3000);
   float volty12real=((volty12/1023)*5);
   float volty5real=((volty5/1023)*5);
   float volty3real=((volty3/1023)*5);
   float zvlnenie12real=((zvlnenie12/1023)*5);
   float zvlnenie5real=((zvlnenie5/1023)*5);
   float zvlnenie3real=((zvlnenie3/1023)*5);
   lcd.clear();
   lcd.printIn("12V:");
   lcd.cursorTo(1,4);
   lcd.printIn(dtostrf(volty12real, 2, 2, charBuf));
   lcd.cursorTo(2,0);
   lcd.printIn("5V:");
   lcd.cursorTo(2,3);
   lcd.printIn(dtostrf(volty5real, 2, 2, charBuf));
   delay(3000);
}

int citaj()
{
   int num = 0;
   char key = kpd.getKey();
   while(key != '#'){
        switch (key)
        {
    case NO_KEY:
    break;

    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      lcd.print(key);
      num = num * 10 + (key - '0');
      break;

    case '*':
        num = 0;
        lcd.clear();
        lcd.printIn("Zadajte hodnotu");
        lcd.cursorTo(2, 0);
        break;
       }
     key = kpd.getKey();
   }
   return num;
}

void nastav(int hodnota)
{
  digitalWrite(46, HIGH && (hodnota & B10000000));
  digitalWrite(47, HIGH && (hodnota & B01000000));
  digitalWrite(48, HIGH && (hodnota & B00100000));
  digitalWrite(49, HIGH && (hodnota & B00010000));
  digitalWrite(50, HIGH && (hodnota & B00001000));
  digitalWrite(51, HIGH && (hodnota & B00000100));
  digitalWrite(52, HIGH && (hodnota & B00000010));
  digitalWrite(53, HIGH && (hodnota & B00000001));
}

I’ve tested it literally line after line and the problem occurs only when I add dtostrf

There's a lot of code there to have suddenly found an error, but I don;t quite see where charBuf is written to.

Yes, lot of code, thats why I posted only part of it. It compiles and runs just fine, dtostrf does its job good (converts float to string) and I get the right result shown on display. But when void loop starts again, I can get no keypad input. Only reset helps.

dtostrf: "The caller is responsible for providing sufficient storage in s." Is four characters enough for the format you are printing?

   while(enter != '#'){
        switch (enter)
        {
    case NO_KEY:
    break;
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
    case '*':
    break;
       }
     enter = kpd.getKey();
   }

None of the cases do anything. Why are they here?

   lcd.printIn(dtostrf(volty12real, 2, 2, charBuf));

Two characters before the decimal point, the decimal point, two characters after the decimal point, and the trailing NULL. Hmmm, looks like more than 4 characters to me.

...a biig, nice facepalm for me XD I've changed charBuf to 10 and no problem now whatsoever. It worked with 4 characters when I did some testing outside the main code, but as it looks, not here.

AWOL, thank you!