interruption

hello there
I am new to arduino and am working on w project in which i program a servo motor to turn from 125 degree to 175 in slow motion
using a an interruption by a pushbutton , i added a sweep loop
the motor work the way it has to be but when i push the button it stops for a moment than it works again , not doing the sweep button
i added serialprint in the sweet loop to check if it enters the loop and it does but the motor doesn’t turn in the way the interruption comand it to
please help

Hi mariem

Please post your code (use the code tags - the "#" button above the smileys).

And could you post a diagram of how your hardware is connected.

Thanks

Ray

well this is the code am trying now

#include <Servo.h> 
 
Servo myservo;  
 
int pos = 175;     
 
void setup() 
{ 
  myservo.attach(9); 
  attachInterrupt(0, traite_int, RISING);
 Serial.begin(9600); 
} 
 
void loop() 
{ 
  for(pos = 175; pos>=125; pos-=1)      
  {                                
    myservo.write(pos); 
pos=myservo.read();  
attachInterrupt(0, traite_int, RISING);
    delay(100);                        
  } 
} 
 void traite_int()
  {
    int i=1;
    int pos1;
    
while(i<=5)
 {Serial.println("salma"); 

    for(pos1=175; pos1>=125; pos1 -=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos1); 
   pos1=myservo.read(); 
   
Serial.println(pos1);    // tell servo to go to position in variable 'pos' 
    delay(500);                       // waits 15ms for the servo to reach the position 
  }   
 pos1=myservo.read();  
 for(pos1=125 ; pos1 <= 175; pos1 += 1) // goes from 0 degrees to 180 degrees 
  {        
    // in steps of 1 degree 
    myservo.write(pos1);   
    pos1=myservo.read();
Serial.println(pos1);     // tell servo to go to position in variable 'pos' 
    delay(500);                       // waits 15ms for the servo to reach the position 
  } 
 i++ ;
}  
 }

Thanks, Mariem

attachInterrupt(0, traite_int, RISING);

You have specified traite_int as an Interrupt Service Routine (ISR). Then you define traite_int as …

void traite_int()
{
    int i=1;
    int pos1;

    while(i<=5)
    {
        Serial.println("salma"); 

        for(pos1=175; pos1>=125; pos1 -=1)     // goes from 180 degrees to 0 degrees 
        {                                
            myservo.write(pos1); 
            pos1=myservo.read(); 

            Serial.println(pos1);    // tell servo to go to position in variable 'pos' 
            delay(500);                       // waits 15ms for the servo to reach the position 
        }   
        pos1=myservo.read();  
        for(pos1=125 ; pos1 <= 175; pos1 += 1) // goes from 0 degrees to 180 degrees 
        {        
            // in steps of 1 degree 
            myservo.write(pos1);   
            pos1=myservo.read();
            Serial.println(pos1);     // tell servo to go to position in variable 'pos' 
            delay(500);                       // waits 15ms for the servo to reach the position 
        } 
        i++ ;
    }  
}

That is MUCH too much code to have in an ISR. ISRs should be as short as possible and return control quickly to loop(). Typically they just update a variable or set a boolean flag which is then checked and acted on in loop().

Also, delay() and Serial.println in an ISR are problems.

It is quite possible that you do not need an interrupt to achieve what you want. Can you explain in words what you want to happen when you press the button?

Regards

Ray

i simply want the motor to go from 125 to 175 and then from 175 to 125 when i push the button, and it has to be done 5 times :/ at least 5 times i know it is way too long but ...

There is no need to use interrupts to do what you want. Interrupts are only necessary when something very brief (microseconds or less) has to be detected.

The program in the first post in this Thread illustrates how various things can happen without needing to use interrupts. It might provide a template for your project.

...R

i simply want the motor to go from 125 to 175 and then from 175 to 125 when i push the button, and it has to be done 5 times :/ at least 5 times

OK, the servo should sweep backwards and forwards when the switch is pressed? What do you want to happen before the switch is pressed?

And if the user releases the switch before the 5 sweeps have finished, what should happen? Should the servo stop where it is, or carry on and finish the 5 sweeps?

thanks the thing is , that am using easyvr shield for speech regonition , and when it is loud , the module can not detect the voice , so am usng pushbuttons as back us system so i have 3 pushbuttons, one closes the mechanical hand , the other opens the mechanical hand and the third does that sweepthe first 2 interrupts worked but that third one is driving me crazy :~ :~ :~ i don't get why would it accept the first 2 and not the third by the way ,i tried to include the pushbuttons in so many ways and failed the interruption was the best solution since 2 worked and my professor said i should use it :/ from what described above , do you think thread works ? and why the first 2 intterupts worked ? any thoughts ?

Hackscribble:

i simply want the motor to go from 125 to 175 and then from 175 to 125 when i push the button, and it has to be done 5 times :/ at least 5 times

OK, the servo should sweep backwards and forwards when the switch is pressed? What do you want to happen before the switch is pressed?

And if the user releases the switch before the 5 sweeps have finished, what should happen? Should the servo stop where it is, or carry on and finish the 5 sweeps?

i want it to sweep when i press the button and when i release it finishs the 5 sweeps and then stops but when i press it stops for few seconds and work again -_-

i want it to sweep when i press the button and when i release it finishs the 5 sweeps and then stops

From what you describe, and as Robin2 said, there is no need for interrupts. The example program that Robin2 linked to sounds very much the way to go.

Remember that loop() repeats constantly very quickly. And that if you are trying to do something that is very slow in comparison (like moving a servo), a good way is to forget about "for" loops and instead write some code that moves the servo a little bit each time round loop(), and stops when the servo reaches the required position. Probably easiest to think about how this would work for "open hand" or "close hand" and then extend to the more complicated case of "sweeping open and close 5 times".

from what described above , do you think thread works ?

Do you mean as in multi-threading? Not relevant and not possible (I think) on the Arduino.

okay okay :) thanks alot for both of you