아두이노 코드
#include <Servo.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
void checkEntered1(int button);
int code1[] = {3,5,3,5,2};
int code2[] = {2,3,4,4,1};
int entered[6];
int kpin = 13, xpin = A0, ypin = A1;
int second = 0;
int first = 0;
int Red = 8;
int Green = 9;
void setup() {
Serial.begin(9600);
servo1.attach(10);
servo1.write(180);
servo2.attach(11);
servo2.write(180);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(button5, INPUT_PULLUP);
pinMode(button6, INPUT_PULLUP);
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
Serial.println("Choose Your Box");
}
void loop() {
int swValue = digitalRead(kpin);
int xValue = analogRead(xpin);
int yValue = analogRead(ypin);
if(xValue >=1022){
second++;
Serial.println("Choose Second Box");
}
else if(xValue <= 5){
first++;
Serial.println("Choose First Box");
}
if (digitalRead(button1) == LOW){
checkEntered1(1);
delay(250);
}
else if (digitalRead(button2) == LOW){
checkEntered1(2);
delay(250);
}
else if (digitalRead(button3) == LOW){
checkEntered1(3);
delay(250);
}
else if (digitalRead(button4) == LOW){
checkEntered1(4);
delay(250); //wait
}
else if (digitalRead(button5) == LOW){
checkEntered1(5);
delay(250);
}
else if(digitalRead(button6) == LOW){
close_all();
}
}
void checkEntered1(int button){
if (entered[0] != 0){
checkEntered2(button);
}
else if(entered[0] == 0){
entered[0] = button;
Serial.print("1: ");Serial.println(entered[0]);
}
}
void checkEntered2(int button){
if (entered[1] != 0){
checkEntered3(button);
}
else if(entered[1] == 0){
entered[1] = button;
Serial.print("2: ");Serial.println(entered[1]);
}
}
void checkEntered3(int button){
if (entered[2] != 0){
checkEntered4(button);
}
else if (entered[2] == 0){
entered[2] = button;
Serial.print("3: ");Serial.println(entered[2]);
}
}
void checkEntered4(int button){
if (entered[3] != 0){
checkEntered5(button);
}
else if (entered[3] == 0){
entered[3] = button;
Serial.print("4: ");Serial.println(entered[3]);
}
}
void checkEntered5(int button){
if (entered[4] == 0){
entered[4] = button;
Serial.print("5: ");Serial.println(entered[4]);
delay(100);
compareCode();
}
}
void compareCode(){
for (int i = 0; i<5;i++){
Serial.println(entered[i]);
}
if ((second < first) &&(entered[0]==code1[0]) && (entered[1]==code1[1]) && (entered[2]==code1[2]) && (entered[3]==code1[3]) && (entered[4]==code1[4])&& (entered[5]==code1[5])){
Serial.println("First Box Open");
digitalWrite(Green, HIGH);
servo1.write(120);
for (int i = 0; i < 6; i++){
entered[i] = 0;
}
}
else if((second > first) &&(entered[0]==code2[0]) && (entered[1]==code2[1]) && (entered[2]==code2[2]) && (entered[3]==code2[3]) && (entered[4]==code2[4])&& (entered[5]==code2[5])){
Serial.println("Second Box Open");
digitalWrite(Red, HIGH);
servo2.write(120);
for (int i = 0; i < 6; i++){
entered[i] = 0;
}
}
else {
Serial.println("Close");
for (int i = 0; i < 7; i++){
entered[i] = 0;
}
}
}
void close_all(){
Serial.println("All Reset / Close");
servo1.write(180);
servo2.write(180);
first = 0;
second = 0;
digitalWrite(Red, LOW);
digitalWrite(Green, LOW);
}
파이썬 코드
from Arduino import Arduino
import time
servoPin1 = 10
servoPin2 = 11
kpin = 13
xpin = 0
ypin = 1
second = 0
first = 0
Red = 8
Green = 9
button1 = 2
button2 = 3
button3 = 4
button4 = 5
button5 = 6
button6 = 7
code1 = [1,1,1,1,1]
code2 = [3,3,3,3,3]
entered = [0,0,0,0,0]
board = Arduino('9600')
board.pinMode(kpin,"INPUT")
board.Servos.attach(servoPin1)
board.Servos.write(servoPin1, 180)
board.Servos.attach(servoPin2)
board.Servos.write(servoPin2, 180)
board.pinMode(button1, "INPUT")
board.pinMode(button2, "INPUT")
board.pinMode(button3, "INPUT")
board.pinMode(button4, "INPUT")
board.pinMode(button5, "INPUT")
board.pinMode(button6, "INPUT")
board.pinMode(Red, "OUTPUT")
board.pinMode(Green, "OUTPUT")
print("Choose Your Box")
def checkEntered1(button):
if entered[0] != 0:
checkEntered2(button)
elif entered[0] == 0:
entered[0] = button
print("1: ",entered[0])
def checkEntered2(button):
if entered[1] != 0:
checkEntered3(button)
elif entered[1] == 0:
entered[1] = button
print("2: ",entered[1])
def checkEntered3(button):
if entered[2] != 0:
checkEntered4(button)
elif entered[2] == 0:
entered[2] = button
print("3: ",entered[2])
def checkEntered4(button):
if entered[3] != 0:
checkEntered5(button)
elif entered[3] == 0:
entered[3] = button
print("4: ",entered[3])
def checkEntered5(button):
if entered[4] == 0:
entered[4] = button
print("5: ",entered[4])
time.sleep(0.5)
compareCode()
def close_all():
print("All Reset / Close")
board.Servos.write(servoPin1,180)
board.Servos.write(servoPin2,180)
first = 0
second = 0
board.digitalWrite(Red, "LOW")
board.digitalWrite(Green, "LOW")
def compareCode():
if entered[0]==code1[0] & entered[1]==code1[1] & entered[2]==code1[2] & entered[3]==code1[3] & entered[4]==code1[4]:
print("First Box Open")
board.digitalWrite(Green, "HIGH")
board.Servos.write(servoPin1,120)
for i in range(0,5):
entered[i] = 0
elif entered[0]==code2[0] & entered[1]==code2[1] & entered[2]==code2[2] & entered[3]==code2[3] & entered[4]==code2[4]:
print("Second Box Open")
board.digitalWrite(Red, "HIGH")
board.Servos.write(servoPin2,120)
for i in range(0,5):
entered[i] = 0
else:
print("wrong")
while True:
if board.analogRead(xpin) >=1000:
second = second +1
print("Choose Second Box")
time.sleep(0.5)
elif board.analogRead(ypin) >= 1000:
first = first +1
print("Choose First Box")
time.sleep(0.5)
if board.digitalRead(button1) == 0:
checkEntered1(1)
time.sleep(0.5)
elif board.digitalRead(button2) == 0:
checkEntered1(2)
time.sleep(0.5)
elif board.digitalRead(button3) == 0:
checkEntered1(3)
time.sleep(0.5)
elif board.digitalRead(button4) == 0:
checkEntered1(4)
time.sleep(0.5)
elif board.digitalRead(button5) == 0:
checkEntered1(5)
time.sleep(0.5)
elif board.digitalRead(button6) == 0:
close_all()
설명
학교 다닐 때 미니 프로젝트로 만든 건데.. 귀엽길래 ㅎ
가볍게만 봐주셨으면 합니다 :)
'Dev > Arduino' 카테고리의 다른 글
3. LED 연결해보자(Arduino / Python) (0) | 2021.02.27 |
---|---|
2. 아두이노 PC와 연결해보자 (0) | 2021.02.24 |
1. 아두이노 기본 기능과 종류 (0) | 2021.01.27 |
Arduino IDE 다운로드 및 설치 (0) | 2021.01.22 |