I have two pieces of code. One for the PIR sensor:
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
And one for the motor:
//2-Way motor control
int motorPin1 = 5; // One motor wire connected to digital pin 5
int motorPin2 = 6; // One motor wire connected to digital pin 6
torPin
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pins as an output:
pinMode(motorPin1, OUTPUT);
pinMode(mo2, OUTPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
rotateLeft(150, 500);
rotateRight(50, 1000);
rotateRight(150, 1000);
rotateRight(200, 1000);
rotateLeft(255, 500);
rotateRight(10, 1500);
}
void rotateLeft(int speedOfRotate, int length){
analogWrite(motorPin1, speedOfRotate); //rotates motor
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
delay(length); //waits
digitalWrite(motorPin1, LOW); // set the Pin motorPin1 LOW
}
void rotateRight(int speedOfRotate, int length){
analogWrite(motorPin2, speedOfRotate); //rotates motor
digitalWrite(motorPin1, LOW); // set the Pin motorPin1 LOW
delay(length); //waits
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
}
void rotateLeftFull(int length){
digitalWrite(motorPin1, HIGH); //rotates motor
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
delay(length); //waits
digitalWrite(motorPin1, LOW); // set the Pin motorPin1 LOW
}
void rotateRightFull(int length){
digitalWrite(motorPin2, HIGH); //rotates motor
digitalWrite(motorPin1, LOW); // set the Pin motorPin1 LOW
delay(length); //waits
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
}
I would like to combine the Two and make it so that instead of ledPin being the output and being triggered after the PIR sensor detects motion, the motor is triggered