Problemi con funzione Tone

Ciao a tutti, ho un problema con un progetto.

Sto costruendo un tester per iniettori da automobile e utilizzo Arduino Uno come driver. Sto utilizzando la funzione Tone per generare un onda quadra con la quale pilotare gli iniettori. La parte hardware funziona egregiamente ma sto riscontrando problemi con la modulazione dell'onda.

La funzione tone vedo che è strutturata " tone (pin, frequenza in hertz, durata) ".

Ho una variabile (freq) che gestisce la frequenza, il problema è che all'aumentare della variabile la frequenza sugli iniettori non risponde come dovrebbe.

Faccio un esempio, quando il valore è uguale a 6, gli iniettori "girano" più velocemente di quando è a 8.

Se qualcuno riuscisse a spiegarmi dove o cosa sto sbagliando mi farebbe un favore immenso!

Vi allego il codice, scusate se è disordinato :sweat_smile:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 6, 4, 3, 2);


int minbuttonState = 0;         // current state of the button
int minlastButtonState = 0;     // previous state of the button
int maxbuttonState = 0;         // current state of the button
int maxlastButtonState = 0;     // previous state of the button
int onn = 0;
int onf = 0;
int ison = 0;

int spm = 800;
int freq = spm/120;

void setup() {
  pinMode(10, INPUT);
  pinMode(8, INPUT);
  pinMode(1, INPUT);
  
  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("RPM:  ");
  
}

void loop() {
  freq = spm/120;
  minbuttonState = digitalRead(10);
  maxbuttonState = digitalRead(8);
  onn = digitalRead(1);
  if (minbuttonState != minlastButtonState) {
    // if the state has changed, increment the counter
    if (minbuttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      spm = spm-200;
    }
    delay(50); 
    }
   if (maxbuttonState != maxlastButtonState) {
    // if the state has changed, increment the counter
    if (maxbuttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      spm = spm+200;
    }
    delay(50); 
    }
    if (onn!= onf) {
    // if the state has changed, increment the counter
    if (onn == HIGH) {
      if (ison == 0) {
      // if the current state is HIGH then the button
      // wend from off to on:
      tone(7, freq);
      ison = 1;
      delay(50);
      }
      else {
        noTone(7);
        ison = 0;
        }
        
        delay(50); 
    }
    }
    lcd.setCursor(0, 1);
    lcd.print(spm);
    lcd.print("   ");
    lcd.print(freq);
    lcd.print("   ");

minlastButtonState = minbuttonState;
maxlastButtonState = maxbuttonState;
onf = onn;
}

Ti invitiamo a presentarti (dicci quali conoscenze hai di elettronica e di programmazione) qui: Presentazioni
e a leggere il regolamento se non lo hai già fatto: Regolamento
Qui una serie di link utili, non inerenti al tuo problema:

La funzione tone() ha una frequenza tra 31Hz e 65535Hz per Arduino UNO.

Se imposti un valore minore, ottieni risultati erronei.

Per ottenere frequenze basse è meglio usare millis().

Eviterei di usare delay() che potrebbe non essere preciso a causa della sua dipendenza da interrupt.