Struggling with interrupts for project

Hey guys, I have been working on a project recently but I feel I need to use interrupts but am struggling to understand them.

#include <Servo.h>

Servo myservo_1;
Servo myservo_2;
Servo myservo_3;
Servo myservo_4;

int pos = 0;
int servovar = 0;
int last_Button_State = LOW;
int current_Button_State = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool WasHigh = false;
bool WasHigh_1 = false;

void servo_1_turn_1(){for (pos = 90; pos >= 35; pos -= 1) {myservo_1.write(pos);delay(15);}}
void servo_1_turn_2(){for (pos = 35; pos <= 150; pos += 1) {myservo_1.write(pos);delay(15);}}
void servo_1_turn_3(){for (pos = 150; pos >= 35; pos -= 1) {myservo_1.write(pos);delay(15);}}
void servo_2_turn_1(){for (pos = 90; pos >= 35; pos -= 1) {myservo_2.write(pos);delay(15);}}
void servo_2_turn_2(){for (pos = 35; pos <= 150; pos += 1) {myservo_2.write(pos);delay(15);}}
void servo_2_turn_3(){for (pos = 150; pos >= 90; pos -= 1) {myservo_2.write(pos);delay(15);}}
void servo_3_turn_1(){for (pos = 90; pos <= 150; pos += 1) {myservo_3.write(pos);delay(15);}}
void servo_3_turn_2(){for (pos = 150; pos >= 90; pos -= 1) {myservo_3.write(pos);delay(15);}}
void servo_4_turn_1(){for (pos = 90; pos <= 150; pos += 1) {myservo_4.write(pos);delay(15);}}
void servo_4_turn_2(){for (pos = 150; pos >= 35; pos -= 1) {myservo_4.write(pos);delay(15);}}
void servo_4_turn_3(){for (pos = 35; pos <= 90; pos += 1) {myservo_4.write(pos);delay(15);}}

void switchPressed()
{
  Serial.println("Switch pressed test");
  if (servovar == 1){servovar=servovar+1;Serial.println("Dummy turn\n");}
  else if(servovar == 3){servo_3_turn_1();servovar = servovar + 1;Serial.println("Turn one\n");}
  else if (servovar == 5){servo_2_turn_1();servovar = servovar + 1;Serial.println("Turn two\n");}
  else if (servovar == 7){servo_3_turn_2();servovar = servovar + 1;Serial.println("Turn three\n");
  else if (servovar == 9){servo_2_turn_2();servovar = servovar + 1;Serial.println("Turn four\n");}
  else if (servovar == 11){servo_2_turn_3();servovar = servovar + 1;Serial.println("Turn five\n");}
  else if (servovar == 13){servo_4_turn_1();servovar = servovar + 1;Serial.println("Turn six\n");}
  else if (servovar == 15){servo_4_turn_2();servovar = servovar + 1;Serial.println("Turn seven\n");}
  else if (servovar == 17){servo_4_turn_3();servovar = servovar + 1;Serial.println("Turn eight\n");}
  else if (servovar == 19){servo_1_turn_2();servovar = servovar + 1;Serial.println("Turn nine\n");}
  else if (servovar == 21){servo_1_turn_3();servovar = servovar + 1;Serial.println("Turn ten\n");servovar = 0;}
}

void setup()
{
  myservo_1.attach(9);
  myservo_2.attach(10);
  myservo_3.attach(11);
  myservo_4.attach(12);

  pinMode(4, INPUT);
  digitalWrite(4, LOW);
  servo_1_turn_1();
  Serial.begin(9600);
}

void loop() 
{
  lastDebounceTime = millis();
  if(digitalRead(4) == HIGH && WasHigh == false)
  {
    servovar=servovar+1;
    switchPressed();
    Serial.print(servovar);
    Serial.print("\n");
    delay(debounceDelay);
    WasHigh = true;
  }
  if(digitalRead(4) == LOW && WasHigh == true)
  {
    delay(debounceDelay);
    WasHigh = false;
  }  
}

This is my original code before I tried adding interrupts. Basically, inputs on pin 4 cause the program to work through a sequence of servo movements, 10 in total, which, once it has finished it last one, will set the variable back to 0 so that it can start again. The issue I am having though is sometimes I want to be able to set it back to the start mid operation. So I was thinking I could use an interrupt so that anytime pin 7 (believe this is an interrupt pin on a Leonardo board) went high, it would set “servovar” back to 0 so the process could start over. I tried adding an interrupt but it doesn’t seem to be working, this is my adjusted code with the interrupt.

#include <Servo.h> //asasdasd

Servo myservo_1;  
Servo myservo_2; 
Servo myservo_3;  
Servo myservo_4;  

int pos = 0;    
int servovar = 0; 
int last_Button_State = LOW;
int current_Button_State = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool WasHigh = false;
bool WasHigh_1 = false;

void servo_1_turn_1(){for (pos = 90; pos >= 35; pos -= 1) {myservo_1.write(pos);delay(15);}} 
void servo_1_turn_2(){for (pos = 35; pos <= 150; pos += 1) {myservo_1.write(pos);delay(15);}}
void servo_1_turn_3(){for (pos = 150; pos >= 35; pos -= 1) {myservo_1.write(pos);delay(15);}} 
void servo_2_turn_1(){for (pos = 90; pos >= 35; pos -= 1) {myservo_2.write(pos);delay(15);}} 
void servo_2_turn_2(){for (pos = 35; pos <= 150; pos += 1) {myservo_2.write(pos);delay(15);}} 
void servo_2_turn_3(){for (pos = 150; pos >= 90; pos -= 1) {myservo_2.write(pos);delay(15);}} 
void servo_3_turn_1(){for (pos = 90; pos <= 150; pos += 1) {myservo_3.write(pos);delay(15);}} 
void servo_3_turn_2(){for (pos = 150; pos >= 90; pos -= 1) {myservo_3.write(pos);delay(15);}}
void servo_4_turn_1(){for (pos = 90; pos <= 150; pos += 1) {myservo_4.write(pos);delay(15);}} 
void servo_4_turn_2(){for (pos = 150; pos >= 35; pos -= 1) {myservo_4.write(pos);delay(15);}}
void servo_4_turn_3(){for (pos = 35; pos <= 90; pos += 1) {myservo_4.write(pos);delay(15);}} 

void switchPressed()
{
  Serial.println("Switch pressed test");
  if (servovar == 1){servovar=servovar+1;Serial.println("Dummy turn\n");}
  else if(servovar == 3){servo_3_turn_1();servovar = servovar + 1;Serial.println("Turn one\n");} 
  else if (servovar == 5){servo_2_turn_1();servovar = servovar + 1;Serial.println("Turn two\n");} 
  else if (servovar == 7){servo_3_turn_2();servovar = servovar + 1;Serial.println("Turn three\n");}
  else if (servovar == 9){servo_2_turn_2();servovar = servovar + 1;Serial.println("Turn four\n");} 
  else if (servovar == 11){servo_2_turn_3();servovar = servovar + 1;Serial.println("Turn five\n");}
  else if (servovar == 13){servo_4_turn_1();servovar = servovar + 1;Serial.println("Turn six\n");} 
  else if (servovar == 15){servo_4_turn_2();servovar = servovar + 1;Serial.println("Turn seven\n");} 
  else if (servovar == 17){servo_4_turn_3();servovar = servovar + 1;Serial.println("Turn eight\n");} 
  else if (servovar == 19){servo_1_turn_2();servovar = servovar + 1;Serial.println("Turn nine\n");} 
  else if (servovar == 21){servo_1_turn_3();servovar = servovar + 1;Serial.println("Turn ten\n");servovar = 0;} 
}

void softReset()
{
  if(digitalRead(7) == HIGH && WasHigh_1 == false)
  {
    servovar = 0;
    Serial.print("test\n");
    delay(debounceDelay);
    WasHigh_1 = true;
  }
  if(digitalRead(7) == LOW && WasHigh_1 == true)
  {
    delay(debounceDelay);
    WasHigh_1 = false;
  }
  
}

void setup()
{
  myservo_1.attach(9);
  myservo_2.attach(10);
  myservo_3.attach(11);
  myservo_4.attach(12);

  pinMode(4, INPUT);
  digitalWrite(4, LOW);
  pinMode(7, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(7), softReset, CHANGE);
  servo_1_turn_1();
  Serial.begin(9600);
}

void loop() 
{
  lastDebounceTime = millis();
  if(digitalRead(4) == HIGH && WasHigh == false)
  {
    servovar=servovar+1;
    switchPressed();
    Serial.print(servovar);
    Serial.print("\n");
    delay(debounceDelay);
    WasHigh = true;
  }
  if(digitalRead(4) == LOW && WasHigh == true)
  {
    delay(debounceDelay);
    WasHigh = false;
  }  
}

Here I added the interrupts for pin 7, and a function it could jump to to set servovar to 0, but it just seems to ignore the function entirely, any mistakes I have made with the interrupt?

Cheers for the help guys.

Why do you think that you need an interrupt? You can simply do the reset in loop/), where you check (and debounce) the signal on digital pin 7, just like you already do for pin 4.

Ifyouwriteeverythingononelinethenit'sverydifficulttodebug.

You should not be using interrupts or delay(). Open the Blink-without-delay example sketch and work out what it is doing. Then google for "arduino several things at one time".

DrDiettrich:
Why do you think that you need an interrupt? You can simply do the reset in loop/), where you check (and debounce) the signal on digital pin 7, just like you already do for pin 4.

Not sure why, call it an assumption on my behalf.

#include <Servo.h> //asasdasd

Servo myservo_1;  
Servo myservo_2; 
Servo myservo_3;  
Servo myservo_4;  

int pos = 0;    
int servovar = 0; 
int last_Button_State = LOW;
int current_Button_State = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool WasHigh = false;
bool WasHigh_1 = false;

void servo_1_turn_1(){for (pos = 90; pos >= 35; pos -= 1) {myservo_1.write(pos);delay(15);}} 
void servo_1_turn_2(){for (pos = 35; pos <= 150; pos += 1) {myservo_1.write(pos);delay(15);}}
void servo_1_turn_3(){for (pos = 150; pos >= 35; pos -= 1) {myservo_1.write(pos);delay(15);}} 
void servo_2_turn_1(){for (pos = 90; pos >= 35; pos -= 1) {myservo_2.write(pos);delay(15);}} 
void servo_2_turn_2(){for (pos = 35; pos <= 150; pos += 1) {myservo_2.write(pos);delay(15);}} 
void servo_2_turn_3(){for (pos = 150; pos >= 90; pos -= 1) {myservo_2.write(pos);delay(15);}} 
void servo_3_turn_1(){for (pos = 90; pos <= 150; pos += 1) {myservo_3.write(pos);delay(15);}} 
void servo_3_turn_2(){for (pos = 150; pos >= 90; pos -= 1) {myservo_3.write(pos);delay(15);}}
void servo_4_turn_1(){for (pos = 90; pos <= 150; pos += 1) {myservo_4.write(pos);delay(15);}} 
void servo_4_turn_2(){for (pos = 150; pos >= 35; pos -= 1) {myservo_4.write(pos);delay(15);}}
void servo_4_turn_3(){for (pos = 35; pos <= 90; pos += 1) {myservo_4.write(pos);delay(15);}} 

void switchPressed()
{
  Serial.println("Switch pressed test");
  if (servovar == 1){servovar=servovar+1;Serial.println("Dummy turn\n");}
  else if(servovar == 3){servo_3_turn_1();servovar = servovar + 1;Serial.println("Turn one\n");} 
  else if (servovar == 5){servo_2_turn_1();servovar = servovar + 1;Serial.println("Turn two\n");} 
  else if (servovar == 7){servo_3_turn_2();servovar = servovar + 1;Serial.println("Turn three\n");}
  else if (servovar == 9){servo_2_turn_2();servovar = servovar + 1;Serial.println("Turn four\n");} 
  else if (servovar == 11){servo_2_turn_3();servovar = servovar + 1;Serial.println("Turn five\n");}
  else if (servovar == 13){servo_4_turn_1();servovar = servovar + 1;Serial.println("Turn six\n");} 
  else if (servovar == 15){servo_4_turn_2();servovar = servovar + 1;Serial.println("Turn seven\n");} 
  else if (servovar == 17){servo_4_turn_3();servovar = servovar + 1;Serial.println("Turn eight\n");} 
  else if (servovar == 19){servo_1_turn_2();servovar = servovar + 1;Serial.println("Turn nine\n");} 
  else if (servovar == 21){servo_1_turn_3();servovar = servovar + 1;Serial.println("Turn ten\n");servovar = 0;} 
}

void setup()
{
  myservo_1.attach(9);
  myservo_2.attach(10);
  myservo_3.attach(11);
  myservo_4.attach(12);

  pinMode(4, INPUT);
  digitalWrite(4, LOW);
  pinMode(7, INPUT);
  digitalWrite(7, LOW);
  servo_1_turn_1();
  Serial.begin(9600);
}

void loop() 
{
  lastDebounceTime = millis();
  if(digitalRead(4) == HIGH && WasHigh == false)
  {
    servovar=servovar+1;
    switchPressed();
    Serial.print(servovar);
    Serial.print("\n");
    delay(debounceDelay);
    WasHigh = true;
  }
  if(digitalRead(4) == LOW && WasHigh == true)
  {
    delay(debounceDelay);
    WasHigh = false;
  } 
  if(digitalRead(7) == HIGH && WasHigh_1 == false)
  {
    servovar = 0;
    Serial.print("test\n");
    delay(debounceDelay);
    WasHigh_1 = true;
  }
  if(digitalRead(7) == LOW && WasHigh_1 == true)
  {
    delay(debounceDelay);
    WasHigh_1 = false;
  }
}

Is that what you were suggesting? Have changed the pin 7 to a regular input in the start up and added the changes to the loop.

You still have delays. This code spends seconds-to-minutes in delays and just a few nanoseconds looking at the pins to see if they’re currently pressed.

MorganS: You still have delays. This code spends seconds-to-minutes in delays and just a few nanoseconds looking at the pins to see if they're currently pressed.

Is that just a case of bad programming or will that actively cause an issue? Also, is it the delays in the servo movements, the ones for the switch de-bouncing, or is it both?

Use the AutoFormat tool to lay your code out so it is easy to read and post the revised version in your next Reply and I will look at it.

...R

"Not sure why, call it an assumption on my behalf." "but am struggling to understand them"

Well then heed the advice and DON'T.

chickwolf: Is that just a case of bad programming or will that actively cause an issue? Also, is it the delays in the servo movements, the ones for the switch de-bouncing, or is it both?

All of them. Every time you move a servo from 35 to 150 at 1-degree steps and delay 15 ms at each step, this routine takes 1725 ms to complete. That's 1725 ms you can not respond to anything else, or move another servo (that's probably where your idea of interrupts came from - it will work but it's not the best solution).

Check out the blink-without-delay example on how to to use millis() so you can move all servos and respond to buttons at the same time without using interrupts.

Lots and lots of layout improvements can be made to increase readability.

Instead of a long list of if .. else if .. you may use switch .. case statements.

Instead of servovar = servovar + 1 use servofar += 1 or even servovar++.

Add comments. The more the better. You'll love yourself for it when you pick up the project again after a few weeks (or months, or even years).

Where possible, use more descriptive names for your variables and objects than "myservo1", call it "leftServo" or "rightLegServo" or so - makes it much easier to remember what does what.

The demo Several Things at a Time is an extended example of BWoD. It includes the movement of a servo and may help with understanding the technique.

...R

DrDiettrich: Why do you think that you need an interrupt? You can simply do the reset in loop/), where you check (and debounce) the signal on digital pin 7, just like you already do for pin 4.

After a bit of tinkering around with what you suggested I managed to get it working, cheers for the help!