Motor being triggered by PIR sensor

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

I'm sorry the first post was hard to read. It might be easier now.

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

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:

Code: [Select]
//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

Do you have both programs working the way you want?

Paul

Yes, They both work well independently, I just need a way to merge them so that they work together.

instead of ledPin being the output and being triggered after the PIR sensor detects motion, the motor is

Which motor? You have two motors. Clearly, you know what makes the LED come on and go off, and you know what makes the motor(s) start and stop. Just replace the LED on code with the motor(s) start code, and the LED off code with the motor(s) stop code.

@brandonk8325, do not cross-post. Threads merged.

Yes that should work but there are multiple foods that are in the code for the motor and I don’t know where those fit in the code

brandonk8325:
Yes that should work but there are multiple foods that are in the code for the motor and I don’t know where those fit in the code

Does that statement make a lick of sense to you? That looks like a randomly generated list of words to me.

Code does NOT have food in it.