#define DV_PIN 7 // Define PD7 as DV_PIN
#define Senser 6
unsigned int myinput = 0; // รหัสเริ่มต้น
unsigned int Password[] = {32, 16, 8}; // รหัสขั้นแรก (เปลี่ยนตามที่คุณต้องการ)
unsigned int KEY[] = {2,8,16};
unsigned int KEY1[] = {2,8,32};
unsigned int KEY2[] = {2,16,32};
unsigned int KEY3[] = {1,8,32};
unsigned int KEY4[] = {1,4,16};
unsigned int KEY5[] = {1,8,32};
unsigned int KEY6[] = {1,2,4};
int inputIndex = 0;
unsigned int Sensor;
int tigger = 0;
void setup() {
DDRB |= 0b00111000;
DDRC &= 0b11000000;
DDRD = 0;
pinMode(DV_PIN, INPUT);
pinMode(Sensor, INPUT);
}
void IR_Reflect_Sensor() {
PORTB &= ~0b00100000;
if (digitalRead(Senser)) {
if (Sensor == 0) {
PORTB ^= 0x20; //0b00100000
}
}
delay(100);
}
void Traffic_light_module() {
PORTB |= 0b00011000;
if (tigger >= 0 && tigger <= 3) {
if (tigger == 3) {
PORTB ^= 0x08;
delay(5000);
PORTB |= 0x08;
tigger = 0;
} else if (tigger >= 0 && tigger <= 2) {
PORTB |= 0x10;
if (tigger >=2 && tigger <= 2) {
PORTB ^= 0x10;
} else {
PORTB |= 0x10;
}
}
}
}
void loop() {
IR_Reflect_Sensor();
PORTB |= 0b00011000;
if (digitalRead(Senser) == LOW) {
if(digitalRead(DV_PIN) == LOW ){
if (digitalRead(PORTC & 0b01111111) ) {
myinput = PINC & 0b00111111;
if (myinput == 1 || myinput == 2 || myinput == 4 || myinput == 8 || myinput == 16 || myinput == 32) {
//tigger += 1;
delay(100);
}
//Traffic_light_module();
if (myinput == Password[inputIndex]) {
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b001000;
delay(5000);
inputIndex = 0;
}
} else if (myinput == KEY[inputIndex] ) {
// Your code here
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b010000;
delay(5000);
inputIndex = 0;
}
}
else if (myinput == KEY1[inputIndex] ){
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b010000;
delay(5000);
inputIndex = 0;
}
}
else if (myinput == KEY2[inputIndex] ){
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b010000;
delay(5000);
inputIndex = 0;
}
}
else if (myinput == KEY3[inputIndex] ){
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b010000;
delay(5000);
inputIndex = 0;
}
}
else if (myinput == KEY4[inputIndex] ){
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b010000;
delay(5000);
inputIndex = 0;
}
}
else if (myinput == KEY5[inputIndex] ){
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b010000;
delay(5000);
inputIndex = 0;
}
}
else if (myinput == KEY6[inputIndex] ){
inputIndex++;
if (inputIndex == sizeof(Password) / sizeof(Password[0])) {
PORTB ^= 0b010000;
delay(5000);
inputIndex = 0;
}
}
}
}
delay(100);
}
}