Here are a couple of quick points and suggestions to determine if I'm going down the correct path to help you.
Combining the Sketsches
First, I'm assuming you understand that each sketch which runs is basically made up of :
1) Setup (runs one time each time the device is powered up)
2) Loop (runs / loops while the device is powered)
Pins and Ports
Ok, next thing is that Pins are the Arduino pins which are reading input.
Ports are pins that are writing output (setting voltage high for example).
Starting with that basic understanding we can continue thinking about this in the following way.
Insure Each Pin/Port Used For Only One Thing
Insure that no pin (D1, D2, etc) is used for two different things in each of your sketches.
Again this is basic but just setting up the idea here.
If we can guarantee that each Pin/Port is only used for one thing then we can easily combine the SETUP and LOOP portions of the 3 sketches and they _should_* all work the same way as when only one is running.
*However, there could be challenges with speed of processing inputs causing later code in the loop to not be processed until after the earlier parts of the code in the loop.
IE - if D1 is getting data in the script and then doing something and keeps the processor busy then the lower parts of the sketch will not be run until the D1 sketch portion is done. Hope that makes sense.
Here I will now attempt to combine your first two sketches:
int ledPin = 13;
int knockSensor = 0;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100;
int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void loop() {
val = analogRead(knockSensor);
if (val >= THRESHOLD) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
Serial.println("Knock!");
}
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); if(lockLow){
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW);
if(takeLowTime){
lowIn = millis(); takeLowTime = false; }
if(!lockLow && millis() - lowIn > pause){
lockLow = true;
Serial.print("motion ended at "); Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
delay(100); }
Conflicts
I don't see any major conflicts in those two sketches -- meaning that you _should_ be able to combine them and run them as I've done.
NOTE:I may have copy/pasted code improperly so please examine it more closely.
You may want to put a different LED on an output pin (port) and do a digitalWrite to that led when you do the second portion of the script so you can tell which one is lighting the appropriate LED.
Summary
Maybe you knew all of this, but this is my attempt to get you started so you can see that you should be able to easily combine the scripts with just a little work and keeping in mind that portions of the script may slow the other portions down.