Devices & Components
Arduino Uno Rev3
16x2 LCD display with I²C interface
10 jumper wires 150mm male
Pack of 50 female-female jumper wires in various colors
Breadboard 100x160
Step Motor 5V DC
Software & Tools
Arduino IDE
Project description
Code
Lock System
cpp
Lock System with Arduino Uno
1#include <LiquidCrystal.h> 2 3// LCD: RS, E, D4, D5, D6, D7 4LiquidCrystal lcd(12, 7, 5, 4, 3, 2); // E'yi D7’ye bağladığını varsayıyorum 5 6// Motor pinleri (senin bağlantına göre) 7int in1 = 8; 8int in2 = 9; 9int in3 = 10; 10int in4 = 11; 11 12// Tek butonla kontrol 13const int buttonPin = 6; 14int buttonCount = 0; 15bool lastButtonState = HIGH; 16bool isUnlocked = false; 17 18void setup() { 19 delay(500); // LCD’nin hazır olmasını bekle 20 21 // LCD başlat 22 lcd.begin(16, 2); 23 lcd.setCursor(0, 0); 24 lcd.print("AAA"); 25 26 // Motor pinleri 27 pinMode(in1, OUTPUT); 28 pinMode(in2, OUTPUT); 29 pinMode(in3, OUTPUT); 30 pinMode(in4, OUTPUT); 31 32 // Buton pini 33 pinMode(buttonPin, INPUT_PULLUP); 34 35 Serial.begin(9600); 36 37 // Başlangıç bobin enerjisi 38 stepMotor(1); 39} 40 41 42void loop() { 43 bool currentState = digitalRead(buttonPin); 44 45 if (lastButtonState == HIGH && currentState == LOW) { 46 buttonCount++; 47 Serial.print("Butona basildi: "); 48 Serial.println(buttonCount); 49 50 lcd.clear(); 51 lcd.setCursor(0, 0); 52 lcd.print("Basilma: "); 53 lcd.print(buttonCount); 54 lcd.print("/3"); 55 56 delay(200); 57 } 58 59 lastButtonState = currentState; 60 61 if (buttonCount >= 3) { 62 buttonCount = 0; 63 64 lcd.clear(); 65 lcd.setCursor(0, 0); 66 67 if (!isUnlocked) { 68 Serial.println("Motor ileri donuyor"); 69 lcd.print("Kilit Aciliyor..."); 70 rotateMotor(90); 71 isUnlocked = true; 72 } else { 73 Serial.println("Motor geri donuyor"); 74 lcd.print("Kilit Kapaniyor..."); 75 rotateMotor(-90); 76 isUnlocked = false; 77 } 78 79 delay(1000); 80 lcd.clear(); 81 lcd.setCursor(0, 0); 82 lcd.print("Sistem Hazir"); 83 } 84} 85 86// Motoru derece kadar döndür 87void rotateMotor(int degree) { 88 int steps = map(abs(degree), 0, 360, 0, 2048); 89 int dir = degree > 0 ? 1 : -1; 90 91 for (int i = 0; i < steps; i++) { 92 stepMotor(dir); 93 delay(2); 94 } 95} 96 97// Motor adım kontrolü 98void stepMotor(int dir) { 99 static int stepIndex = 0; 100 int sequence[4][4] = { 101 {1, 0, 0, 1}, 102 {1, 1, 0, 0}, 103 {0, 1, 1, 0}, 104 {0, 0, 1, 1} 105 }; 106 107 stepIndex += dir; 108 if (stepIndex > 3) stepIndex = 0; 109 if (stepIndex < 0) stepIndex = 3; 110 111 digitalWrite(in1, sequence[stepIndex][0]); 112 digitalWrite(in2, sequence[stepIndex][1]); 113 digitalWrite(in3, sequence[stepIndex][2]); 114 digitalWrite(in4, sequence[stepIndex][3]); 115}
Downloadable files
Lock System
Lock System
sketch_may18a.ino
Documentation
Lock
Lock
sketch_may18a.ino
Comments
Only logged in users can leave comments