Go Down

Topic: Power switch issue with an Ai PIR Sensor (Read 116 times) previous topic - next topic

onordbo

Hello, fellow Arduino developers!

I'm stuck on a slight problem with a project I'm working on at school right now. Basically what we are trying to do is simulate a "Thunderstorm"-cloud.

At the moment, the project is powered by the following components:

- Arduino UNO
- USB Power
- four single-colored LED
- Ai PIR Sensor from Adafruit
- A fuckton of wires

Basically, the issue at hand seems to be code based, as the wiring triggers everything as it is supposed to, it's just a matter of turning off the LEDs after they have completed their blink cycle.

Essentially, I want the Arduino to do the following:

-> If the PIR Sensor detects motion, run random blink cycle for x amount of seconds
-> Play a thunderstorm-sound from a bluetooth speaker, connected to the computer powering the Arduino
-> Else do nothing, wait for motion detection to run the code explained above

Here is a video demonstration of the sensor at work at this point in time, running with the code supplied in the post:
http://youtu.be/gpNCP8sNQug

This is an imgur album of the current wiring to the board (slightly unclear photos, I can take new ones if it's necessary):
http://imgur.com/a/T9rap

This is the source code from the project that I have been working with so far:

Code: [Select]

//LED variables, designating pin lights
int ledRed = 6;
int ledGreen = 8;
int ledYellow = 9;
int ledBlack = 13;

//Sensor variables
int ledPin = 13;                // choose the pin for the light
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int signal = 0;                 // variable for reading the pin status

//Designate LED states
void setup() {
  pinMode (ledRed, OUTPUT);
  pinMode (ledGreen, OUTPUT);
  pinMode (ledYellow, OUTPUT);
  pinMode (ledBlack, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  signal = digitalRead(inputPin);    // read input value
  if (signal == HIGH) {              // Check if the input state is HIGH
    if (pirState == HIGH) {           // We have just turned it on
      Serial.println("Motion");
      pirState = LOW;
      digitalWrite(ledRed, random(1));
      digitalWrite(ledGreen, random(1));
      digitalWrite(ledYellow, random(1));
      digitalWrite(ledBlack, random(1));
      delay(100);
    } else {
      Serial.println("No motion");
      digitalWrite(ledRed, 0);
      digitalWrite(ledGreen, 0);
      digitalWrite(ledYellow, 0);
      digitalWrite(ledBlack, 0);
      pirState = HIGH;
    }
  }
}


Pastebin:
http://pastebin.com/mhqBDu5x

Any and all help is greatly appreciated! I seem to be really stumped with this one.

//@onordbo

omersiar

pirState variable is useless. You are checking PIR state already

Code: [Select]
 
signal = digitalRead(inputPin);    // read input value
if (signal == HIGH) {              // Check if the input state is HIGH


with this code.

If you want to turn off leds first configure them in setup to off then when motion detected blink them and turn them off again.

omersiar

https://gist.github.com/omersiar/d970eeb0558beddaf8dc

Code: [Select]
//LED variables, designating pin lights
int ledRed = 6;
int ledGreen = 8;
int ledYellow = 9;
int ledBlack = 13;

//Sensor variables
//int ledPin = 13;                // ??? You don't use it ledBlack uses 13

int inputPin = 2;               // choose the input pin (for PIR sensor)
//int pirState = LOW;             // we going to read digital pin state no need variable
//int signal = 0;                 // no need

//Designate LED states
void setup() {
  pinMode (ledRed, OUTPUT);
  pinMode (ledGreen, OUTPUT);
  pinMode (ledYellow, OUTPUT);
  pinMode (ledBlack, OUTPUT);
  pinMode (inputPin, INPUT);  //If we dont define pin's mode this pin electrically will be different see digitalPins reference
 
  digitalWrite(ledRed, LOW);  // Turn off leds first start
  digitalWrite(ledGreen, LOW);
  digitalWrite(ledYellow, LOW);
  digitalWrite(ledBlack, LOW);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(inputPin) == HIGH) {  // You can combine digitalRead with if statement
      Serial.println("Motion");
      //pirState = LOW; // no need
      digitalWrite(ledRed, random(1));
      digitalWrite(ledGreen, random(1));
      digitalWrite(ledYellow, random(1));
      digitalWrite(ledBlack, random(1));
      delay(100);
      // here we need to turn off leds again its up to you how you want to that you can call
      // tunoffleds(); //or
      // digitalWrite(leds, LOW);
      }
    // else { else do nothing
      //Serial.println("No motion");
      //digitalWrite(ledRed, 0);
      //digitalWrite(ledGreen, 0);
      //digitalWrite(ledYellow, 0);
      //digitalWrite(ledBlack, 0);
    //}
}

Go Up