Hi, i'm new with arduino and i ask you some very basic help.
I have a security keyboard with a normally closed relè and a PIR sensor. I would like that when the keyboard relè is open and the PIR go active the ledPin go HIGH and remain HIGH.It will go LOW only when the keyboard relè will close again.
I can't understand why if i write:
The value must be updated inside the while loop for the while loop to see any change.
It would be better written thus:
digitalWrite(ledPin, HIGH);
while(keyboard == LOW) {
keyboard = digitalRead(3); // or whatever you do to read the keyboard value
}
digitalWrite(ledPin, LOW);
You only need to do the digitalWrite once to turn the LED on, and once to turn it off again. The while will then delay between the on and the off reading the keyboard.
majenko:
Where are you getting the value "keyboard" from?
The value must be updated inside the while loop for the while loop to see any change.
It would be better written thus:
digitalWrite(ledPin, HIGH);
while(keyboard == LOW) {
keyboard = digitalRead(3); // or whatever you do to read the keyboard value
}
digitalWrite(ledPin, LOW);
You only need to do the digitalWrite once to turn the LED on, and once to turn it off again. The while will then delay between the on and the off reading the keyboard.
Thanks for yout answer.
I understand what you are talking about but i have not been clare. I have two sensor schematically like push bottons and a keyboard with its relè. So if the relè of the keyboard is open and its level LOW i want to monitorize the two (or even more) sensors. If one of the sensors will detect something the output pin (ledPin) should go HIGH. This is what i wrote:
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(sensMov);
buttonState3 = digitalRead(tastiera);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if(buttonState3 == HIGH){
digitalWrite(ledPin, LOW);
stato=0;
}
else if (buttonState3 == LOW){
if (buttonState == LOW) {
// turn LED on:
stato=1;
}
else if (buttonState2 == LOW) {
// turn LED on:
stato=1;
}
}
if (stato>0){
while (buttonState3 == LOW){
digitalWrite(ledPin, HIGH);
stato++;
}
stato=0;
digitalWrite(ledPin, LOW);
}
}
but in this way once lightened the led never turn off.
Sorry but i haven't obtained what i would. This is the code i uploaded:
const int buttonPin = 2;
const int ledPin = 13;
const int sensMov = 3;
const int tastiera = 4;
int buttonState;
int buttonState2;
int buttonState3;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(sensMov, INPUT);
pinMode(tastiera, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(sensMov);
buttonState3 = digitalRead(tastiera);
if (buttonState3 == LOW && buttonState == LOW ){
digitalWrite(ledPin, HIGH);
while (buttonState3 == LOW){
buttonState3 = digitalRead(tastiera);
}
}
if (buttonState3 == LOW && buttonState2 == LOW ){
digitalWrite(ledPin, HIGH);
while (buttonState3 == LOW){
buttonState3 = digitalRead(tastiera);
}
}
digitalWrite(ledPin, LOW);
}
where "buttonState" and "buttonState2" are temporary button and "buttonState3" is an always open or always closed botton.
What i get is that the ledPin get on when one of the pushbotton is pressed and than turn off when the push botton is released. I would like that the ledPin stay on till I turn "HIGH" the buttonState3. Can anyone help me?
I would like that the ledPin stay on till I turn "HIGH" the buttonState3.
So you need to remember that the buttons have been pushed in a variable. You need to test that the variable shows the button pressed OR the button is being pressed to keep the light on. When the third button is pressed you then make the variable forget the button has been pressed and so make the light go off.
I would like that the ledPin stay on till I turn "HIGH" the buttonState3.
So you need to remember that the buttons have been pushed in a variable. You need to test that the variable shows the button pressed OR the button is being pressed to keep the light on. When the third button is pressed you then make the variable forget the button has been pressed and so make the light go off.
Thanks a lot for your answer, i tried in this way:
const int buttonPin = 2;
const int ledPin = 13;
const int sensMov = 3;
const int tastiera = 4;
int buttonState;
int buttonState2;
int buttonState3;
int state=0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(sensMov, INPUT);
pinMode(tastiera, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(sensMov);
buttonState3 = digitalRead(tastiera);
digitalWrite(ledPin, LOW);
state=0;
if (buttonState3 == LOW && buttonState == LOW ){
state=state++;
}
if (buttonState3 == LOW && buttonState2 == LOW ){
state=state++;
}
else{
state=0;
}
while(state > 0 && buttonState3 == LOW){
digitalWrite(ledPin, HIGH);
buttonState3 = digitalRead(tastiera);
}
}
but the led is alway on when bottonState3 is LOW. Can you tell me why?
I just wrote this, to this time is the best i did.
const int buttonPin = 2;
const int ledPin = 13;
const int sensMov = 3;
const int tastiera = 4;
int buttonState;
int buttonState2;
int buttonState3;
int state;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(sensMov, INPUT);
pinMode(tastiera, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(sensMov);
buttonState3 = digitalRead(tastiera);
if (buttonState3 == LOW && buttonState == LOW ){
state++;
}
if (buttonState3 == LOW && buttonState2 == LOW ){
state++;
}
while(state > 0 && buttonState3 == LOW){
digitalWrite(ledPin, HIGH);
buttonState3 = digitalRead(tastiera);
state++;
}
digitalWrite(ledPin, LOW);
state=0;
}
But the led continue to turn off when the bottonState and bottonState2 are not LOW. I don't understand why it goes out of the "while loop". Anyone can suggest me how to solve the problem?
I don't understand why it goes out of the "while loop".
Because that is what your code is telling it to do.
The:-
digitalWrite(ledPin, LOW);
is executed every time round the loop. If you only want to turn it off when you have the other button pressed then put an if statement in front of the code that turns it off.