This is the code:
#include <Wire.h>
struct keyboardPin
{
int digitalPin;
int pin;
};
keyboardPin pins[7];
int lockerPin1=9;
int lockerPin2=10;
int lockerPinPwm=11;
int unlockerButtonPin=15;
int lockerButtonPin=16;
bool looked=false;
int countKeyboardPin=0;
int pin[]={0,0,0,0};
int insertedPin[4];
int speakerPin=14
;
int countInsertedPin=0;
void addPin(int digitalPin,int pin)
{
pins[countKeyboardPin].digitalPin=digitalPin;
pins[countKeyboardPin].pin=pin;
countKeyboardPin++;
}
int getLinie(int pin)
{
int line=-1;
switch(pin)
{
case 2:
line=1;
break;
case 7:
line=2;
break;
case 6:
line=3;
break;
case 4:
line=4;
break;
}
return line;
}
int getColoana(int pin)
{
int col=-1;
switch(pin)
{
case 3:
col=1;
break;
case 1:
col=2;
break;
case 5:
col=3;
break;
}
return col;
}
int transforLinColToNumber(int lin,int col)
{
return ((lin-1)*3)+col;
}
int getTasta(int pin1,int pin2)
{
int col,lin;
col=getColoana(pin1);
if(col==-1)
col=getColoana(pin2);
lin=getLinie(pin1);
if(lin==-1)
lin=getLinie(pin2);
int rez=transforLinColToNumber(lin,col);
if(rez==11)
return 0;
return rez;
}
void LockDoor()
{
Serial.println("Locking");
digitalWrite(lockerPin1,LOW);
digitalWrite(lockerPin2,HIGH);
digitalWrite(lockerPinPwm,HIGH);
delay(1000);
digitalWrite(lockerPinPwm,LOW);
digitalWrite(lockerPin1,LOW);
digitalWrite(lockerPin2,LOW);
}
void UnlockDoor()
{
Serial.println("Unlocking");
digitalWrite(lockerPin1,HIGH);
digitalWrite(lockerPin2,LOW);
digitalWrite(lockerPinPwm,HIGH);
delay(1000);
digitalWrite(lockerPinPwm,LOW);
digitalWrite(lockerPin1,LOW);
digitalWrite(lockerPin2,LOW);
}
void beep()
{
tone(speakerPin,440,250);
}
void notOkBeep()
{
tone(speakerPin,100,250);
}
void setup()
{
Wire.begin(4);
Wire.onReceive(i2cReceiveEvent);
Serial.begin(9600);
addPin(2,1);
addPin(3,2);
addPin(4,3);
addPin(5,4);
addPin(6,5);
addPin(7,6);
addPin(8,7);
pinMode(unlockerButtonPin,INPUT_PULLUP);
pinMode(lockerButtonPin,INPUT_PULLUP);
for(int i=0;i<countKeyboardPin;++i)
{
if(getLinie(pins[i].pin)!=-1)
{
pinMode(pins[i].digitalPin,OUTPUT);
digitalWrite(pins[i].digitalPin,HIGH);
}
else
pinMode(pins[i].digitalPin,INPUT_PULLUP);
}
pinMode(lockerPin1,OUTPUT);
pinMode(lockerPin2,OUTPUT);
pinMode(lockerPinPwm,OUTPUT);
pinMode(speakerPin,OUTPUT);
}
void checkPin()
{
bool ok=true;
for(int i=0;i<4;++i)
{
if(pin[i]!=insertedPin[i])
{
ok=false;
break;
}
}
if(ok)
{
Serial.println("ok");
UnlockDoor();
}
else
{
Serial.println("!ok");
notOkBeep();
}
countInsertedPin=0;
}
void pushedTasta(int number)
{
insertedPin[countInsertedPin]=number;
countInsertedPin++;
if(countInsertedPin==4)
checkPin();
}
void loop()
{
if(!digitalRead(unlockerButtonPin))
{
UnlockDoor();
}
if(!digitalRead(lockerButtonPin))
{
LockDoor();
}
for(int i=0;i<countKeyboardPin;++i)
{
if(getLinie(pins[i].pin)==-1)
continue;
digitalWrite(pins[i].digitalPin,LOW);
for(int j=0;j<countKeyboardPin;++j)
{
if(j!=i)
{
if(!digitalRead(pins[j].digitalPin))
{
int tasta=getTasta(pins[j].pin,pins[i].pin);
if(tasta==12)
{
LockDoor();
}
else
{
beep();
pushedTasta(tasta);
delay(300);
Serial.println(tasta);
}
}
}
}
digitalWrite(pins[i].digitalPin,HIGH);
}
}
void i2cReceiveEvent(int howMany)
{
int x = Wire.read();
Serial.print("Via i2c:");
Serial.println(x);
if(x==0)
{
LockDoor();
}
if(x==1)
{
UnlockDoor();
}
}
Bypass caps for logical or for motor power for l298?
I have a multimeter,I have a buzzer which makes a sound every time a key is pressed on keyboard