单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:5894|回复:4

Arduino计算器程序

[复制链接]
ID:469959发表于 2019-1-18 08:47|显示全部楼层

vch

vch

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

  3. //d7--d4,12 11 10 9 e-8 rs-7 1234--A2--A5 5678--2354
  4. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

  5. // 2. Keypad Pins
  6. const byte Rows = 4;
  7. const byte Cols = 4;
  8. char keys[Rows][Cols] =
  9. {
  10.   {'1', '2', '3', '+'},
  11.   {'4', '5', '6', '-'},
  12.   {'7', '8', '9', '*'},
  13.   {'C', '0', '=', '/'}
  14. };
  15. byte rowPins[Rows] = {A2, A3, A4, A5};
  16. byte colPins[Cols] = {2, 3, 4, 5};
  17. Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, Rows, Cols);

  18. // 3. Dot Button
  19. int dot = A0;
  20. int dotFlag = 0;
  21. int dotButton = 0;

  22. // 4. Calculator Operators
  23. float num1, num2, fraction;
  24. float total;
  25. char operation, button;

  26. // 5. Loading Setup
  27. char input[16];
  28. int n = 1750;

  29. void setup()
  30. {
  31.   // Initialize dot button as input to Arduino
  32.   pinMode(dot, INPUT);

  33.   // Initialize LCD Size
  34.   lcd.begin(16, 2);

  35.   // LCD Loading Setup Begin
  36.   lcd.clear();
  37.   lcd.setCursor(3, 0);
  38.   lcd.print("LOADING...");
  39.   for (int i = 0; i< 16; i++)
  40.   {
  41.        lcd.setCursor(i, 1);
  42.        lcd.write(255);
  43.        delay(50);
  44.   }
  45.   lcd.clear();
  46.   lcd.setCursor(1, 0);
  47.   lcd.print("Simple Arduino");
  48.   lcd.setCursor(3, 1);
  49.   lcd.print("Calculator");
  50.   delay(n);
  51.   lcd.clear();
  52.   lcd.setCursor(0, 0);
  53.   lcd.print("Done By Students");
  54.   lcd.setCursor(2, 1);
  55.   lcd.print("Meznan Bahri");
  56.   delay(n);
  57.   lcd.clear();
  58.   lcd.setCursor(1, 0);
  59.   lcd.print("Dareen Zamzami");
  60.   lcd.setCursor(2, 1);
  61.   lcd.print("Joud Mannaa");
  62.   delay(n);
  63.   lcd.clear();
  64.   lcd.setCursor(1, 0);
  65.   lcd.print("Samira Laihabi");
  66.   lcd.setCursor(2, 1);
  67.   lcd.print("& Rana Johar");
  68.   delay(n);
  69.   lcd.clear();
  70.   // LCD Loading Setup End
  71. }
  72. void loop()
  73. {
  74.   // First while loop for num1.
  75.   while (1)
  76.   {
  77.        dotButton = digitalRead(dot);
  78.        button = customKeypad.getKey();
  79.        if (button == 'C')
  80.        {
  81.        dotFlag = 0;
  82.        num1 = 0;
  83.        num2 = 0;
  84.        fraction = 0;
  85.        total = 0;
  86.        operation = 0;
  87.        lcd.clear();
  88.        }
  89.        else if (dotButton == LOW)
  90.        {
  91.        dotFlag = 1;
  92.        }
  93.        else if (button >= '0'&& button< = '9')
  94.        {
  95.        if (dotFlag == 0)
  96.        {
  97.          num1 = num1 * 10 + (button - '0');
  98.          lcd.setCursor(0, 0);
  99.          lcd.print(num1);
  100.        }
  101.        else if (dotFlag == 1)
  102.        {
  103.          fraction = (button - '0');
  104.          num1 = num1 + (fraction / 10);
  105.          lcd.setCursor(0, 0);
  106.          lcd.print(num1);
  107.          dotFlag++;
  108.        }
  109.        else if (dotFlag == 2)
  110.        {
  111.          fraction = (button - '0');
  112.          num1 = num1 + (fraction / 100);
  113.          lcd.setCursor(0, 0);
  114.          lcd.print(num1);
  115.          dotFlag++;
  116.        }
  117.        }
  118.        else if (button == '-' || button == '+' || button == '*' || button == '/')
  119.        {
  120.        operation = button;
  121.        dotFlag = 0;
  122.        lcd.setCursor(0, 1);
  123.      lcd.print(operation);
  124.        break;
  125.        }
  126.   }
  127.   // Second while loop for num2.
  128.   while (1)
  129.   {
  130.        dotButton = digitalRead(dot);
  131.        button = customKeypad.getKey();
  132.        if (button == 'C')
  133.        {
  134.        dotFlag = 0;
  135.        num1 = 0;
  136.        num2 = 0;
  137.        fraction = 0;
  138.        total = 0;
  139.        operation = 0;
  140.        lcd.clear();
  141.        break;
  142.        }
  143.        else if (dotButton == LOW)
  144.        {
  145.        dotFlag = 1;
  146.        }
  147.        else if (button >= '0'&& button< = '9')
  148.        {
  149.        if (dotFlag == 0)
  150.        {
  151.          num2 = num2 * 10 + (button - '0');
  152.          lcd.setCursor(1, 1);
  153.          lcd.print(num2);
  154.        }
  155.        else if (dotFlag == 1)
  156.        {
  157.          fraction = (button - '0');
  158.          num2 = num2 + (fraction / 10);
  159.          lcd.setCursor(1, 1);
  160.          lcd.print(num2);
  161.          dotFlag++;
  162.        }
  163.        else if (dotFlag == 2)
  164.        {
  165.          fraction = (button - '0');
  166.          num2 = num2 + (fraction / 100);
  167.          lcd.setCursor(1, 1);
  168.          lcd.print(num2);
  169.          dotFlag++;
  170.        }
  171.        }
  172.        if (button == '=')
  173.        {
  174.        domath();
  175.        break;
  176.        }
  177.   }
  178.   // Third while loop for ensuring C button is executed after while loop 2.
  179.   while (1)
  180.   {
  181.        button = customKeypad.getKey();
  182.        if (button == 'C')
  183.        {
  184.        dotFlag = 0;
  185.        num1 = 0;
  186.        num2 = 0;
  187.        fraction = 0;
  188.        total = 0;
  189.        operation = 0;
  190.        lcd.clear();
  191.        break;
  192.        }
  193.   }
  194. }
  195. void domath()
  196. {
  197.   switch (operation)
  198.   {
  199.        case '+':
  200.        total = num1 + num2;
  201.        break;
  202.        case '-':
  203.        total = num1 - num2;
  204.        break;
  205.        case '/':
  206.        total = num1 / num2;
  207.        break;
  208.        case '*':
  209.        total = num1 * num2;
  210.        break;
  211.   }
  212.   lcd.print('=');
  213.   if (operation == '/'&& num2 == 0)
  214.   {
  215.        lcd.print("ERROR 0 DIV");
  216.   }
  217.   else
  218.   {
  219.        lcd.print(total);
  220.   }
  221. }

复制代码
ID:1发表于 2019-1-19 02:00|显示全部楼层
补全原理图或者详细说明一下电路连接即可获得100+黑币
ID:666465发表于 2019-12-21 00:55来自手机|显示全部楼层
你好,我想问一下用Proteus仿真arduino328开发版实现6计算器功能的源码有吗,或者是你会做吗?
ID:1021009发表于 2022-4-23 18:11|显示全部楼层
作者使用的keypad库是下载的哪一个,我用的是proteus 8仿真工具
ID:138707发表于 2023-7-29 18:25|显示全部楼层
作者使用的keypad库是下载的哪一个

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网