Pages: [1]   Go Down
Author Topic: Momentary output signal  (Read 105 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone . i have this sketch below, where i am to HOLD(and not release) a push button(input) and i have a relay(output) that i want to act as a button (momentarily triggering) . whereas if i HOLD the input, the relay will momentarily be ON, and after 200 milliseconds, it will be off .
and if i Release the button (from holding) , it will also be momentarily ON for 200milliseconds and after that it will be OFF.

however, this is what happens with my sketch below;
whenever i HOLD my input button, the process keeps on repeating , the relay is momentarily ON, then OFF, and then it will trigger again ON and then off, but i am not yet releasing the input button .

what i want is that at the time that i hold the input button (and not yet releasing), it will be momentarily ON, after 200ms it will be OFF and that's it. it must happen for only ONE time the moment i hold it . and the same must be true at the time that i release the input button.

what is the simplest way to do it ?


/////////this is my sketch///////////////////



const int buttonPin = 2;     
int relay1 =  1;

void setup() {

pinMode(relay1, OUTPUT);
pinMode(buttonPin, INPUT);   
}

void loop(){

  int buttonState = digitalRead(buttonPin);

   if (buttonState == HIGH) {     
    digitalWrite(relay1, HIGH);
    delay(200);
    digitalWrite(relay1, LOW);
  }
  else {
    digitalWrite(relay1, HIGH);
    delay(200);
    digitalWrite(relay1, LOW);
  }
}
Logged

UK
Offline Offline
Tesla Member
***
Karma: 134
Posts: 7594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want the one button to do two things your code needs to detect when the button state CHANGES - not what it IS.

Something like this (not complete and not tested)

Code:
byte newButtonState = HIGH;
byte prevButtonState = HIGH;
boolean relayState = false;
unsigned long prevRelayMillis;
unsigned long relayIntervalMillis = 200;

void setup() {
   // usual stuff
}

void loop() {
  readButton();
  operateRelay();
}

void readButton() {
   newButtonState = digitalRead(buttonPin);
   if (newButtonState != prevButtonState) {
      relayState = true;
   }
}

void operateRelay() {
   if (relayState == true) {
       digitalWrite(relayPin, HIGH);
       prevRelayMillis = millis();
       relayState = false;
    }
   if (millis() - prevRelayMillis >= relayIntervalMillis)
      digitalWrite(relayPin, LOW);
  }
}

...R

Edited to correct an error. What is now relayState = true; had (incorrectly) been relayState = 'S';
  Error spotted by @UKHeliBob
...R
« Last Edit: September 09, 2014, 04:41:05 am by Robin2 » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 116
Posts: 4306
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Robin - I appreciate that your code was incomplete and untested but
Code:
boolean relayState = false;
and
Code:
relayState = 'S';
are not consistent.

Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
are not consistent.
But, annoyingly enough, not wrong.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 134
Posts: 7594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Robin - I appreciate that your code was incomplete and untested but
Code:
boolean relayState = false;
and
Code:
relayState = 'S';
are not consistent.
Thanks for spotting that

I was trying two approaches and got mixed up. It should be relayState = true;

I will correct the code.

...R
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 116
Posts: 4306
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
are not consistent.
But, annoyingly enough, not wrong.
That's why I chose my words carefully.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 544
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Monitor the time of the initial press, and do not let another press occur until some as yet undefined time interval has passed.
That's the basis of Blink Without Delay.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: