Hi everybody. I have this piece of code, but in my serial monitor I dont get anything.
When I call analogRead one or two times y get the result, but when I call it three to six times it does not work.
Any suggestion?
// Arrays to save our results in
unsigned long start_times[100];
unsigned long stop_times[100];
unsigned long values0[100];
unsigned long values1[100];
unsigned long values2[100];
unsigned long values3[100];
unsigned long values4[100];
unsigned long values5[100];
// Setup the serial port and pin 2
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned int i;
// capture the values to memory
for(i=0;i<100;i++) {
start_times[i] = micros();
values0[i] = analogRead(A0);
values1[i] = analogRead(A1);
values2[i] = analogRead(A2);
values3[i] = analogRead(A3);
values4[i] = analogRead(A4);
values5[i] = analogRead(A5);
stop_times[i] = micros();
}
// print out the results
Serial.println("\n\n--- Results ---");
for(i=0;i<100;i++) {
Serial.print(values4[i]);
Serial.print(" elapse = ");
Serial.print(stop_times[i] - start_times[i]);
Serial.print(" us\n");
}
delay(6000);
}
You do not need the stop times as they are essentially the start times of the next round.
and as you are only interested in the difference that will fit in a byte too (with a little trick)
=> you can get 130 readings per channel,
Sketch uses 2,988 bytes (9%) of program storage space. Maximum is 32,256 bytes. Global variables use 1,913 bytes (93%) of dynamic memory, leaving 135 bytes for local variables. Maximum is 2,048 bytes. Low memory available, stability problems may occur
#define SIZE 130
byte times[SIZE];
unsigned int values0[SIZE];
unsigned int values1[SIZE];
unsigned int values2[SIZE];
unsigned int values3[SIZE];
unsigned int values4[SIZE];
unsigned int values5[SIZE];
// Setup the serial port and pin 2
void setup()
{
Serial.begin(115200); // way faster
}
void loop()
{
for (byte i = 0; i < SIZE; i++)
{
unsigned long start = micros();
values0[i] = analogRead(A0);
values1[i] = analogRead(A1);
values2[i] = analogRead(A2);
values3[i] = analogRead(A3);
values4[i] = analogRead(A4);
values5[i] = analogRead(A5);
times[i] = (micros() - start)/4;
}
// print out the results
Serial.println("\n\n--- Results ---");
for(byte i = 0; i < SIZE; i++)
{
Serial.print(values4[i]);
Serial.print(" elapse = ");
Serial.print(times[i] * 4);
Serial.print(" us\n");
}
delay(6000);
}