extrnal interrupt handling issue

mayuri_creintors:
hi...sterretje

Can you send me an example code for how to use hardware interrputs???

Not until you explain why you are using an external interrupt, and what you expect the interrupt handler to do.

Your question is like "Can you write me some code. I don't know what it should do, do I'll test it and let you know whether it works, or not.".

ok.. let me explain you what i need. i am programming for a special purpose machine which has 4 pneumatic cylinders , 1 encoder,1 dc motor and 2 proximity sensors. I need to program to forward and reverse strokes for a particular mechanical operation. In a cycle an instance occurs where i need to cut a wire for specific length . mechanically an encoder is coupled with a dc motor. when the motor starts encoder starts giving output. We have calibrated like( x millimeters of wire= y pulses) .To count the encoder pulses i need an interrupt handler where i am counting the falling edges. In variable "Tmp" i am storing calibrated pulses and counting the encoder pulses in variable" Value". What I am expecting is when encoder pulses and calibrated pulses matches , motor should stop. in case an encoder fails to count, we are providing a proximity sensor to since the wire and stop the motor . This is what the project is

To count the encoder pulses i need an interrupt handler where i am counting the falling edges.

That makes sense.

In variable "Tmp" i am storing calibrated pulses

That doesn't. What is a "calibrated pulse"? Why isn't the variable named something meaningful, like fredFlinstone?

What I am expecting is when encoder pulses and calibrated pulses matches

So, on every pass through loop(), check whether the counted pulses matches the required number of pulses. If it does, stop the motor.

The only external interrupt is generated by the encoder, and the handler simply needs to increment a variable.

yes Paul, the same i am doing…but comparison not happening between Value and Tmp… just value is going on increasing as i rotate a rotary encoder

I would just start over and make a real sketch. Drop the stupid fun.h and make a proper .ino. Make a proper library if you want to but don’t include 99% of the program in another file.

Drop all the while loops you have.

Start using proper variable names! A proper variable name is (in most cases) not just 3 characters. So PC1, Tmp, PX1 are all bad names. And so is “value”, value of what? Same for Dev_length etc.

Fixing something in a messy shop where all the tools are just scattered true out the whole shop is a very hard job. So a good start here is to get yourself a clean shop first and make some code that actually makes sens.

I make a start for you:

/**********Settings*******************/
const unsigned int wireLength = ; //In pulses of encoder

/**********Pin definitions************/
const byte EncoderPin = ;//whichever pin you use for the encoder
const byte ProximityPin = ;//whatever


/**********Global varibales***********/
volatile unsigned int encoderPulses = 0;  //Holds the pulses from the encoder
bool motorIsRunning = true; //Is the motor running?

//Increment received pulses
void encoderInterrupt(){
  encoderPulses++;
}

void setup(){
  //Setup encoder
  pinMode(EncoderPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(EncoderPin), encoderInterrupt, FALLING) ;
  
  //setup proximity sensor
  pinMode(ProximityPin, INOUT_PULLUP); //or whatever you want
}

void loop(){
  if(motorIsRunning){
    checkWireLength();
  }
}

void checkWireLength(){
  //check is wire is desired length or proximity sensor is tripped
  if(encoderPulses >= wireLength || !digitalRead(ProximityPin)){
    stopMotor();
  }
}

void stopMotor(){
  motorIsRunning = false;
}

Thanks Sir,
I will work on it.. Thanks for all valuable inputs... even my code i working now.. m happy.. Thanku all........................