Here is what I think needs to be changed, in bold. In, addition, physically wire up the signal wire from the PIR to digital pin 2 instead of analogue pin 2.
//Closes a circuit on a hacked keyboard to type a letter with the use of a relay
#define SENSOR 2 //the input pin where the PIR sensor is connected (digital pin 2)
#define TRANSISTOR 4 //the output pin where the BASE pin for the transistor is connected (digital pin 4)int val = 0; //the state of the PIR input pin
int val2 = 0; //the state of the transistor output pinvoid setup() {
pinMode (SENSOR, INPUT); //sets SENSOR as an input
digitalWrite(SENSOR, HIGH); //enables the internal pull-up
pinMode (TRANSISTOR, OUTPUT); //sets the TRANSISTOR as an output
Serial.begin(9600); //sets up the serial port
}void loop () {
val = digitalRead(SENSOR); //read the state of SENSOR and store it
val2 = digitalRead(TRANSISTOR); //read the state of TRANSISTOR and store itif (val == LOW) { //check to see if the PIR has been tripped
digitalWrite(TRANSISTOR, LOW); //stop sending voltage to TRANSISTOR
} else {
digitalWrite(TRANSISTOR, HIGH); //send voltage to TRANSISTOR
}Serial.println(val2); //print out the state of val2
if (val2 == HIGH) { //check to see if TRANSISTOR pin is sending any voltage
digitalWrite(TRANSISTOR, LOW); //stop sending any voltage to TRANSISTOR
delay(2000); //wait two seconds for the PIR to stop sending a signal
}
delay(10); //delay 10ms for program stability
}