#define IR_PIN 2 // Definisci il pin collegato al ricevitore IR
#define MAX_SAMPLES 255 // Numero massimo di campioni
volatile uint16_t timings[MAX_SAMPLES]; // Array per memorizzare i tempi
volatile uint8_t index = 0; // Indice per il prossimo elemento nell'array
volatile uint8_t capturing = 0; // Flag per indicare se la cattura è in corso
unsigned long overflowCount=0;
// Funzione ISR per gestire i cambiamenti di stato
ISR(PCINT2_vect) {
static uint32_t lastTime = 0; // Tempo dell'ultimo cambio di stato
uint32_t currentTime = micros(); // Ottieni il tempo corrente in microsecondi
if (capturing) {
// Calcola la differenza di tempo dall'ultimo cambio di stato
uint16_t duration = (uint16_t)(currentTime - lastTime);
// Verifica che non ci sia overflow
if (index < MAX_SAMPLES) {
timings[index++] = duration; // Memorizza la durata nell'array
} else {
capturing = 0; // Ferma la cattura se l'array è pieno
}
} else {
capturing = 1; // Inizia la cattura al primo cambiamento
}
lastTime = currentTime; // Aggiorna il tempo dell'ultimo cambiamento
}
ISR(TIMER1_OVF_vect) {
overflowCount++; // Increment overflow counter
}
unsigned long myMicros() {
unsigned long tmpOverflow = overflowCount;
unsigned long t;
uint8_t oldSREG = SREG; // Save the status register
cli(); // Disable interrupts
t = TCNT1; // Get current timer value
// Check if overflow occurred after reading TCNT1
if ((TIFR1 & (1 << TOV1)) && (t < 65535)) {
tmpOverflow++;
}
SREG = oldSREG; // Restore the status register (reenable interrupts)
// Return the time in microseconds
return ((tmpOverflow << 16) + t)/2;
}
void setup() {
Serial.begin(115200); // Inizializza la seriale a 115200 baud
// Set Timer 1 to normal mode
TCCR1A = 0; // Normal mode
TCCR1B = 0; // Reset Timer 1 registers
// Set the prescaler to 64
TCCR1B |= (1 << CS11);
// Enable Timer 1 overflow interrupt
TIMSK1 |= (1 << TOIE1);
// Utilizza la macro per impostare il pin IR come input
DDRD &= ~(1 << IR_PIN);
// Configura l'interrupt sul pin IR
PCICR |= (1 << PCIE2); // Abilita gli interrupt sul PCINT2
PCMSK2 |= (1 << IR_PIN); // Abilita gli interrupt per il pin IR_PIN
}
void loop() {
if (!capturing && index > 0) {
// Se la cattura è terminata e ci sono dati disponibili, stampa i risultati
Serial.println("Captured timings (in microseconds):");
for (uint8_t i = 0; i < index; i++) {
Serial.println(timings[i]);
}
index = 0; // Resetta l'indice per la prossima cattura
capturing = 0; // Resetta il flag di cattura
}
}
Welcome to the forum
Are you aiming to optimize speed, memory usage, portability, something else ?
Define 'optimize'.
You're not asking to have code that looks like it might have been generated by ChatGPT or one of its cousins to be altered to look less like it was written by an AI, are you?
@0xbera
Before starting optimize the code - did you really tested it? Is it worked as intended?
Yes I did.
Make it better / faster.
Hi! This is my first time using Arduino, this code is not 100% made by ChatGPT. I used AI to get an idea but I tested it and changed something.
E.G Checked for overflow in ISR
Hi! I want to optimize speed and memory usage. Also I'm afraid of overflow and I'm wondering if micros() usage for a long period could be a problem.
Still not precise enough
How much faster do you need it ?
Which Arduino board have you run it on ?
Have you asked ChatGPT thow to make it faster ?
You can remove myMicros()
function since it is not used anywhere in the code.
You can only optimize for one of several variables, speed, ram, create speed, and maintenance cost. As far as overflow, that is trivial so if it bothers you I think I know who really wrote the code, it starts with Chat.
By using experience and thinking.
Your first time using Arduino? And you went straight for ChatGPT? Good luck, and good-bye.
Does AI often swap between English and Italian in the comments?
I'd better not tell my wife: she'll replace me with a robot by next week!
NOTE
Comments may have been added or changed by @0xbera to clarify the code
A further confusion is that he/she does not appear to be located in Italy although that does not preclude him/her from using Italian comments
Optimization-wise, it looks pretty good.
The worst thing is probably the use of the Arduino micros() function in the ISR.
myMicros() is theoretically slightly better, but you're not currently using it, and I'm not convinced that the timer1 code is what you want. (It SAYS it's using the /64 prescaler, but the myMicros() code looks like it's expecting the /8 prescaler. (sadly, /16 isn't one of the choices.))
Sure, if you expect intervals longer than ~4000 seconds. Can't fix that expect by being careful, or making the code less efficient (64bit math!)
I'm currently in Spain for vacation! Sorry P:
No need to be sorry. Enjoy your holiday, which at least explains your Spanish IP address
Thank you!