possibile che siano addirittura posticipati di 204 microsecondi? forse ho sbaglaito a non postarvi tutto il codice, perchè da quello che vedo io in output ad esempio, la prima misura sul c_voltage avviene al tempo di 20us
#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
// 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("How many measure do you want to take?");
}
void loop()
{
int num_misure;
float c_voltage, ps_voltage, current;
static float res = 3.59;
unsigned long time1, time2, time0;
int i ;
if(Serial.available() > 0)
{
num_misure = Serial.parseInt(); // Arduino stream function which receive a string on the serial port and then changes it int an integer
if(num_misure > 0)
{
for(i = 1 ; i <= num_misure; i++)
{
Serial.print("Misura n.");
Serial.println(i);
digitalWrite(RELAY_PIN, HIGH);
delay(20); // relay transition time::::::: problema, ho impostato questo ritardo per aspettare la commutazione del ralay, ma questo non ha un tempo fisso
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;
Serial.println(ps_voltage);
Serial.println(time1);
Serial.println(c_voltage);
Serial.println(time2);
Serial.println(current);
Serial.print('\n');
}
while(current >0.000001);
Serial.println("Please wait for the capacitor to discharge");
digitalWrite(RELAY_PIN, LOW);
do
{
c_voltage = analogRead(A1);
c_voltage = c_voltage * 5 / 1023;
Serial.println(c_voltage);
delay(50); // just a delay to not print all the values
}
while(c_voltage > 0.001);
Serial.print("\n\n");
}
}
}}
l'ho modificato rispetto a quello postato prima settando il prescaler a 16 come mi avevi descritto anche tu
l'output che ho è questo:(lo posto come codice èper renderlo leggibile)
How many measure do you want to take?
Misura n.1
2.65
32
1.01
48
0.46
2.74
1592
1.03
1612
0.48
2.83
3332
1.11
3352
0.48
3.83
25316
2.09
25336
0.49
5.00
59636
3.58
59656
0.39
5.00
93956
4.86
93976
0.04
5.00
128280
5.00
128296
0.00
Please wait for the capacitor to discharge
5.00
1.64
0.47
0.13
0.01
0.00