Advertisement
Guest User

Untitled

a guest
May 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. #include <Keypad.h>
  2.  
  3. const byte rows= 4;     // Number of rows on the keypad
  4. const byte columns= 3;  // Number of columns on the keypad
  5.  
  6. // The 2-dimensional array keymap uses the same layout as the keypad
  7. char keymap[rows][columns]=
  8.   {
  9.     {'1', '2', '3'},
  10.     {'4', '5', '6'},
  11.     {'7', '8', '9'},
  12.     {'*', '0', '#'}
  13.   };
  14.  
  15. // These arrays map rows and columns to the Arduino pins
  16. byte rowPins[rows]       = {13,12,11,10}; //Rows 0 to 3
  17. byte columnPins[columns] = {9,8,7}; //Columns 0 to 2
  18.  
  19. // Initialises an instance of the Keypad class
  20. Keypad keypad = Keypad(makeKeymap(keymap), rowPins, columnPins, rows, columns);
  21.  
  22. // initialise code as 'xx'
  23. String code = "xx\0";
  24.  
  25. // arrays to store product info
  26. String products[] = {"Twix", "Mars Bar", "Squares", "Bueno", "Quavers", "Wotsits", "Mini Cheddars", "Freddo", "KitKat"};
  27. int prodCode[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  28. float prodCost[] = {0.80, 1, 0.50, 0.50, 0.60, 0.40, 0.35, 0.20, 0.60};
  29. const int numberOfProds = 9;
  30.  
  31.  
  32.  
  33. int checkPosition(String codeStr)
  34. {
  35.   // if first or 2nd position are x, they are empty so can have a value inserted
  36.   if (codeStr[0] == 'x')
  37.     return 0;  
  38.   else if (codeStr[1] == 'x')
  39.     return 1;  
  40.   else
  41.   // otherwise return 666 to signify full code selection
  42.     return 666;
  43. }
  44.  
  45.  
  46. // get name of item from given code; return error message if invalid
  47. String getItem(String codeStr)
  48. {
  49.   int codeInt = codeStr.toInt();
  50.   for (int i = 0; i <= (numberOfProds - 1); i++)
  51.   {
  52.     if (codeInt == prodCode[i])
  53.     {
  54.       return products[i];
  55.     }
  56.   }
  57.   return "Invalid item code.";
  58. }
  59.  
  60.  
  61.  
  62. void setup()
  63. {
  64.   // put your setup code here, to run once:
  65.   Serial.begin(9600);
  66. }
  67.  
  68.  
  69. void loop()
  70. {
  71.   // Set key equal to the key pressed
  72.   char key = keypad.getKey();
  73.  
  74.   // If a key was pressed (ie not NO_KEY)
  75.   if (key != NO_KEY)
  76.   {
  77.     Serial.println(key);
  78.     int position = 666;
  79.    
  80.     switch (key)
  81.     {
  82.       case '1':
  83.         position = checkPosition(code);
  84.         if (position == 666)
  85.         {
  86.           // print invalid choice to LCD
  87.           Serial.println("Invalid choice.");
  88.         }
  89.         else
  90.         {
  91.           code[position] = '1';
  92.         }
  93.       break;
  94.       case '2':
  95.         position = checkPosition(code);
  96.         if (position == 666)
  97.         {
  98.           // print invalid choice to LCD
  99.           Serial.println("Invalid choice.");
  100.         }
  101.         else
  102.         {
  103.           code[position] = '2';
  104.         }
  105.       break;
  106.       case '3':
  107.         position = checkPosition(code);
  108.         if (position == 666)
  109.         {
  110.           // print invalid choice to LCD
  111.           Serial.println("Invalid choice.");
  112.         }
  113.         else
  114.         {
  115.           code[position] = '3';
  116.         }
  117.       break;
  118.       case '4':
  119.         position = checkPosition(code);
  120.         if (position == 666)
  121.         {
  122.           // print invalid choice to LCD
  123.           Serial.println("Invalid choice.");
  124.         }
  125.         else
  126.         {
  127.           code[position] = '4';
  128.         }
  129.       break;
  130.       case '5':
  131.         position = checkPosition(code);
  132.         if (position == 666)
  133.         {
  134.           // print invalid choice to LCD
  135.           Serial.println("Invalid choice.");
  136.         }
  137.         else
  138.         {
  139.           code[position] = '5';
  140.         }
  141.       break;
  142.       case '6':
  143.         position = checkPosition(code);
  144.         if (position == 666)
  145.         {
  146.           // print invalid choice to LCD
  147.           Serial.println("Invalid choice.");
  148.         }
  149.         else
  150.         {
  151.           code[position] = '6';
  152.         }
  153.       break;
  154.       case '7':
  155.         position = checkPosition(code);
  156.         if (position == 666)
  157.         {
  158.           // print invalid choice to LCD
  159.           Serial.println("Invalid choice.");
  160.         }
  161.         else
  162.         {
  163.           code[position] = '7';
  164.         }
  165.       break;
  166.       case '8':
  167.         position = checkPosition(code);
  168.         if (position == 666)
  169.         {
  170.           // print invalid choice to LCD
  171.           Serial.println("Invalid choice.");
  172.         }
  173.         else
  174.         {
  175.           code[position] = '8';
  176.         }
  177.       break;
  178.       case '9':
  179.         position = checkPosition(code);
  180.         if (position == 666)
  181.         {
  182.           // print invalid choice to LCD
  183.           Serial.println("Invalid choice.");
  184.         }
  185.         else
  186.         {
  187.           code[position] = '9';
  188.         }
  189.       break;
  190.       case '0':
  191.         position = checkPosition(code);
  192.         if (position == 666)
  193.         {
  194.           // print invalid choice to LCD
  195.           Serial.println("Invalid choice.");
  196.         }
  197.         else
  198.         {
  199.           code[position] = '0';
  200.         }
  201.       break;
  202.       case '*':
  203.         //reset array to xx
  204.         code = "xx\0";
  205.       break;
  206.       case '#':
  207.         Serial.println("Code is:" + code);
  208.         String itemName = getItem(code);
  209.         Serial.println(itemName);
  210.       break;
  211.     }
  212.   }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement