Mål: Att kunna avläsa vilken knapp som blivit tryckt på knappsatsen och skriva det i serial monitorn.
Teori: Knappsatsen är egentligen en mängd strömbrytare ordnade i rader och kolumner. Då en knapp trycks ner sluts en strömkrets och arduinon kan läsa av vilken knapp det är med hjälp av ett bibliotek som måste inkluderas i början av koden:
#include <Keypad.h>
Koppling: Knappsatsens kablar är numrerade 1-8 sett från vänster. Dessa kan kopplas till vilken som helst Arduino digital pin mellan 2-13. Beroende på vilka pin du använder ska du se till att få numreringen korrekt i koden.
Fritzing kopplingsschema
Arduino kod 1
/*4x4 Matrix Keypad connected to Arduino This code prints the key pressed on the keypad to the serial port*/ #include <Keypad.h> const byte numRows= 4; //number of rows on the keypad const byte numCols= 4; //number of columns on the keypad //keymap defines the key pressed according to the row and columns just as appears on the keypad char keymap[numRows][numCols]= { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; //Code that shows the the keypad connections to the arduino terminals byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3 byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3 //initializes an instance of the Keypad class Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); void setup() { Serial.begin(9600); } void loop() { char keypressed = myKeypad.getKey(); if (keypressed != NO_KEY) { Serial.print(keypressed); } }
Arduino kod 2
mål: att kunna sätta på t.ex. en lysdiod om man slagit in rätt lösenord
/* code by Maker based on http://playground.arduino.cc/Code/Keypad http://musicdiver.com/wordpress/2013/01/keypad-code-lock-with-arduino/ */ #include <Keypad.h> char secretCode[] = {"123456"}; // set password int place = 0; const byte rows = 4; const byte cols = 3; char keys[rows][cols] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte rowPins[rows] = {7, 2, 3, 5}; // row 1,2,3,4 check schematic byte colPins[cols] = {6, 8, 4}; // col 1,2,3 check schematic Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); int redPin = 10; int greenPin = 11; int solenoidPin = 12; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(solenoidPin, OUTPUT); setLocked(true); Serial.begin(9600); // serial comm. only for debugging purposes digitalWrite(solenoidPin, LOW); } void loop() { char key = keypad.getKey(); if (key == '*' || key == '#') { place = 0; Serial.println("*#"); setLocked(true); Serial.println(place); } else if(place == 6) { setLocked(false); Serial.println(place); Serial.println("open"); } else if(key == '\0') { Serial.println("Null character"); } else if (key != secretCode[place]) { place = 0; Serial.println("invalid pass"); Serial.println(place); } else if (key == secretCode[place]) { place++; Serial.println(place); Serial.println("correct pass"); } delay(50); } void setLocked(int locked) { if (locked) { digitalWrite(redPin, HIGH); digitalWrite(greenPin, LOW); delay(80); digitalWrite(redPin, LOW); delay(80); digitalWrite(redPin, HIGH); delay(80); digitalWrite(redPin, LOW); delay(80); digitalWrite(redPin, HIGH); digitalWrite(solenoidPin, LOW); } else { digitalWrite(redPin, LOW); digitalWrite(greenPin, HIGH); digitalWrite(solenoidPin, HIGH); } }
Keypad password servo kod
/* code by Maker based on http://playground.arduino.cc/Code/Keypad http://musicdiver.com/wordpress/2013/01/keypad-code-lock-with-arduino/ */ #include <Keypad.h> // include keypad library by Stanley & Brevig #include <Servo.h> // include servo library char secretCode[] = {"2018A"}; // set password int antal = sizeof(secretCode) - 1; // count password characters int place = 0; // keep track of element in password array const byte rows = 4; const byte cols = 4; char keys[rows][cols]= { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte rowPins[rows] = {9,8,7,6}; // Rows 0 to 3 byte colPins[cols]= {5,4,3,2}; // Columns 0 to 3 Servo myservo; Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); void setup() { myservo.attach(11); // attach servo signal to pin 11 Serial.begin(9600); // serial comm. only for debugging purposes myservo.write(0); // start servo at default position Serial.println("CanDy MachINE ................ 2018"); Serial.println(""); } void loop() { char key = keypad.getKey(); if (key == '*' || key == '#') { place = 0; Serial.print(place); Serial.println(" *#"); } else if(place == antal) { Serial.println("----- GIVE CANDY -----"); myservo.write(90); delay(2000); myservo.write(0); place = 0; } else if(key == '\0') { //Serial.println("Null character"); } else if (key != secretCode[place]) { place = 0; Serial.print(place); Serial.println(" invalid pass"); } else if (key == secretCode[place]) { place++; Serial.print(place); Serial.println(" correct pass"); } delay(50); }
——-