He encontrado algunas diferencias en los datos transmitidos con estos 2 códigos:
- "datos no rápidos": es como yo lo hacía antes
- "datos rápidos": es como lo quiero hacer ahora
Después de los códigos pongo el resultado.
// ********************
// datos no rápidos
// ********************
#define mysize 200
#define PINS_SENSOR_BARRIER 4
unsigned long tStart;
unsigned long tEnd;
byte mydata[mysize];
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.println("");
Serial.println("esperando corte...");
mydata[0] =analogRead(PINS_SENSOR_BARRIER);
while (analogRead(PINS_SENSOR_BARRIER)>mydata[0]-10){};
tStart=micros();
for (int i=1; i<mysize;i++)
{
mydata[i]=analogRead(PINS_SENSOR_BARRIER);
}
tEnd=micros();
for (int i=0; i<mysize;i++)
{
Serial.print(mydata[i],DEC);
Serial.print(" ");
}
Serial.println("");
Serial.println("NEW ACQUISITION");
Serial.print("tStart=");
Serial.println(tStart);
Serial.print("tEnd=");
Serial.println(tEnd);
Serial.print("Tiempo: ");
Serial.println(tEnd-tStart);
Serial.print("nPoints=");
Serial.println(mysize);
}
tengo una duda
El resultado de este 1º código es:
"esperando corte...
108 74 56 40 29 22 19 16 17 17 16 17 16 17 17 17 19 21 29 44 61 77 92 100 103 106 106 106 107 107 107 107 107 106 108 107 107 108 108 108 108 108 107 108 108 107 108 108 108 108 108 108 108 108 107 108 108 108 107 108 107 108 107 107 107 107 108 107 106 107 107 107 107 107 107 107 107 107 107 107 107 106 107 107 107 107 107 106 107 107 107 107 107 107 107 107 107 107 108 107 107 107 107 106 107 106 108 107 108 107 108 107 108 107 108 108 107 107 108 108 108 108 108 107 108 108 108 108 108 108 108 108 108 108 108 108 107 108 108 108 107 108 108 107 107 108 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 106 107 107 107 107 106 107 107 106 107 106 107 107 106 107 107 106 107 107 106 107 107 106 107 107 107 107 107 107 108 107 107 107 107 107 107 107 107 108
NEW ACQUISITION
tStart=1129752
tEnd=1152040
Tiempo: 22288
nPoints=200
esperando corte..."
// ********************
// datos rápidos
// ********************
#define mysize 200
unsigned long tStart;
unsigned long tEnd;
byte mydata[mysize];
void setup()
{
Serial.begin(115200);
//Prescaler
//ADPS2 - ADPS1 - ADPS0 - Division Factor
//0 0 0 ->2
//0 0 1 ->2
//0 1 0 ->4
//0 1 1 ->8
//1 0 0 ->16
//1 0 1 ->32
//1 1 0 ->64
//1 1 1 ->128
//Configure to Prescaler=32
bitWrite(ADCSRA,ADPS2,1); // sbi(ADCSRA, ADPS2);
bitWrite(ADCSRA,ADPS1,0); // cbi(ADCSRA, ADPS1);
bitWrite(ADCSRA,ADPS0,1); // sbi(ADCSRA, ADPS0);
// Input Channel Selections
// MUX3210 Single Ended Input
// 0000 ADC0
// 0001 ADC1
// 0010 ADC2
// 0011 ADC3
// 0100 ADC4
// 0101 ADC5
// 0110 ADC6
// 0111 ADC7
// 1000 ACD8
// Entrada A4
ADMUX=(1<<ADLAR)|(0<<REFS1)|(1<<REFS0)|(0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(0<<MUX0);
}
void loop()
{
Serial.println("espero corte:");
mydata[0] =analogReadFast();
while (analogReadFast()>mydata[0]-10){};
tStart=micros();
for (int i=1; i<mysize;i++)
{
mydata[i]=analogReadFast();
}
tEnd=micros();
for (int i=0; i<mysize;i++)
{
Serial.print(mydata[i],DEC);
Serial.print(" ");
}
Serial.println("");
Serial.println("NEW ACQUISITION");
Serial.print("tStart=");
Serial.println(tStart);
Serial.print("tEnd=");
Serial.println(tEnd);
Serial.print("Tiempo: ");
Serial.println(tEnd-tStart);
Serial.print("nPoints=");
Serial.println(mysize);
}
int analogReadFast()
{
ADCSRA|=(1<<ADSC); // sbi(ADCSRA, ADSC);
// ADSC is cleared when the conversion finishes
while (bit_is_set(ADCSRA, ADSC));
return ADCH;
}
Y el resultado de éste código es:
"espero corte:
27 16 15 15 14 13 12 11 10 10 9 8 8 7 7 7 6 6 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 6 6 7 7 8 8 9 10 11 11 12 13 14 15 15 16 17 18 19 19 20 21 22 22 23 23 24 24 24 25 25 25 25 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 27 26 26 26 26 26 27 27 26 27 26 27 26 27 27 27 27 26 27 26 27 27 27 27 26 27 27 27 27 27 27 26 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 26 27 27 27 27
NEW ACQUISITION
tStart=2726484
tEnd=2732068
Tiempo: 5584
nPoints=200
espero corte:"
Es verdad que se gana en rapidez, pero en ambos casos he usado la misma barrera IR, con las mismas condiciones de luz, y cuando uso el método rápido los datos del sensor son mas pequeños. ¿donde está el fallo?