Hello! I've just got arduino, and I need help with code. I'm relatively noob in programming Arduino and this is pretty simple project, so please don't laugh.
I took my old computer case and i thought making a cabinet with arduino controlled lock. I'm in end phase of my project, just need some coding advise. I have three sensors. One is for sensing if cabinet door is closed, second is for sensing if the lock is locked and third is for sensing if the lock is opened. Arduinos job is to lock the cabinet door if trigger is pressed (ie pushbutton), or unlock if it's locked. But only when the cabinets door is closed. [See attached picture.]
Here's the code i'd worked out so far:
/*
/* Nevermind the following text, it's in estonian.
Elektrooniliselt juhitav seinakapp
Kapi põhikomponendiks on Arduino. Ukse avab Mootor1, mis on ühendatud relee abil arduino Digital Pin 5'ga (PWM). Lukku juhib Mootor2,
mis on ühendatud otse Digital Pin 3'ga (PWM). Sensoriteks on neli lülitit, mis on ühendatud vastavalt Pin'idesse D4, D7, D8, D9.
Triggeriks on Reed switch, mis on ühendatud D2'te. Vaja on veel ühendada hunnik LED'e, ja vajadusel veel midagi.
Koostatud 2012
by Kristjan Tozen
http://www.arduino.cc/
*/
// Sätime Pin numbrid konstantidena
const int OpenPin = 7; // Reversed pushbutton scheme from Arduino basic tutorials
const int LockPin = 8; // Pushbutton scheme from Arduino basic tutorials
const int UnlockPin = 9; // Pushbutton scheme from Arduino basic tutorials
const int TriggerPin = 2; // Pushbutton scheme from Arduino basic tutorials
const int MotorPin1 = 3; // Lock motor PIN1
const int MotorPin2 = 5; // Lock motor PIN2
// Variables:
int OpenState = 0;
int LockState = 0;
int UnlockState = 0;
int TriggerState = 0;
void setup() {
// Pinmodes
pinMode(OpenPin, INPUT);
pinMode(LockPin, INPUT);
pinMode(UnlockPin, INPUT);
pinMode(TriggerPin, INPUT);
pinMode(MotorPin2, OUTPUT);
pinMode(MotorPin1, OUTPUT);
Serial.begin(9600); //for debugging
}
void loop(){
// Reads sensors
OpenState = digitalRead(OpenPin);
LockState = digitalRead(LockPin);
UnlockState = digitalRead(UnlockPin);
TriggerState = digitalRead(TriggerPin);
Serial.println("OpenState:");
Serial.println(OpenState);
Serial.println("LockState:");
Serial.println(LockState);
Serial.println("UnlockState:");
Serial.println(UnlockState);
Serial.println("TriggerState:");
Serial.println(TriggerState);
if (LockState == HIGH && UnlockState == LOW && TriggerState == HIGH && OpenState == LOW) { //note that cabinet open door sensor is reversed pushbutton, so instead of closing switch to 5 volts, it
// closes to GND.
// Unlocks the lock, if the cabinet door is closed, lock is locked and trigger is pushed.
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, HIGH);
}
if (LockState == LOW && UnlockState == HIGH && TriggerState == HIGH && OpenState == LOW) {
// Locks the lock, if the cabinet door is closed, lock is unlocked and trigger is pushed
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin2, LOW);
}
delay(10);
}
}
Problem is that motor still keep running even if lock is unlocked and vice-versa. I want the lock motor to keep running UNTIL lock is unlocked or locked.
Please don't mind my language, it isn't my native one.