Outils pour utilisateurs

Outils du site


wiki:projets:code_pave_numerique_ecran_lcd_21.7_2016

Ceci est une ancienne révision du document !


<code=cpp>

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

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = {

{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'X','0','=','/'},

}; byte rowPins[ROWS] = {53,51,49,47}; connect to row pinouts byte colPins[COLS] = {45,43,41,39}; connect to column pinouts Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

variables declaration boolean valOnePresent = false; boolean next = false; boolean final = false; String num1, num2; int ans; char op; void setup(){ lcd.begin(16,2); lcd.setCursor(2,0); lcd.print(“Debit Volumique”); delay(6000); lcd.clear(); clears the LCD screen and positions the cursor in the upper-left corner. }

void loop(){

char key = myKeypad.getKey();
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
  if (valOnePresent != true){
    num1 = num1 + key;
    int numLength = num1.length();
    lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
    lcd.print(num1);
  }
  else {
    num2 = num2 + key;
    int numLength = num2.length();
    lcd.setCursor(15 - numLength, 1);
    lcd.print(num2);
    final = true;
  }
}
else if (valOnePresent == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')){
  if (valOnePresent == false){
    valOnePresent = true;
    op = key;
    lcd.setCursor(15,0); //operator on right corner
    lcd.print(op);
  }
}
else if (final == true && key != NO_KEY && key == '='){
  if (op == '+'){
    ans = num1.toInt() + num2.toInt();
  }
  else if (op == '-'){
    ans = num1.toInt() - num2.toInt();
  }
  else if (op == '*'){
    ans = num1.toInt() * num2.toInt();
  }
  else if (op == '/'){
    ans = num1.toInt() / num2.toInt();
  }    
    lcd.clear();
    lcd.setCursor(15,0);
    lcd.autoscroll();
    lcd.print(ans);
    lcd.noAutoscroll();
}
else if (key != NO_KEY && key == 'X'){
  lcd.clear();
  valOnePresent = false;
  final = false;
  num1 = "";
  num2 = "";
  ans = 0;
  op = ' ';
}

}

<\code>

wiki/projets/code_pave_numerique_ecran_lcd_21.7_2016.1459245031.txt.gz · Dernière modification: 2016/09/11 10:47 (modification externe)