Skip to content

Instantly share code, notes, and snippets.

@theapi
Last active December 30, 2015 04:49
Show Gist options
  • Save theapi/7778399 to your computer and use it in GitHub Desktop.
Save theapi/7778399 to your computer and use it in GitHub Desktop.
tmp ir noticing (sillyness)
#define LED_LEFT_PIN 4
#define LED_RIGHT_PIN 3
#define PIR_LEFT_PIN 12
#define PIR_RIGHT_PIN 11
#define MOTOR_LEFT_PIN 6
#define MOTOR_RIGHT_PIN 5
#define F_PIR_LEFT 1 // 1 = alarm on 0 = alarm off
#define F_PIR_RIGHT 2 // 1 = alarm on 0 = alarm off
#define F_MOTOR_LEFT 3
#define F_MOTOR_RIGHT 4
long previousMillis = 0;
long interval = 500; // interval
long prevPirLowLeft = 0;
long prevPirLowRight = 0;
long pirDebounce = 3000; // Ignore high for this long after other pir low
byte toggle = 0;
volatile byte flags = 0;
void setup() {
pinMode(MOTOR_LEFT_PIN, OUTPUT);
pinMode(MOTOR_RIGHT_PIN, OUTPUT);
pinMode(LED_LEFT_PIN, OUTPUT);
pinMode(LED_RIGHT_PIN, OUTPUT);
pinMode(PIR_LEFT_PIN, INPUT); // declare sensor as input
pinMode(PIR_RIGHT_PIN, INPUT);
bitClear(flags, F_PIR_LEFT);
bitClear(flags, F_PIR_RIGHT);
bitClear(flags, F_MOTOR_LEFT);
bitClear(flags, F_MOTOR_RIGHT);
//Serial.begin(9600);
// the pirs are both attached to pin 2 aswell as ther own pin
// so they can both cause an interupt
//attachInterrupt(0, pirChange, CHANGE);
}
void loop() {
/*
if (digitalRead(PIR_PIN) == LOW) { // falling
Serial.println("no");
digitalWrite(LED_PIN, LOW);
} else {
Serial.println("Motion detected!");
digitalWrite(LED_PIN, HIGH);
}
delay(1);
*/
unsigned long currentMillis = millis();
pir();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
motorLeft();
motorRight();
}
}
void pir() {
unsigned long nowMillis = millis();
// Ignore the left pir if the right is on.
if (bitRead(flags, F_PIR_RIGHT) == 0) {
if (digitalRead(PIR_LEFT_PIN) == LOW) {
if (bitRead(flags, F_PIR_LEFT) == 1) {
digitalWrite(LED_LEFT_PIN, LOW);
bitClear(flags, F_PIR_LEFT);
prevPirLowRight = nowMillis;
}
} else {
// high
if (nowMillis - prevPirLowLeft > pirDebounce) {
digitalWrite(LED_LEFT_PIN, HIGH);
bitSet(flags, F_PIR_LEFT);
//Serial.println("Motion detected!");
}
}
}
// Ignore the right pir if the left just fired.
if (bitRead(flags, F_PIR_LEFT) == 0) {
if (digitalRead(PIR_RIGHT_PIN) == LOW) {
if (bitRead(flags, F_PIR_RIGHT) == 1) {
digitalWrite(LED_RIGHT_PIN, LOW);
bitClear(flags, F_PIR_RIGHT);
prevPirLowLeft = nowMillis;
}
} else {
// high
if (nowMillis - prevPirLowRight > pirDebounce) {
digitalWrite(LED_RIGHT_PIN, HIGH);
bitSet(flags, F_PIR_RIGHT);
//Serial.println("Motion detected!");
}
}
}
}
void motorLeft() {
if (bitRead(flags, F_PIR_LEFT) == 1) {
if (bitRead(flags, F_MOTOR_LEFT) == 0) {
analogWrite(MOTOR_LEFT_PIN, 200);
bitSet(flags, F_MOTOR_LEFT);
} else {
//bitClear(flags, F_MOTOR_LEFT);
analogWrite(MOTOR_LEFT_PIN, 0);
}
} else {
// no motor movement
bitClear(flags, F_MOTOR_LEFT);
analogWrite(MOTOR_LEFT_PIN, 0);
}
}
void motorRight() {
// a slower motor
if (bitRead(flags, F_PIR_RIGHT) == 1) {
if (bitRead(flags, F_MOTOR_RIGHT) == 0) {
analogWrite(MOTOR_RIGHT_PIN, 220);
bitSet(flags, F_MOTOR_RIGHT);
} else {
analogWrite(MOTOR_RIGHT_PIN, 0);
}
} else {
// no motor movement
bitClear(flags, F_MOTOR_RIGHT);
analogWrite(MOTOR_RIGHT_PIN, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment