``volatile int enA=10;
volatile int in1=9;
volatile int in2=8;
In the above code I'm trying to rotate a motor in one direction and whenever i have an interrupt on pin 2 (INT0) I'm trying to reduce its speed. The problem I'm facing is if i do not include that delay in the main loop the interrupt doesn't work, whereas, whenever I include the delay in the main loop the isr works only for the delay specified in the main loop (in this case 1000ms). Can anyone please let me know how do i overcome this problem? Also i want the motor to resume it's speed when i have an interrupt again on the same pin i.e pin2.
Please let me know about a solution for this. I'm driving the motor using an L298N driver with a supply of 5V 1A current. the motor used is a DC geared motor.
Without the delay the interrupt works just fine, but the code in loop() that keeps getting repeated over and over a few hundred times a millisecond keeps undoing what you have tried to do in the ISR.
If you want the motor to slow on one interrupt and then speed back up on the next then the interrupt needs to set a flag that lets the program know to go slow or fast and then in the loop look at that flag and do your analogWrite accordingly.
Since your pin numbers won't change you don't need to declare them 'volatile'. That is to warn the compiler that the value can change at any time so it doesn't optimize away repeated access. You SHOULD declare them 'const' so an accidental write to those variables won't change your pin numbers.
This is a classic trap for "newbies".
Why are you imagining you need to use an "interrupt"?
You most certainly do not.
And as always, the "XY Problem".
Just explain carefully what you actually need to do - what is the motor doing, what is the control input that you want to influence its operation, exactly how do you want this control to work and so on.
Then we can advise on proper code (which as always, means, you do not use "delay()") to perform the task - and permit you do do other processing as well.
Also, please read the instructions on posting here and go back and fix your first post.