per non complicarmi troppo la vita per il momento ho deciso di lasciare la risoluzione a 8bit, vorrei provare però a cambiare strategia mettendo i dati prima in alcuni array e poi a stamparli, il problema è che dopo che accendo il relay, non succede niente, non mi stampa niente.
non mi sembra che il codice sia sbagliato.
i due array bidimensionali fungono da tabelle: nella prima colonna ci sono i valori di tensione misurati, nella seconda colonna ci sono i tempi a cui avvengono le misure. il principio è di riempire gli array con un numero x di misure e poi ordinare i risultati in ordine crescente dei tempi, ma ripeto, non ho alcun output.
qualcuno riesce a vedere il problema?
#define RELAY_PIN 3 //digital pin to control the relay
#define c_pin A1 //capacitor analog pin
#define ps_pin A0 //power supply analog pin
#define num_misure 50
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
void setup()
{
// set prescale to 16
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
cbi(ADCSRA,ADPS0) ;
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600); // open serial
Serial.println("Press the spacebar to start to measure");
}
void loop()
{
static int relayVal = 0;
int cmd;
float c_voltage, ps_voltage, current;
static float res = 3.59;
unsigned long time1, time2, time0;
float PS_VOLTAGE[1000][2]; // these two arrays are tables in which we put the voltage
float C_VOLTAGE[1000][2]; // measurements and the time arduino took em
float CURRENT[1000];
int j = 0;
while(Serial.available() > 0)
{
cmd = Serial.read();
switch(cmd)
{
case ' ':
{
relayVal ^= 1;
Serial.println("Relay on");
break;
}
default:
{
Serial.println("Press the spacebar to start to measure");
}
}
if(relayVal)
{
do{
digitalWrite(RELAY_PIN, HIGH);
delay(20); // relay transition time ---------- problema, ho impostato questo ritardo
// per aspettare la commutazione del relay, ma questa non è tempocostante
time0 = micros(); // time the measure start
do
{
c_voltage = analogRead(c_pin);
time1 = micros()-time0; //time c_voltage measurement
ps_voltage = analogRead(ps_pin);
time2 = micros() - time0; //time ps_voltage measurement
c_voltage = c_voltage *5 /1023;
ps_voltage = ps_voltage * 5 / 1023;
current = (ps_voltage - c_voltage)/res;
//put all the measurements in their arrays
PS_VOLTAGE[j][0] = ps_voltage;
PS_VOLTAGE[j][1] = time1;
C_VOLTAGE[j][0] = c_voltage;
C_VOLTAGE[j][1] = time2;
CURRENT[j] = current;
j++;
}
while(current >0.000001);
//capacitor discharge
digitalWrite(RELAY_PIN, LOW);
do
{
c_voltage = analogRead(c_pin);
c_voltage = c_voltage * 5 / 1023;
}
while(c_voltage > 0.001);
}
while( j <= num_misure - 1 );
/*
ordinamento vettori in base ai tempi per sincronizzare le misure
*/
j= 0;
//stampa risultati
for ( j = 0; j < num_misure - 1; j++)
{
Serial.print(PS_VOLTAGE[j][0]);
Serial.print(" ");
Serial.println(PS_VOLTAGE[j][1]);
}
relayVal = 0; //reitialize relayVal
Serial.print("\n\n");
Serial.print("do you want to repeat the measure?");
}
}
}