Skip to content

Instantly share code, notes, and snippets.

@archmangler
Last active June 13, 2020 17:16
Show Gist options
  • Save archmangler/b2d8c84ce4375c46234dbc04874c3d4f to your computer and use it in GitHub Desktop.
Save archmangler/b2d8c84ce4375c46234dbc04874c3d4f to your computer and use it in GitHub Desktop.
ESP32 Motion Detection With Interrupts (4-channel input , 4 channel output)
  • Processinng code for an ESP32-based obstacle detection system with 4 channel input and 4 channel output
/*********
  Traiano Welcome
  Copied heavily from 
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

#define timeSeconds 10

// Set GPIOs for LED and PIR Motion Sensor
const int led0 = 23;
const int led1 = 22;
const int led2 = 19;
const int led3 = 21;

const int motionSensor0 = 33;
const int motionSensor1 = 32;
const int motionSensor2 = 34;
const int motionSensor3 = 35;

// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;

// Checks if motion was detected, sets LED HIGH and starts a timer
void IRAM_ATTR detectsMovement0() {
  Serial.println("OBSTACLE DETECTED LEFT CORNER!!!");
  digitalWrite(led0, HIGH);
  startTimer = true;
  lastTrigger = millis();
}

void IRAM_ATTR detectsMovement1() {
  Serial.println("OBSTACLE DETECTED LEFT SIDE!!!");
  digitalWrite(led1, HIGH);
  startTimer = true;
  lastTrigger = millis();
}

void IRAM_ATTR detectsMovement2() {
  Serial.println("OBSTACLE DETECTED LEFT SIDE!!!");
  digitalWrite(led2, HIGH);
  startTimer = true;
  lastTrigger = millis();
}

void IRAM_ATTR detectsMovement3() {
  Serial.println("OBSTACLE DETECTED LEFT SIDE!!!");
  digitalWrite(led3, HIGH);
  startTimer = true;
  lastTrigger = millis();
}

void setup() {
  // Serial port for debugging purposes
  Serial.begin(115200);
  
  //Motion Sensor mode INPUT_PULLUP
  pinMode(motionSensor0, INPUT_PULLUP);
  pinMode(motionSensor1, INPUT_PULLUP);
  pinMode(motionSensor2, INPUT_PULLUP);
  pinMode(motionSensor3, INPUT_PULLUP);

  // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
  attachInterrupt(digitalPinToInterrupt(motionSensor0), detectsMovement0, RISING);
  attachInterrupt(digitalPinToInterrupt(motionSensor1), detectsMovement1, RISING);
  attachInterrupt(digitalPinToInterrupt(motionSensor2), detectsMovement2, RISING);
  attachInterrupt(digitalPinToInterrupt(motionSensor3), detectsMovement3, RISING);

  // Set LED to LOW
  pinMode(led0, OUTPUT);
  digitalWrite(led0, LOW);
  
  pinMode(led1, OUTPUT);
  digitalWrite(led1, LOW);
  
  pinMode(led2, OUTPUT);
  digitalWrite(led2, LOW);
  
  pinMode(led3, OUTPUT);
  digitalWrite(led3, LOW);
  
}

void loop() {
  // Current time
  now = millis();
  // Turn off the LED after the number of seconds defined in the timeSeconds variable
  if(startTimer && (now - lastTrigger > (timeSeconds*100))) {
    Serial.println("Motion stopped...");
    
    digitalWrite(led0, LOW);
    startTimer = false;

    digitalWrite(led1, LOW);
    startTimer = false;

    digitalWrite(led2, LOW);
    startTimer = false;

    digitalWrite(led3, LOW);
    startTimer = false;
    
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment