Proprio adesso ho scritto un'abbozzo del codice che consentirà di mappare un minimo la carburazione. Ho testato l'integrato che sostituirà il potenziometro: è abbastanza semplice da controllare. Bisogna solo mandargli in seriale il valore (da 0 a 255), e il valore resta impresso finché non scollego l'alimentazione 5v.
#include <SPI.h>
const int slaveSelectPin = 10;
const int tpsLett=A0; //la lettura analogica del tps
const int rpmLett=6; //la lettura analogica dei rpm
int tps=0; //la variabile che accoglierà il tps
int rpm=0; //la variabile che accoglierà gli rpm
int level=0; //il dato da inviare al potenziometro
//la mappa caricata
int TPS5[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int TPS10[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int TPS20[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int TPS30[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int TPS50[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int TPS70[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int TPS100[15] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void setup()
{
pinMode (slaveSelectPin, OUTPUT);
SPI.begin();
digitalPotWrite(level);
}
void loop()
{
tps=mapTps(analogRead(tpsLett)); //fase 1: leggo i valori e li mappo
rpm=mapRpm(pulseIn(rpmLett,HIGH,2000));
//quantizzo i valori rpm
if (rpm<=2000) {rpm=0;}
else {
if ((rpm>2000)&&(rpm<=3000)) {rpm=1;}
else {
if ((rpm>3000)&&(rpm<=4000)) {rpm=2;}
else {
if ((rpm>4000)&&(rpm<=5000)) {rpm=3;}
else {
if ((rpm>5000)&&(rpm<=6000)) {rpm=4;}
else {
if ((rpm>6000)&&(rpm<=7000)) {rpm=5;}
else {
if ((rpm>7000)&&(rpm<=8000)) {rpm=6;}
else {
if ((rpm>8000)&&(rpm<=9000)) {rpm=7;}
else {
if ((rpm>9000)&&(rpm<=10000)) {rpm=8;}
else {
if ((rpm>10000)&&(rpm<=11000)) {rpm=9;}
else {
if ((rpm>11000)&&(rpm<=12000)) {rpm=10;}
else {
if ((rpm>12000)&&(rpm<=13000)) {rpm=11;}
else {
if (rpm>13000) {rpm=12;}
}
}
}
}
}
}
}
}
}
}
}
}
//quantizzo i valori tps
if (tps<=5) {tps=5;}
else {
if ((tps>5)&&(tps<=10)) {tps=10;}
else {
if ((tps>10)&&(tps<=20)) {tps=20;}
else {
if ((tps>20)&&(tps<=30)) {tps=30;}
else {
if ((tps>30)&&(tps<=50)) {tps=50;}
else {
if ((tps>50)&&(tps<=70)) {tps=70;}
else {
if (tps>70) {tps=100;}
}
}
}
}
}
}
//adesso mando i valori della mappa al potenziometro
switch (tps) {
case 5:
tps5(rpm);
break;
case 10:
tps10(rpm);
break;
case 20:
tps20(rpm);
break;
case 30:
tps30(rpm);
break;
case 50:
tps50(rpm);
break;
case 70:
tps70(rpm);
break;
case 100:
tps100(rpm);
break;
}
} //fine del ciclo loop
int digitalPotWrite(int value) //funzione che si occupa di mandare un valore al potenziometro compreso tra 0 e 255
{
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x11);
SPI.transfer(value);
digitalWrite(slaveSelectPin, HIGH);
}
int mapTps(int value) //funzione che mappa il valore tps da 0 a 100
{
map(value,0,255,0,100);
return value;
}
int mapRpm(float value) //funzione che trasforma il periodo in rpm
{
value = 15/(value*1000000);
return value;
}
//qui le funzioni che scelgono il vettore mappa in funzione dell'apertura tps
int tps5 (int value)
{
}
int tps10 (int value)
{}
int tps20 (int value)
{}
int tps30 (int value)
{}
int tps50 (int value)
{}
int tps70 (int value)
{}
int tps100 (int value)
{}