 # Using 3 prox sensors to monitor two Chain that needs to stay at equal length.

PaulS: So, post some pictures showing the chains/sprockets/sensors, so we can get some idea whether you are trying to measure the right thing.

I have attached the picture above.

PaulS: Can you measure the time between now and when I got up this morning? Or do you need to calculate that?

you could measure that with a stop watch

The pic shows a shiny new looking section. Is there an existing problem with chain stretch or are you being proactive? I ask because since the chains are always in lockstep, assuming both are lubricated equally, they'd stretch together.

PaulS: I'm afraid the pictures don't help a lot. The drawing shows two sensors "looking at green bolts". Your post talks about 3 sensors, two of check are counting 47 pins.

It isn't clear how knowing the interval between pins is going to tell you anything about chain stretch.

My question is how to record time intervals 47 times using 2 prox sensors and also to measure the time interval between a flag using another prox sensor. I know how to use these timings to determine chain stretch. if I can get help with the timings then my question is answered.

dougp: The pic shows a shiny new looking section. Is there an existing problem with chain stretch or are you being proactive? I ask because since the chains are always in lockstep, assuming both are lubricated equally, they'd stretch together.

That's why i'm working on this because they don't get lubricated equally sometimes and I want to know when one is stretching than the other.

dohdesmond: My question is how to record time intervals 47 times using 2 prox sensors...

The most direct way, IMO, would be to start a timing interval (counting millis/micros) when a sensor *goes *true/false. When it *goes *true/false again add the accumulated value to the total, reset same and start again. After 47 intervals store the total.

dohdesmond:
My question is how to record time intervals 47 times using 2 prox sensors and also to measure the time interval between a flag using another prox sensor. I know how to use these timings to determine chain stretch. if I can get help with the timings then my question is answered.

dougp:
The most direct way, IMO, would be to start a timing interval (counting millis/micros) when a sensor *goes * true/false. When it *goes * true/false again add the accumulated value to the total, reset same and start again. After 47 intervals store the total.

I would see it more like a rotary encoder solution… ie in the case of a rotary encoder 2 sensors detect a pulse train that are 90deg out of phase which helps determine direction of rotation.

in your case, if I understood correctly you want ensure the the sensors trigger within a certain tolerance of each other. IMHO I do not think to wait 47 intervals (but you can record them if you with); all to need to do is to measure the delta between the interrupt triggers

ie Sensor A triggered → store micros() and calc delta time Sensor B was last triggered
Sensor B triggered → store micros() and calc delta time Sensor A was last triggered

then you can loads of stuff like storing 47 intervals, calculating average delta etc, etc

if delta > tolerance then one is stretching more than the other

sherzaad: I would see it more like a rotary encoder solution... ie in the case of a rotary encoder 2 sensors detect a pulse train that are 90deg out of phase which helps determine direction of rotation.

in your case, if I understood correctly you want ensure the the sensors trigger within a certain tolerance of each other. IMHO I do not think to wait 47 intervals (but you can record them if you with); all to need to do is to measure the delta between the interrupt triggers

ie Sensor A triggered -> store micros() and calc delta time Sensor B was last triggered Sensor B triggered -> store micros() and calc delta time Sensor A was last triggered

then you can loads of stuff like storing 47 intervals, calculating average delta etc, etc

if delta > tolerance then one is stretching more than the other

That's what I had in mind but my problem is putting that in code. is there any example code that I can follow? I know how to do the analysis if I am able to obtain the data for the 47 intervals and store it and also being able to make the program to restart counting the intervals over and over again every time it sees the zero flag.

dougp: The most direct way, IMO, would be to start a timing interval (counting millis/micros) when a sensor *goes *true/false. When it *goes *true/false again add the accumulated value to the total, reset same and start again. After 47 intervals store the total.

Do you have any example code I can follow?

dohdesmond: That's what I had in mind but my problem is putting that in code. is there any example code that I can follow? I know how to do the analysis if I am able to obtain the data for the 47 intervals and store it and also being able to make the program to restart counting the intervals over and over again every time it sees the zero flag.

maybe something like this using interrupt-on-change (compiles but not tested!) :

``````//ISR (PCINT0_vect) pin change interrupt for D8 to D13
//ISR (PCINT1_vect) pin change interrupt for A0 to A5
//ISR (PCINT2_vect) pin change interrupt for D0 to D7

const uint8_t arr_size = 47;
uint8_t resetFlag = 8; //flag prox pin
uint8_t sensorA = 9;   //prox A pin
uint8_t sensorB = 10;  //prox B pin
uint8_t A = 0, B = 0;
unsigned long timesA;
unsigned long timesB;
unsigned long oldtime;
volatile uint8_t interval_cnt = 0;
volatile unsigned long intervals[arr_size];

ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
{
oldtime = micros();

//all actions executed when any of the sensors are HIGH
if (digitalRead(resetFlag) == HIGH) { //reset sensor triggered. reset interval counter
interval_cnt = 0;
}
else if (digitalRead(sensorA) == HIGH) { //SensorA triggered
if (B == 1) { //SensorB previously triggered
intervals[interval_cnt] = oldtime - timesB; //record interval
interval_cnt = (interval_cnt + 1) % arr_size; //increment interval counter
B = 0; //reset SensorB flag
}
timesA = oldtime; //save current time
A = 1; //set Sensor A flag
}
else if (digitalRead(sensorB) == HIGH) { //SensorB triggered
if (A == 1) { //SensorA previously triggered
intervals[interval_cnt] = oldtime - timesA;
interval_cnt = (interval_cnt + 1) % arr_size;
A = 0; //reset SensorA flag
}
timesB = oldtime;
B = 1; //reset SensorB flag
}
}

void pciSetup(byte pin)
{
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin));  // enable pin
PCIFR  |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR  |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}

void setup()
{
Serial.begin(115200);

pinMode(resetFlag, INPUT);
pinMode(sensorA, INPUT);
pinMode(sensorB, INPUT);

// enable interrupt-on-change for pin...
pciSetup(resetFlag);
pciSetup(sensorA);
pciSetup(sensorB);
}

void loop()
{
Serial.print("Delta Time(us): ");
Serial.println(intervals[interval_cnt]);
}
``````
``````[quote author=sherzaad link=msg=3754241 date=1527786866]
maybe something like this using interrupt-on-change (compiles but not tested!) :

[code
``````

[/quote] Thank you. I would test this and give you feedback