0

Start button doesn't respond correctly, i can't figure out whats wrong with the code ? pls help,

this code is for 24 second and 14 second countdown timers in a same unit

comes with three physical push buttons

  1. start/pause
  2. reset
  3. shift (Arduino pin 8 as shift button)

both 1 and 2 uses arduino pin 2 and 3 for interrupts, by pressing reset counter sets for 24 second timer and shift+reset will set the timer for 14 seconds and by pressing start button will start the countdown and press again will pause the timer,

to display it uses two seven segment displays with 74LS47 Binery code decoder

here is the code

//Declaring and Initializing Variables
int p4 = 4;
int p5 = 5;
int p6 = 6;
int p7 = 7;
int p9 = 9;
int p10 = 10;
int Buzzer = 12;
boolean Standby = true;
unsigned long newTime, oldTime = 0;
const long timeDelay = 1000;
volatile int Num1 = 0;
volatile int Num2 = 0;
volatile int Zero = 0;
volatile byte Press = 0;
volatile boolean Ready = false;
volatile boolean Shifter = false;
volatile int Shift = 8;


void setup() {
  pinMode(Shift, INPUT);//Shift pin button
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  pinMode(9, OUTPUT);//FirstDigit
  pinMode(10, OUTPUT);//SecondDigit

  pinMode(12, OUTPUT);//Buzzer
  attachInterrupt(0, Starting, HIGH);
  attachInterrupt(1, ReStarting, RISING);
  digitalWrite(12, LOW); //Set it LOW first
}

void refresh() {
  SecondDigit();
  FirstDigit();
}

void Starting() {
  //Running and Stopping here
  if (Ready == false) {
    Press = 0;
  }

  Press++;
  if (Press >= 4) Press = 2;
} //End of Starting

void ReStarting() {
  //Restart sequence
  Zero = 0;
  if (Press == 0) {
    Num2 = 2;
    Num1 = 4;
    Ready = true;
    Press = 1;

  }

  if (digitalRead(Shift) == HIGH) Shifter = true;

  if (Shifter == false) {
    if (Zero == 3)Press = 1;
    Num1 = 4;
    Num2 = 2;
    Zero = 0;
    Ready = true;
    Press = Press;
  }
  else {
    if (Press == 2) {
      if ((Num1 + (Num2 * 10)) < 14) {
        Num1 = 4;
        Num2 = 1;
        Zero = 1;
        Ready = true;
        Press = 2;
      }
    }
    else {
      if (Press == 1 || Press == 3) {
        //Uncomment the next line to disable the 14 seconds reset if time is greater than 14 seconds
        if ((Num1 + (Num2 * 10)) < 14) {
          Num1 = 4;
          Num2 = 1;
          Zero = 1;
          Ready = true;
          Press = 1;
        }
      }
    }
  }
}//End of ReStarting


void Display() {
  if (Press == 2) {
    if (Zero <= 3) {
      Num1--;
      if (Num1 == 0)Zero++;
      if (Num1 == -1) {
        Num1 = 9;
        if (Num2 > 0)Num2--;

      }
    }
  }
  for (int Transition = 0; Transition < 500; Transition++) {
    FirstDigit();
    SecondDigit();
    if (Ready == true) {

      if (Zero == 3)BuzzMaker();
    }
  }
} //End of Display


void FirstDigit() {
  //Display 4 BCD digits of Number in First LED Segment
  digitalWrite(10, LOW);
  digitalWrite(9, HIGH);
  if (digitalRead(Shift) == HIGH) Shifter = true;
  digitalWrite(4, bitRead(Num1, 0));
  digitalWrite(5, bitRead(Num1, 1));
  digitalWrite(6, bitRead(Num1, 2));
  digitalWrite(7, bitRead(Num1, 3));
  delay(1);
  Shifter = false;
} //End of FirstDigit


void SecondDigit() {
  //Display 4 BCD digits of Number in Second LED Segment
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  if (digitalRead(Shift) == HIGH) Shifter = true;
  digitalWrite(4, bitRead(Num2, 0));
  digitalWrite(5, bitRead(Num2, 1));
  digitalWrite(6, bitRead(Num2, 2));
  digitalWrite(7, bitRead(Num2, 3));
  delay(1);
  Shifter = false;
} //End of SecondDigit

void BuzzMaker() {
  //Configuring Buzzer frequency and setting go with the counting numerals
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(12, HIGH);

  delay(1000);
  digitalWrite(12, LOW);
  delay(200);
  digitalWrite(12, HIGH);

  delay(500);
  digitalWrite(12, LOW);

  Press = 2;
  Zero = 0;
  Ready = true;
  Shifter = false;
}//End of BuzzMaker


void loop() {
  if (Press == 0)refresh;
  refresh();
  newTime = millis();
  if ((newTime - oldTime) >= timeDelay) {
    oldTime = newTime;

    if (Num2 == 0 && Num1 == 0 && Press == 2) {

      Press = 1;
    }

    //Here we are going to access each case with each different routine by case
    switch (Press) {

      case 0://Initial

        Display();
        break;

      case 1://Stop - Displaying 24 but still not yet running

        Display();
        break;

      case 2://Running

        Display();
        // PlayTime();
        break;

    }
  }//End of millis() condition

} //End of main loop function
  • What do you mean by "doesn't respond correctly"? Can you tell us what it's doing and what it should be doing? – VE7JRO Sep 16 at 16:21
  • 1
    Your indentation is a mess, making it hard to follow your code. Can you clean up your indentation? You also need to document your code and explain what both what it's supposed to do and how, in detail, it fails to accomplish your goals. – Duncan C Sep 16 at 16:25
  • Without going through your (messy) code: have you debounced the button? – Sim Son Sep 16 at 16:29
  • i am apologizing for my indentations... doesn't respond means at the first when i press start button it starts at once then i press again it wont pause i have to press start two or three times to pause it the again to start same sometimes it respond quickly some times not at all – Amal R Sep 16 at 16:41
  • 1
    one of the person from a forum told me to add a debounce which i dont know how to add... i already add a physical debounce using diode capacitor and resistor which gives some clearing up but still the start button response isn't smooth – Amal R Sep 16 at 16:50
0

There is a library you can include that debounces buttons Bounce2.

Here is a test sketch that debounces a button and lets you change the "state" of a variable.

// Connect one end of a N.O. push button switch to
// GND and the other end to pin 2 of the Arduino.
#include <Bounce2.h>

byte buttonState = 0;
const byte modeButtonPin = 2;
const unsigned long debouncerInterval = 50;  // Time in ms.

Bounce modeButton = Bounce();

void setup(){
  Serial.begin(9600);
  pinMode(modeButtonPin, INPUT_PULLUP);
  modeButton.attach(modeButtonPin);
  modeButton.interval(debouncerInterval);
}

void loop(){
  if(modeButton.update()){
    if(modeButton.read() == 0){
      buttonState++;
      if(buttonState > 2){buttonState = 0;}
      Serial.println(buttonState);
    }
  }
}
  • my dear friend VE7JRO i humbly thank you for helping me, i am a total newcomer with very little experience ... pardon my idiosyncrasies, since i use pin 2 as interrupt 0, i dont no how to implement this debounce code in to my code because i didn't declare it as a pin 2 in my code, can you help me to add it – Amal R Sep 16 at 18:11
  • You can use any digital pin. I just used pin 2 in the example sketch. – VE7JRO Sep 16 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.