Go Down

Topic: Sensor de movimento submisso ao sensor de luz (Read 376 times) previous topic - next topic

renandw

boa noite. tenho a plataforma do Arduino como um hobby. e no meu projeto estou tentando fazer o sensor de movimento só ser ativado quando a luz do ambiente estiver suficientemente baixa. porém não estou iniciando meu aprendizado em programação... se alguém puder me ajudar agradeço

void setup()
{
  pinMode(A0, INPUT);
  pinMode(5, OUTPUT);
  pinMode(3, INPUT);
}

void loop()
{
  if (analogRead(A0) < 200) {
    digitalWrite(5, LOW);
  } else {
    digitalWrite(5, HIGH);
  }
  if (digitalRead(3) < HIGH) {
    digitalWrite(5, LOW);
  } else {
    digitalWrite(5, HIGH);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

o pino A0 é o sensor de luz
o pino 5 é o LED
o pino 3 é o sensor de movimento

obrigado pessoal

bubulindo

Tens de encadear os if's...

Code: [Select]


  if (analogRead(A0) < 200 && digitalRead(3) != HIGH) {
    digitalWrite(5, LOW);
  } else {
    digitalWrite(5, HIGH);
  }


This... is a hobby.


mtnviewguy

coincidentemente, brinquei com um project muito similar hoje a tarde:

Controlo um Relê a partir de um sensor de movimento e um sensor de Luz. O relê só ativa se estiver escuro e algum movimento for detectado. Depois de um tempo pre-determinado, sem movimento detectado, o relê desliga.

Code: [Select]

/*
 * //////////////////////////////////////////////////
 * //making sense of the Parallax PIR sensor's output
 * //////////////////////////////////////////////////
 *
 * Switches a LED according to the state of the sensors output pin, if it's dark.
 *
 * @Author: Ruy Cesar (mtnviewguy at gmail ponto com)
 * @date: 8. May 2019
 *
 * released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
 * http://creativecommons.org/licenses/by-nc-sa/2.0/de/
 *
 * The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
 * (http://www.parallax.com/detail.asp?product_id=555-28027)
 *
 * The PIR sensor's output pin goes HIGH if motion is present.
 *
 * Also used on this sketch is a standard Photoresistor.
 *
 * Resistance decreases if more light shines on its surface
 * - In well lit room, resistance is < 10K OHM
 * - In darkness, typical resistance is > 1M OHM
 *
 */

/////////////////////////////
//VARS
//the time we give the PIR motion sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 20;

// PIR motion sensor output pin (used Analog on Nano brd because it was easy to wire). 
int sensorPin = A7;

// photoresistor pin 1 (added a 200K OHM pull down resistor on P1; photoresistor pin2 = Nano bd 3.3V)
int lightdetpin = A2;       

//Output pin that controlls LED/relay
int ledPin = 13;

// time of last detected motion (used to verify if time out occured)
long unsigned int last_motion;         

// turn off LED/rellay 'pause' miliseconds after last motion was detected
long unsigned int pause = 100000; 

/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);  //start with LED/rellay OFF

  //give the PIR sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){
     while(analogRead(lightdetpin > 200)) {    // while it's dark, check motion sensor
     if(analogRead(sensorPin) > 400){        // if motion, turn on LED/relay
       Serial.print(analogRead(lightdetpin));
       Serial.println("---");
       digitalWrite(ledPin, HIGH);      //  save(reset) last motion time
       last_motion = millis();
       delay(500);
       }

     if(analogRead(sensorPin) < 200){         // if no motion, check for time-out
        Serial.print(analogRead(lightdetpin));
        Serial.println("---");     
        if((last_motion + pause) < millis()) {
          digitalWrite(ledPin, LOW);          //If timout (no motion > pause) turn relay off
          delay(500);
          }
      } 
     }
}
     
 /*      if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }*/

Eletro_Arduino

Pessoal, boa tarde.

Caso precisem de peças Arduino e com valores acessíveis, a Eletrogate <https://www.eletrogate.com/kit-robotica> tem as melhores peças com os melhores preços do mercado.

Go Up