Programming

Hi,
Please help fix the code for when keep button press output is only one time high and low
Thanku

kuni122000:
Hi,
Please help fix the code for when keep button press output is only one time high and low
Thanku

No need to double post!

Would you like us to wave a magic wand?

Paul

Welcome to the group.
Perhaps you ment to attach your current sketch for us to take a look at.
What is your experience in programming and electronics.

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Show us a good schematic of your circuit.
Show us a good image of your wiring.

/* switch
*

  • Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  • press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
  • a minimum delay between toggles to debounce the circuit (i.e. to ignore
  • noise).
  • David A. Mellis
  • 21 November 2006
    */

int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin

int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers

void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(outPin, state);

previous = reading;
}

Change:
long time = 0;
to:
unsigned long time = 0;

You may have to change this:
pinMode(inPin, INPUT);
to:
pinMode(inPin, INPUT_PULLUP);

How is your switch wired?

Move this:
previous = reading;
into:
the 1st 'if' block.

What are you trying to do with this project?

Hello
I am trying to make simple door bell
Because when some body keep press door bell switch
I want hear sound only one time
Thanku

How will you be connecting the switch?
How far away from the Arduino will it be?

larryd:
How will you be connecting the switch?
How far away from the Arduino will it be?

Not design yet and still trying fix the code
Thanku

@kuni122000
Add a comment to each line of this code.

Attached your commented code to this thread so we can review it.

/* switch

   Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
   press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
   a minimum delay between toggles to debounce the circuit (i.e. to ignore
   noise).

   David A. Mellis
   21 November 2006
*/

#define PUSHED HIGH

const byte outPin = 13; //
const byte inPin = 2;   // <----<<<< Connect 4.7K resistor form D02 to GND.
//                                   Connect one contact of the switch to D02, other contact to +5V

// int state = LOW;        //
int reading;            //
int previous;           //

unsigned long timeMillis;  //
unsigned long onMillis;    //

const unsigned long debounce      = 200;  //
const unsigned long duration      = 1000; //


void setup()
{
  pinMode(inPin, INPUT_PULLUP);
  previous = digitalRead(inPin);

  pinMode(outPin, OUTPUT);
  digitalWrite(outPin, LOW);

}

void loop()
{
  reading = digitalRead(inPin);

  if (previous != reading && millis() - timeMillis > debounce)
  {
    timeMillis = millis();
    previous = reading;
        
    if (reading == PUSHED)
    {
      digitalWrite(outPin, HIGH);
      onMillis = millis();
    }

  }

  if (digitalRead(outPin) == HIGH && millis() - onMillis >= duration)
  {
    digitalWrite(outPin, LOW);
  }

}// END of loop()

Edit:
Update sketch

Use the S1 example see in this schematic:
2017-08-05_20-33-24.jpg Click on the image for full resolution.

Thanks lot your response
i follow it
Thanku

@kuni122000
Just a minute, you have some homework.

Comment every line of code in the sketch then post a copy of it here so we can see your work.

.

larryd:
@kuni122000
Just a minute, you have some homework.

Comment every line of code in the sketch then post a copy of it here so we can see your work.

.

/* switch

Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
     press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
     a minimum delay between toggles to debounce the circuit (i.e. to ignore
     noise).

David A. Mellis
     21 November 2006
*/

#define PUSHED HIGH

const byte outPin = 13;//
const byte inPin = 2;// <----<<<< Connect 4.7K resistor form D02 to GND.
//Connect one contact of the switch to D02, other contact to +5V

int state = LOW;//
int reading;//
int previous;//

unsigned long timeMillis;//
unsigned long onMillis;//

const unsigned long debounce = 100; //
const unsigned long duration = 500; //

void setup()
{
  pinMode(inPin, INPUT_PULLUP);
  previous = digitalRead(inPin);

pinMode(outPin, OUTPUT);
  digitalWrite(outPin, state);

}

void loop()
{
  reading = digitalRead(inPin);

if (previous != reading && millis() - timeMillis > debounce)
  {
    timeMillis = millis();
    previous = reading;
    if (reading == PUSHED)
    {
      digitalWrite(outPin, HIGH);
      onMillis = millis();
    }

}

if (digitalRead(outPin) == HIGH && millis() - onMillis >= duration)
  {
    digitalWrite(outPin, LOW);
  }

}
// END of loop()

Guess there is a translation issue. :slight_smile:

larryd:
Guess there is a translation issue. :slight_smile:

/* switch

     Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
     press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
     a minimum delay between toggles to debounce the circuit (i.e. to ignore
     noise).

     David A. Mellis
     21 November 2006
*/

#define PUSHED HIGH

const byte outPin = 13;// for bell
const byte inPin = 2;// <----<<<< Connect 4.7K resistor form D02 to GND.
//Connect one contact of the switch to D02, other contact to +5V

int state = LOW;//the current state of the output pin
int reading;//the current state of the button pin
int previous;//the previous state of the button pin

unsigned long timeMillis;//
unsigned long onMillis;//

const unsigned long debounce = 200; //the last time the output pin was toggled
const unsigned long duration = 500; //the debounce time; increase if the output flickers


void setup()
{
  pinMode(inPin, INPUT_PULLUP);
  previous = digitalRead(inPin);

  pinMode(outPin, OUTPUT);
  digitalWrite(outPin, state);

}

void loop()
{
 
  reading = digitalRead(inPin);

  if (previous != reading && millis() - timeMillis > debounce)
  {
    timeMillis = millis();
    previous = reading;
    if (reading == PUSHED)
    {
      
      digitalWrite(outPin, HIGH);//ding
      onMillis = millis();
    
    
    
    }

  }

if (digitalRead(outPin) == HIGH && millis() - onMillis >= duration)
{
digitalWrite(outPin, LOW);//dong
  
}else{
if (digitalRead(outPin) == LOW && millis() - timeMillis <= duration)
digitalWrite(outPin,HIGH);//another ding when switch release 

digitalWrite(outPin,LOW);//another dong when switch release
}
}// END of loop()

Please describe what YOU think this line of code is doing:

if (digitalRead(outPin) == HIGH && millis() - onMillis >= duration)

Paul