Adding a decimal

Hi everyone,

I have a code which I wrote with help from some nice people on the forum a few years ago. It has been working fine up until now, but I urgently need some help with adding something to it.

The code is used to control up to 24 relays in a infinite loop with a Mega 2560, where I can adjust the time between every relay, and how long the relay stays on for. The relays control blower motors.

I have it so that I can enter seconds for the delay between each relay, as well as seconds for each relay to be on.

What I need now is help with a decimal point for the delay between each relay. How will I do this?
I've been looking around and read about floating numbers, and various other options, but I'm not sure which one will work for my application. I would like two decimal points and was wondering how can I get the display to show 000.00 and when I enter the number on the keypad it comes in from right to left and replaces the 0s.

The hardware that I have is a Mega, 3.2' screen, relays and a 4x4 keypad.
The code that I'm working with at the moment includes the following for the keypad.

Code: [Select]

// Keypad
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
unsigned int InputNumber;
int pressedKeyCount = 0;

Code: [Select]

char key = keypad.getKey();
void IncreaseNewTime(char key)
{
  if (CurrentMode == SetOffTimeMode || CurrentMode == SetOnTimeMode)
  {
    InputNumber = InputNumber * 10 + key - '0';
    pressedKeyCount ++;
    myGLCD.setColor(VGA_RED);
    myGLCD.printNumI(InputNumber, CENTER, 84);
    if (pressedKeyCount > 5)
      InputNumber = 0,
      pressedKeyCount = 0,
      tooManyNumbers();
  }
}

void SaveNewTime()
{
  if (CurrentMode == SetOffTimeMode)
  {
    offTime = InputNumber;
    offTimeSaved();
  }
  if (CurrentMode == SetOnTimeMode)
  {
    onTime = InputNumber;
    onTimeSaved();
  }
}

void keypadEvent (KeypadEvent key)
{
  if (keypad.getState() == PRESSED)
  {
    switch (key)
    {
      case 'A':
        CurrentMode = TitleMode;
        title();
        InputNumber = 0;
        break;
      case 'B':
        CurrentMode = SetOffTimeMode;
        offTimeChange();
        InputNumber = 0;
        break;
      case 'C':
        CurrentMode = SetOnTimeMode;
        onTimeChange();
        InputNumber = 0;
        break;
      case 'D':
        examplesPage();
        break;
      case '0' ... '9':
        IncreaseNewTime(key);
        break;
      case '*':
        if (InputNumber > 0)
        {
          InputNumber = 0;
          reset();
        }
        break;
      case '#':
        if (InputNumber > 0)
        {
          SaveNewTime();
          InputNumber = 0;
        }
        break;
    }
  }
}

I hope someone can give me some advise.

Thanks a lot

Rather than using floats, I would continue to use integers, but record and store the times as hundredths of seconds. For example, 10.25s would be stored as 1025. When displaying these times, you can use / and % to split the whole and fractional parts and print the "." between them. Elsewhere in the code, where I assume the times need to be converted to milliseconds for use with delay() or compared to millis(), you only need to multiply by 10 rather than 1000.

PaulRB:
Rather than using floats, I would continue to use integers, but record and store the times as hundredths of seconds. For example, 10.25s would be stored as 1025. When displaying these times, you can use / and % to split the whole and fractional parts and print the "." between them. Elsewhere in the code, where I assume the times need to be converted to milliseconds for use with delay() or compared to millis(), you only need to multiply by 10 rather than 1000.

Hi Paul,
Thank you for the reply. I've attached the complete code in .zip file for reference. I have various menus in my code, and also points where the user enters the time delays, and gets to see it on the screen at the same time. I use all the buttons on a 4x4 keypad, so at this stage I cant use one of them for a decimal '.'
Unfortunately I am a novice when it comes to coding, and it took me about 2 months to get the code as far as it is now (With help from some very nice guys on the forum). So I'm going to need some help to understand your thinking of this.
Appreciate your help.

Complete10RelayMechKeypad.zip (4.25 KB)

    if (pressedKeyCount > 5)
      InputNumber = 0,
      pressedKeyCount = 0,
      tooManyNumbers();
  }
}

Here, it seems to me like your indentation suggests something that the compiler will not see.
Is your "if" missing a pair of these {} ?

What would I have to change this to

if (millis() - BlowerStartTime > offTime * 1000UL)

if I want the entry to be in milliseconds and not seconds? That way I can forget about decimal points and just make the user enter the timeout in milliseconds.

I've attached the complete code in .zip file for reference.

Not opening that. Please read the forum guide.

I use all the buttons on a 4x4 keypad, so at this stage I cant use one of them for a decimal '.'

I wasn't suggesting that, only that when displaying the value you can print a ".".

Unfortunately I am a novice when it comes to coding, and it took me about 2 months to get the code as far as it is now (With help from some very nice guys on the forum).

So you took the fish given to you for 2 months instead of taking the opportunity to learn to fish?

if I want the entry to be in milliseconds and not seconds?

What is "the entry"? Is it "offTime"? Are you saying that code works today, if offTime in seconds? Seems to me offTime would need to be in microseconds.

EDIT: I see you changed that while I was posting my reply. Before, it said "/ 1000UL" not "* 1000 UL".

Hi Paul,

Unfortunately I did not study programming after school, and I'm trying to run a business at the same time, so getting time to study this completely is very hard. So I asked for help from people on the forum, and they helped me. I did about 95% of the coding while they were helping me. I did not mislead anyone into doing the entire code for me, and I cant afford to employ a programmer on my staff, so this was the only route I had.
My biggest problem is I did the code 3 years ago, and that was the last I looked at it, so had to refresh my memory quickly.

Sorry if I upset you by asking for help.

I to change the "millis" entry to 1UL and will now just enter the unit in milliseconds on the keypad, this will have to do.

Thanks again for the help.

No, I apologise for being unfriendly. This forum has a reputation to maintain, if we don't want the place to be overrun with freeloaders who think this is a free code writing/hardware debugging service. We enjoy teaching others to fish, to re-use my favourite analogy. Handing out free fish is boring.

While many senior forum members are retired, many of us do work for a living, and help out on the forum during breaks and after hours.

PaulRB:
No, I apologise for being unfriendly. This forum has a reputation to maintain, if we don't want the place to be overrun with freeloaders who think this is a free code writing/hardware debugging service. We enjoy teaching others to fish, to re-use my favourite analogy. Handing out free fish is boring.

While many senior forum members are retired, many of us do work for a living, and help out on the forum during breaks and after hours.

No problem Paul, and thanks for the help with my problem.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.