Go Down

Topic: Why does the countdown increase? (Read 92 times) previous topic - next topic

0Avendano

Thank you for reading. My problem is in the end, the program asks for a number first, then that number is saved in the variable "t" and so that it reappears but in countdown this number appears but bigger why does this happen? for example, I put 26 and the countdown starts at 256. Thanks for reading, I use a translator since I do not speak English.

#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
int t=0;
int segundostotal = 100; 
int a=4;
long first = 0;
long second = 0;
double total = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','=','/'}
};
byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{
lcd.begin(16, 2);               // start lcd

}


void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Ingrese tiempo");
  lcd.setCursor(0,1);
  lcd.print("en sg:");
 
 
  customKey = customKeypad.getKey();
 
  if(customKey !=NO_KEY){
   
    lcd.leftToRight();
    t = (10 * t) + customKey - '0';
    lcd.setCursor(7,1);
    lcd.print(t);
 
  }
     

  if(customKey =='-')
  {
    lcd.clear();
    while (t > 0)

      {
    lcd.setCursor(0,0);
    lcd.print("Movimiento en:");
    lcd.setCursor(0,1);

        delay (1000);          //Descontamos en periodos de 1 segundo

        t--; 
    lcd.print(t);
   
   
  }
  }
 
}

david_2018

You enter '2' '6' '-'

t is calculated as:

t = (10 * 0) + '2' - '0'    // t = 2

t = (10 * 2) + '6' - '0'    // t = 26

t = (10 * 26) + '-' - '0'    // t = 257

Your code does not test for customkey being a number

Code: [Select]
if (customKey != NO_KEY) {  
  lcd.leftToRight();
  t = (10 * t) + customKey - '0';
  lcd.setCursor(7, 1);
  lcd.print(t);
}

Go Up