Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- const byte rows= 4; // Number of rows on the keypad
- const byte columns= 3; // Number of columns on the keypad
- // The 2-dimensional array keymap uses the same layout as the keypad
- char keymap[rows][columns]=
- {
- {'1', '2', '3'},
- {'4', '5', '6'},
- {'7', '8', '9'},
- {'*', '0', '#'}
- };
- // These arrays map rows and columns to the Arduino pins
- byte rowPins[rows] = {13,12,11,10}; //Rows 0 to 3
- byte columnPins[columns] = {9,8,7}; //Columns 0 to 2
- // Initialises an instance of the Keypad class
- Keypad keypad = Keypad(makeKeymap(keymap), rowPins, columnPins, rows, columns);
- // initialise code as 'xx'
- String code = "xx\0";
- // arrays to store product info
- String products[] = {"Twix", "Mars Bar", "Squares", "Bueno", "Quavers", "Wotsits", "Mini Cheddars", "Freddo", "KitKat"};
- int prodCode[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
- float prodCost[] = {0.80, 1, 0.50, 0.50, 0.60, 0.40, 0.35, 0.20, 0.60};
- const int numberOfProds = 9;
- int checkPosition(String codeStr)
- {
- // if first or 2nd position are x, they are empty so can have a value inserted
- if (codeStr[0] == 'x')
- return 0;
- else if (codeStr[1] == 'x')
- return 1;
- else
- // otherwise return 666 to signify full code selection
- return 666;
- }
- // get name of item from given code; return error message if invalid
- String getItem(String codeStr)
- {
- int codeInt = codeStr.toInt();
- for (int i = 0; i <= (numberOfProds - 1); i++)
- {
- if (codeInt == prodCode[i])
- {
- return products[i];
- }
- }
- return "Invalid item code.";
- }
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(9600);
- }
- void loop()
- {
- // Set key equal to the key pressed
- char key = keypad.getKey();
- // If a key was pressed (ie not NO_KEY)
- if (key != NO_KEY)
- {
- Serial.println(key);
- int position = 666;
- switch (key)
- {
- case '1':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '1';
- }
- break;
- case '2':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '2';
- }
- break;
- case '3':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '3';
- }
- break;
- case '4':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '4';
- }
- break;
- case '5':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '5';
- }
- break;
- case '6':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '6';
- }
- break;
- case '7':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '7';
- }
- break;
- case '8':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '8';
- }
- break;
- case '9':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '9';
- }
- break;
- case '0':
- position = checkPosition(code);
- if (position == 666)
- {
- // print invalid choice to LCD
- Serial.println("Invalid choice.");
- }
- else
- {
- code[position] = '0';
- }
- break;
- case '*':
- //reset array to xx
- code = "xx\0";
- break;
- case '#':
- Serial.println("Code is:" + code);
- String itemName = getItem(code);
- Serial.println(itemName);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement