how to make dry contact with serial reading one time in every state in Arduino
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I did this code and when button is released open message is in loop but when I press the button the "close" message keep no loop
how can I show "open" message one time not in loop...I mean when button release then the "open" message shows one time?
So when button is release will write "open" in serial , then when push the buton the write "close" in serial.
int Switch = 2;
int buttonState; // current state of the button
int lastButtonState = 1; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == LOW && buttonState == lastButtonState) {
Serial.println("Open");
delay(100);
}
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState){
Serial.println("Close");
}
lastButtonState = buttonState;
}
arduino-uno
add a comment |
I did this code and when button is released open message is in loop but when I press the button the "close" message keep no loop
how can I show "open" message one time not in loop...I mean when button release then the "open" message shows one time?
So when button is release will write "open" in serial , then when push the buton the write "close" in serial.
int Switch = 2;
int buttonState; // current state of the button
int lastButtonState = 1; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == LOW && buttonState == lastButtonState) {
Serial.println("Open");
delay(100);
}
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState){
Serial.println("Close");
}
lastButtonState = buttonState;
}
arduino-uno
add a comment |
I did this code and when button is released open message is in loop but when I press the button the "close" message keep no loop
how can I show "open" message one time not in loop...I mean when button release then the "open" message shows one time?
So when button is release will write "open" in serial , then when push the buton the write "close" in serial.
int Switch = 2;
int buttonState; // current state of the button
int lastButtonState = 1; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == LOW && buttonState == lastButtonState) {
Serial.println("Open");
delay(100);
}
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState){
Serial.println("Close");
}
lastButtonState = buttonState;
}
arduino-uno
I did this code and when button is released open message is in loop but when I press the button the "close" message keep no loop
how can I show "open" message one time not in loop...I mean when button release then the "open" message shows one time?
So when button is release will write "open" in serial , then when push the buton the write "close" in serial.
int Switch = 2;
int buttonState; // current state of the button
int lastButtonState = 1; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == LOW && buttonState == lastButtonState) {
Serial.println("Open");
delay(100);
}
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState){
Serial.println("Close");
}
lastButtonState = buttonState;
}
arduino-uno
arduino-uno
asked Nov 23 '18 at 16:18
Mohammad KhaledMohammad Khaled
58
58
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
use a flag instead of lastButtonState!
for example:
int flag=0; //global
void loop(){
if(digitalRead(Switch) == LOW && flag==0){
flag=1;
}
else if(digitalRead(Switch)==HIGH && flag ==0){
flag=2;
}
if(flag==1){
//print whatever you want
flag=0;
}
else if(flag==2){
//print the second case
flag=0;
}}
Also, it is not a good practice to read the button state like that. Because buttons will be pressed Async to the system and you should denounce them.
if(digitalRead(SWITCH)==LOW){
delay(40);
if(digitalRead(SWITCH)==LOW)
changeButtonState();
}
I tried Flag, and gave me boncing loop between "open" and "close" and when press the button gave me "close" in loop!
– Mohammad Khaled
Nov 24 '18 at 6:23
add a comment |
I got what I want from the code like this:
int Switch = 2; //Pin for sensor switch
int buttonState; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
Serial.println("open");
delay(180);
}
lastButtonState = buttonState;
if (digitalRead(Switch) == LOW && buttonState == 1){
Serial.println("Close");
delay(200);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449982%2fhow-to-make-dry-contact-with-serial-reading-one-time-in-every-state-in-arduino%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
use a flag instead of lastButtonState!
for example:
int flag=0; //global
void loop(){
if(digitalRead(Switch) == LOW && flag==0){
flag=1;
}
else if(digitalRead(Switch)==HIGH && flag ==0){
flag=2;
}
if(flag==1){
//print whatever you want
flag=0;
}
else if(flag==2){
//print the second case
flag=0;
}}
Also, it is not a good practice to read the button state like that. Because buttons will be pressed Async to the system and you should denounce them.
if(digitalRead(SWITCH)==LOW){
delay(40);
if(digitalRead(SWITCH)==LOW)
changeButtonState();
}
I tried Flag, and gave me boncing loop between "open" and "close" and when press the button gave me "close" in loop!
– Mohammad Khaled
Nov 24 '18 at 6:23
add a comment |
use a flag instead of lastButtonState!
for example:
int flag=0; //global
void loop(){
if(digitalRead(Switch) == LOW && flag==0){
flag=1;
}
else if(digitalRead(Switch)==HIGH && flag ==0){
flag=2;
}
if(flag==1){
//print whatever you want
flag=0;
}
else if(flag==2){
//print the second case
flag=0;
}}
Also, it is not a good practice to read the button state like that. Because buttons will be pressed Async to the system and you should denounce them.
if(digitalRead(SWITCH)==LOW){
delay(40);
if(digitalRead(SWITCH)==LOW)
changeButtonState();
}
I tried Flag, and gave me boncing loop between "open" and "close" and when press the button gave me "close" in loop!
– Mohammad Khaled
Nov 24 '18 at 6:23
add a comment |
use a flag instead of lastButtonState!
for example:
int flag=0; //global
void loop(){
if(digitalRead(Switch) == LOW && flag==0){
flag=1;
}
else if(digitalRead(Switch)==HIGH && flag ==0){
flag=2;
}
if(flag==1){
//print whatever you want
flag=0;
}
else if(flag==2){
//print the second case
flag=0;
}}
Also, it is not a good practice to read the button state like that. Because buttons will be pressed Async to the system and you should denounce them.
if(digitalRead(SWITCH)==LOW){
delay(40);
if(digitalRead(SWITCH)==LOW)
changeButtonState();
}
use a flag instead of lastButtonState!
for example:
int flag=0; //global
void loop(){
if(digitalRead(Switch) == LOW && flag==0){
flag=1;
}
else if(digitalRead(Switch)==HIGH && flag ==0){
flag=2;
}
if(flag==1){
//print whatever you want
flag=0;
}
else if(flag==2){
//print the second case
flag=0;
}}
Also, it is not a good practice to read the button state like that. Because buttons will be pressed Async to the system and you should denounce them.
if(digitalRead(SWITCH)==LOW){
delay(40);
if(digitalRead(SWITCH)==LOW)
changeButtonState();
}
answered Nov 23 '18 at 22:46
TheEngineerTheEngineer
373111
373111
I tried Flag, and gave me boncing loop between "open" and "close" and when press the button gave me "close" in loop!
– Mohammad Khaled
Nov 24 '18 at 6:23
add a comment |
I tried Flag, and gave me boncing loop between "open" and "close" and when press the button gave me "close" in loop!
– Mohammad Khaled
Nov 24 '18 at 6:23
I tried Flag, and gave me boncing loop between "open" and "close" and when press the button gave me "close" in loop!
– Mohammad Khaled
Nov 24 '18 at 6:23
I tried Flag, and gave me boncing loop between "open" and "close" and when press the button gave me "close" in loop!
– Mohammad Khaled
Nov 24 '18 at 6:23
add a comment |
I got what I want from the code like this:
int Switch = 2; //Pin for sensor switch
int buttonState; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
Serial.println("open");
delay(180);
}
lastButtonState = buttonState;
if (digitalRead(Switch) == LOW && buttonState == 1){
Serial.println("Close");
delay(200);
}
}
add a comment |
I got what I want from the code like this:
int Switch = 2; //Pin for sensor switch
int buttonState; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
Serial.println("open");
delay(180);
}
lastButtonState = buttonState;
if (digitalRead(Switch) == LOW && buttonState == 1){
Serial.println("Close");
delay(200);
}
}
add a comment |
I got what I want from the code like this:
int Switch = 2; //Pin for sensor switch
int buttonState; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
Serial.println("open");
delay(180);
}
lastButtonState = buttonState;
if (digitalRead(Switch) == LOW && buttonState == 1){
Serial.println("Close");
delay(200);
}
}
I got what I want from the code like this:
int Switch = 2; //Pin for sensor switch
int buttonState; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
Serial.begin(9600);
pinMode(Switch, INPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(Switch);
if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
Serial.println("open");
delay(180);
}
lastButtonState = buttonState;
if (digitalRead(Switch) == LOW && buttonState == 1){
Serial.println("Close");
delay(200);
}
}
answered Nov 24 '18 at 9:43
Mohammad KhaledMohammad Khaled
58
58
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449982%2fhow-to-make-dry-contact-with-serial-reading-one-time-in-every-state-in-arduino%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown