I have no reasoning for using an analog input vs digital. I just thought I was supposed to use an analog. I'm very new to coding the arduino too.
//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
#define TRANSISTOR 4 //the output pin where the BASE pin for the transistor is connectedint 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
pinMode (TRANSISTOR, OUTPUT); //sets the TRANSISTOR as an output
Serial.begin(9600); //sets up the serial port
}void loop () {
val = analogRead(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
}