Hi, I have a problem, I want to install a pir sensor. The problem is when I want the sensor begins to detect when you send a command, such as when you send the letter 'a' through the serial monitor, recently there begins to detect movements. This is the code that I use but does not work
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int option;
// variables will change:
int buttonState = 0; // variable para leer el estado del boton
void setup() {
Serial.begin(9600);
// led como salida:
pinMode(ledPin, OUTPUT);
// sensor como entrada:
pinMode(buttonPin, INPUT);
}
void loop(){
option=Serial.read();
if(option=='a') {
buttonState = digitalRead( buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
}
In what way does it not work? If you send 'a' while applying 5V to Pin 2 the light should come on. If you send 'a' while applying 0V to Pin 2 the light should go off. Is that not the way you want it to work?
So it should work, but I'm trying not work. control the character 'a' THROUGH the serial monitor and led 13 of the Arduino board stays on forever.
Sorry if they do not understand, do not speak English, I am using a translator
if input = 'a' then check the button state. if its high, turn the led on.
else
if input = 'a' and check the button state is not high, turn the led off.
I wrote it out this way so you can see what is happening with your if statements based on bracket placement.
your only optiobn to turn off the led is if input= 'a'
What I want is that the sensor starts to work after I turn it on, then, for example, the active sending some character, the sensor begins to detect motion, if you see movement I activate led13, and if there is no movement does nothing . Nose how to achieve this.
I have a problem, I use a pir sensor to detect movements, "a small alarm," I tried it and it works, I can not get is that the PIR work after you receive the order, this is the code I am trying to do and not it works
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int option;
// variables will change:
int buttonState = 0; // variable para leer el estado del boton
void setup() {
Serial.begin(9600);
// led como salida:
pinMode(ledPin, OUTPUT);
// sensor como entrada:
pinMode(buttonPin, INPUT);
}
void loop(){
option=Serial.read();
if(option=='a') {
buttonState = digitalRead( buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
}
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int option;
// variables will change:
int buttonState = 0; // variable para leer el estado del boton
void setup() {
Serial.begin(9600);
// led como salida:
pinMode(ledPin, OUTPUT);
// sensor como entrada:
pinMode(buttonPin, INPUT);
}
void loop(){
option=Serial.read();
if(option=='a') {
buttonState = digitalRead( buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
}
If you keep crossposting AND send random people cries for help per PM, you'll get them pissed off.
You blew any chance of me helping you already, not that i think i could do so.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
void setup() {
Serial.begin(9600);
// led como salida:
pinMode(ledPin, OUTPUT);
// sensor como entrada:
pinMode(buttonPin, INPUT);
Serial.println("Button Disabled");
Serial.println("Send 'a' to enable and 'b' to disable.");
}
void loop() {
static boolean buttonEnabled = false;
switch(Serial.read()) {
case 'a':
buttonEnabled = true;
Serial.println("Button Enabled");
break;
case 'b':
buttonEnabled = false;
Serial.println("Button Disabled");
break;
}
if(buttonEnabled)
digitalWrite(ledPin, digitalRead(buttonPin));
}