Problem with interrupt in arduino mega 2560

Hello,

I'm using

  • arduino mega 2560
  • push button connected on a digital pin 41
  • lcd 2 * 16

I'm editing this code below to do that

1 - count number of times the push button pressed and display on lcd
2 - wait 5 Secs using millis () until reach the stability of counts number i need to execute specific part of code
3 - (and if i pressed the push button again during the execution of a function it TERMINATES
and clear counter and recount the clicks to do something else with another number of counts )

BUT the problem is

it does all i need except the 3rd condition

  • when i press the push button , there is no priority or accuracy, i must push it too many clicks to reset and recount

  • Do i need an interrupt ? , and how can i use it ? , because I've never used it before .

Thanks

#include <LiquidCrystal.h>
#include <Servo.h>     // header file for Servo Motors 

Servo Servo4;
Servo Servo1;
Servo Servo3;
Servo Servo2;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(42, 40, 38, 36, 34, 32);
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 5000;           // interval at which to blink (milliseconds)

// this constant won't change:
const int  buttonPin = 41;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int finalCounter = 0; 
void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Start");
  Servo4.attach(48);
  Servo1.attach(50);
  Servo2.attach(52);
  Servo3.attach(46); 

  Servo4.write(90);
  Servo1.write(90);
  Servo2.write(20);
  Servo3.write(150);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last!=current)
  {
    delay(5);
    current = digitalRead(buttonPin);

  }
  return current ; 
}



void loop() {
  // read the pushbutton input pin:
  buttonState = debounce(lastButtonState);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH)
    {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
      lcd.clear();
      lcd.print(buttonPushCounter);

    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (buttonState == LOW)
      finalCounter = buttonPushCounter ;
  }


  if (finalCounter == 3) {
    digitalWrite(ledPin, HIGH);
    lcd.clear();
    lcd.print("buttonPressed 3");
    Servo4.write(70);

  }

  else if ( finalCounter==5)
  {
    lcd.clear();
    lcd.print("buttonPressed 5");
    Servo1.write(70);

  }

  else if ( finalCounter==7){

    lcd.clear();
    lcd.print("buttonPressed 7");
    Servo2.write(90);

  }

  else if ( finalCounter>=9)
  {
    lcd.clear();
    lcd.print("ClearButton");
    buttonPushCounter = 0 ;

    Servo3.write(90);
  }
}

Sgermany:

  • Do i need an interrupt ?

The simple (and complete) answer to this question is NO.

I have been looking at your code in my text editor for a few minutes and I can't make sense of it. Can you provide a plain language description of how it is supposed to work.

And I don't know if I understand your project description - this is what it means to me

  • press a button and immediately start a timer
  • count the button presses during the next 5 seconds
  • call different functions depending on the number of button presses
  • if the button is pressed while one of the functions is happening it should stop immediately and the process should start over.

The only functions that I see in your program are setup(), loop() and debounce() so I can't figure what should stop when a button is pressed.

...R

here is what i need ,

  • press a button and immediately start a timer
  • count the button presses
  • check if the button is low , wait 5 secs
  • perform some actions depending on the number of button presses
    -( if the button is pressed while one of the actions is happening it should stop immediately and the process should start over.)

the problem is i don't know how to do the last line between practice,
how to get attention if the switch is high during the execution of an action

i added this 5 secs because i may need to execute the code depends on "7" presses
before the code depends on "3" presses ,
and in a previous code without timer
the counter begins from "0"
when it reach "3" it executes it's statement directly without giving me a chance to press "7 "presses !

i'm using "actions " here to describe the statements of " if " function at the end of the code
which depends on the number of presses

finally , i'm very sorry for my poor English that's all the vocabulary i know to describe with. :smiley:
Don't let that prevent u from helping me ! :smiley: :smiley: :smiley:

Sgermany:
here is what i need ,

  • press a button and immediately start a timer
  • count the button presses
  • check if the button is low , wait 5 secs

Does this mean that you count all the presses up to the point where the user holds the button down for 5 secs ?

  • perform some actions depending on the number of button presses
    -( if the button is pressed while one of the actions is happening it should stop immediately and the process should start over.)

the problem is i don't know how to do the last line between practice,
how to get attention if the switch is high during the execution of an action

As I said in my earlier post I don't see any actions/functions in your code that could be interrupted. What sort of things have you in mind?

finally , i'm very sorry for my poor English that's all the vocabulary i know to describe with. :smiley:

Your English is fine

...R

Does this mean that you count all the presses up to the point where the user holds the button down for 5 secs ?

No, it means count all the presses until the switch is low for 5 secs and then begin what the number indicates to (5 secs without presses)

if (buttonState == LOW)
      finalCounter = buttonPushCounter ;

As I said in my earlier post I don't see any actions/functions in your code that could be interrupted. What sort of things have you in mind?

here is what i mean

  if (finalCounter == 3) {
    digitalWrite(ledPin, HIGH);
    lcd.clear();
    lcd.print("buttonPressed 3");
    Servo4.write(70);

  }

  else if ( finalCounter==5)
  {
    lcd.clear();
    lcd.print("buttonPressed 5");
    Servo1.write(70);

  }

  else if ( finalCounter==7){

    lcd.clear();
    lcd.print("buttonPressed 7");
    Servo2.write(90);

  }

  else if ( finalCounter>=9)
  {
    lcd.clear();
    lcd.print("ClearButton");
    buttonPushCounter = 0 ;

    Servo3.write(90);

Your English is fine

Thank U :slight_smile:

Sgermany:
No, it means count all the presses until the switch is low for 5 secs and then begin what the number indicates to (5 secs without presses)

You have lost me. Are there two buttons, one is held down continuously while the other is pressed and released X number of times?

I don't see how a single button could be low for 5 seconds and also be switching on and off to cause the counter to increment.

here is what i mean

  if (finalCounter == 3) {

digitalWrite(ledPin, HIGH);
   lcd.clear();
   lcd.print("buttonPressed 3");
   Servo4.write(70);

}

else if ( finalCounter==5)
  ... SNIP....

I presume only one of these sections will work at any one time (depending on the number of switch-presses. But it only takes a fraction of a second for the servo to move so I can't see how it would be practical to interrupt it with another button press.

...R