Hello im kinda new to programming and i want to do that motion sensor would only work and read values only if i send '1' in serial monitor and stop rading then sent '2'
So, where's your code?
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the 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(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
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);
}
}
}
I i want to use this code for sensor,i but i cant get the right if function
Let's give him a head start, I'm bored.
// setup variables, etc.
byte okayToRead;
byte incomingByte;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available()>0){
incomingByte = Serial.read();
if (incomingByte == '1'){
okayToRead = 1;
}
if (incomingByte == '2'){
okayToRead = 2;
}
if ( incomingByte !='1' || incomingByte != '2'){
okayToRead = 3;
}
} // end serial available test
if (okayToRead == 1){
// start reading
}
if (okayToRead == 2){
// stop reading
}
if (okayToRead == 3){
Serial.println ("bad character received");
okayToRead = 0; // clear out status flag
}
} // end loop
What ever i sent i get 'bad character received'. Can someone help me?
// setup variables, etc.
byte okayToRead;
byte incomingByte;
int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int powerPin = 8;
int pirPin = 10; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
void setup(){
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
Serial.begin(9600);
}
void loop(){
if (Serial.available()>0){
incomingByte = Serial.read();
if (incomingByte == '1'){
okayToRead = 1;
}
if (incomingByte == '2'){
okayToRead = 2;
}
if ( incomingByte !='1' || incomingByte != '2'){
okayToRead = 3;
}
} // end serial available test
if (okayToRead == 1){
digitalWrite(powerPin, HIGH);
Serial.print("Sensor on");
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
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);
}
}
}
if (okayToRead == 2){
digitalWrite(powerPin, LOW);
Serial.print("Sensor off");// stop reading
}
if (okayToRead == 3){
Serial.println ("bad character received");
okayToRead = 0; // clear out status flag
}
} // end loopl
if ( incomingByte !='1' || incomingByte != '2')
?
(Hint: de Morgan)
Ok thx now it gets commands, but still doesnt run the sensor
OK, so, run the auto-format tool on your code, and repost the code
// setup variables, etc.
byte okayToRead;
byte incomingByte;
int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int powerPin = 8;
int pirPin = 10; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
pinMode(powerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read() - '0';
if (incomingByte == '1') {
okayToRead = 1;
}
if (incomingByte == '2') {
okayToRead = 2;
}
} // end serial available test
if (okayToRead == 1) {
digitalWrite(powerPin, HIGH);
Serial.print("Sensor on");
if (digitalRead(pirPin) == HIGH) {
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if (digitalRead(pirPin) == LOW) {
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
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 (!lockLow && millis() - lowIn > pause) {
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
}
}
}
if (okayToRead == 2) {
digitalWrite(powerPin, LOW);
Serial.print("Sensor off");// stop reading
}
} // end loopl
incomingByte = Serial.read() - '0'; << converts ASCII to a number
if (incomingByte == '1') { << still looking at ASCII; change to just 1.