Hi everyone. I connected an "Infrared Optical Optocoupler Module" sensor with a perforated disk to a stepper. I created this code to calculate the speed of the stepper motor by reading the number of times the IR signal passes through the slots of the encoder wheel. I used "attachinterrupt" and it works pretty well. specifically I give a constant speed to the stepper motor and then separately use this code to count the speed:
now I would instead need to create an array where to insert a number of samples in micros() to obtain the number of Rising (and possibly also Falling) recorded by the sensor, and catalog them with the delta-T between an ascending and falling. so as to calculate the variation of the delta-T between each variation of the signal and see how long it takes the engine from when it is stopped to when it reaches the reference speed. Do you have any suggestions on how I can modify this code?
because the engine is too fast to reach speed, also it is for an experiment on the sensor requested by my professor. he asked me to measure the number of delta T which varies until it is almost constant at steady state. I was wondering if anyone can do this using AttachInterrupt.
the point is not how long the rising lasts, but the time distance between one rising and another recorded by the IR sensor. as input I actually have a cycle that every second commands the stepper motor to speed 0 and after one second speed 200. in the transition from 0 to 200 I want to see when the number of steps settles to normal. then calculating in number of steps.
schematically the signal variation in the IR would be like this as long as the recorded rising must be constant over time (in terms of pulses per second).
sorry, i misunderstood what you meant by delta-T. seems that you just want to measure the time between events.
no doubt you can count the # of events. and from each event (after the first) you can calculate the speed -- rpm = 60 / (t * N), where N is the # of events per rotation
but it sounds you don't just need to the # of events until it reaches the target speed, but need to monitor the speed to determine when it is stable, presumably varies by < some thresh
yes, I would also later like to understand how to adjust the acceleration to be given to the engine to go from zero to a speed value of 200 more or less quickly.
I was also wondering if this draft code is correct:
// Dichiarazione delle variabili
volatile unsigned long lastChangeTime = 0;
volatile unsigned long changeDuration = 0;
volatile bool isFirstChange = true;
// Definizione della funzione di interrupt
void onChange() {
unsigned long currentTime = micros();
if (!isFirstChange) {
changeDuration = currentTime - lastChangeTime;
}
lastChangeTime = currentTime;
isFirstChange = false;
}
void setup() {
pinMode(22, INPUT);
attachInterrupt(digitalPinToInterrupt(22), onChange, CHANGE);
Serial.begin(115200); // Inizializzazione della comunicazione seriale
}
void loop() {
// Se è stato registrato un nuovo cambiamento, stampa la durata del precedente intervallo
if (changeDuration > 0) {
Serial.print("Change duration: ");
Serial.print(changeDuration);
Serial.println(" microseconds");
changeDuration = 0; // Resetta la durata del cambiamento
}
// Altre operazioni nel loop se necessario
}
the problem is that I tried to do two tests going from 0 to 400 and from 0 to 4000 as speed, the delta ts that I record seem identical, it is almost useless. advice?