#include <Wire.h>
#include <Keypad.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
const byte ROWS = 4;
const byte COLS = 4;
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
const int led_pin1 = 10;
const int led_pin2 = 11;
const int led_pin3 = 12;
const int led_pin4 = 13;
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// declare an SSD1306 display object connected to I2C
int first_code1=6;
int first_code2=0;
int first_code3=2;
int first_code4=1;
int delpassor1 = 0;
int delpassor2 = 0;
int delpassor3 = 0;
int delpassor4 = 0;
int passord = 0;
int posisjon = 0;
int tallting =1;
void setup() {
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true)
;
}
delay(2000); // wait for initializing
Serial.begin(9600);
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
oled.println("Skriv inn");
oled.println("Koden."); // text to display
oled.display();
pinMode(led_pin1, OUTPUT);
pinMode(led_pin2, OUTPUT);
pinMode(led_pin3, OUTPUT);
pinMode(led_pin4, OUTPUT);
}
void loop() {
char customKey = customKeypad.getKey(); // LESER AV NUMMER FRA KEYPAD OG LAGRER DET I "customkey"
if (posisjon == 0){
if (customKey - 48 == first_code1){
oled.print(first_code1);
oled.display();
Serial.println(first_code1);
delpassor1 = customKey - 48;
posisjon = posisjon + 1;
}
else{
Serial.println("Venter på kode 1");
}
}
if (posisjon == 1){
if (customKey - 48 == first_code2){
oled.print(first_code2);
oled.display();
Serial.print(first_code2);
delpassor2 = customKey - 48;
posisjon = posisjon + 1;
}
else{
Serial.println("Venter på kode 2");
}
}
if (posisjon == 2){
if (customKey - 48 == first_code3){
oled.print(first_code3);
oled.display();
Serial.println(first_code3);
delpassor3 = customKey - 48;
posisjon = posisjon + 1;
}
else{
Serial.println("Venter på kode 3");
}
}
if (posisjon == 3){
if (customKey - 48 == first_code4){
oled.print(first_code4);
oled.display();
Serial.print(first_code4);
delpassor4 = customKey - 48;
posisjon = posisjon + 1;
}
else{
Serial.println("Venter på kode 4");
}
}
passord = (delpassor1*1000)+(delpassor2*100)+(delpassor3*10)+(delpassor4);
Serial.println(passord);
if (passord == 6021){
delay(1000);
oled.clearDisplay();
oled.display();
}
while (passord == 6021){
Serial.println(passord);
oled.setTextSize(2); // text size
oled.setCursor(0, 0); // position to display
oled.println("Skriv inn");
oled.println("siffere i"); // text to display
oled.println("sekvensen");
oled.display();
char customKey = customKeypad.getKey();
if (tallting == 1){
if (customKey - 48 == 1){
oled.println(1);
oled.display();
Serial.println(1);
tallting++;
Serial.println(tallting);
digitalWrite(10, HIGH);
delay(500);
Serial.println(tallting);
customKey=48;
}
}
if (tallting ==2){
if (customKey - 48 == 1){
oled.println(" 1");
oled.display();
Serial.println(1);
digitalWrite(11, HIGH);
tallting++;
delay(500);
}
}
if (tallting ==3){
if (customKey - 48 == 2){
oled.println(" 2");
oled.display();
Serial.println(2);
tallting++;
digitalWrite(12, HIGH);
delay(500);
}
}
if (tallting == 4){
if (customKey - 48 == 3){
oled.print(" 3Riktig");
oled.display();
Serial.println(3);
tallting++;
digitalWrite(13, HIGH);
delay(500);
}
}
}
}