#define ARRAYlong 38 static boolean chartline = true; static boolean diag = false; static boolean diagp = true; static boolean diagv = true; int Ain[ARRAYlong]; int numberOfSamples = 0; int Achannel=1; // A1 int Amin= 1024; int Amax= 0; static int VZ = 512; static int trig = 10; // VZ - trig OR VZ + trig will switch to BATCH reading int VZu = VZ-trig; int VZo = VZ+trig; // timing // for inside get_time_u() unsigned long Ltime; unsigned long Ltime_old; // microseconds memory last scan unsigned long Ltime_delta; // microseconds delta from last scan // for inside main unsigned long Ltime_now; unsigned long Ltime_last; unsigned long Ltime_diff; // timing main and batch static unsigned long sample_main_usec = 250000; static unsigned long sample_batch_usec = 11000; // batch = ARRAYlong * sample_batch_usec double sfreq; // custom units #define useCUSTOM // for VOLT static float ACfactor=0.004887; // 5V/1023 static float ACVdif=1.0; // ( call it probe 1/1 ) float ACval; static char ACstr[4]=",V,"; // optional chart output int cApos; static char outchar='_'; // optional operation char ainByte; static int delayUSBrecords = 3; // too many hangups on the arduino terminal //_________________________________________________________________________ void get_time_u() { Ltime = micros(); // read actual micros ( 4,8,.... up to 70 min ) if ( Ltime < Ltime_old ) { Ltime_delta = 4294967285 - Ltime_old + Ltime ; // NOT TESTED!! } else { Ltime_delta = Ltime - Ltime_old; // remember delta time between 2 readings } Ltime_old = Ltime; // remember this time for next delta calc } // end get_time //_________________________________________________________________________ void readsamples() { numberOfSamples=0; // first make the cleanup of array so inside loop not need to check for ( int i = 0; i < ARRAYlong; i++ ) { Ain[i] = 0; } get_time_u(); while ( numberOfSamples < ARRAYlong ) { Ain[numberOfSamples] = analogRead(Achannel); delayMicroseconds(sample_batch_usec); numberOfSamples++; //Count number of times looped. } // end while samplecount get_time_u(); if (diagp) { Serial.print(",samplesusec ");Serial.print(sample_batch_usec); Serial.print(",channel ");Serial.print(Achannel); Serial.print(",samplebatchmsec ");Serial.print(int(Ltime_delta/1000));Serial.print(",samples ");Serial.print(numberOfSamples); sfreq = float(numberOfSamples)*1000.0/float(Ltime_delta); Serial.print(",samplefreq ");Serial.print(long(sfreq*1000)); Serial.print(" Hz,"); Serial.println(); } // end diagp // get array info: Amin= 1024; Amax= 0; for ( int i = 0; i < ARRAYlong; i++ ) { if ( Ain[i] > Amax) { Amax = Ain[i]; } if ( Ain[i] < Amin) { Amin = Ain[i]; } } } //_________________________________________________________________________ void print_chart() { //Serial.println(); if ( false ) { for (int j=0; j < 101; j++){ outchar='_'; if ( j == 0 || j == 10 || j == 20 || j == 30 || j == 40 || j == 50 || j == 60 || j == 70 || j == 80 || j == 90 || j == 100 ) { outchar='|'; } Serial.print(outchar); } Serial.println(); } // end if chartline header for (int i=0; i < ARRAYlong; i++){ if ( chartline ) { chartlineprint(i); delay(delayUSBrecords); // arduino terminal hangups } } // end along samples } // end print_chart //_________________________________________________________________________ void chartlineprint(int count) { if ( chartline ) { cApos=int(Ain[count]/10.23); for (int j=0; j < 101; j++){ outchar='_'; if ( j == 0 || j == 10 || j == 20 || j == 30 || j == 40 || j == 50 || j == 60 || j == 70 || j == 80 || j == 90 || j ==100) { outchar='|'; } if ( j == cApos ) { outchar=48+Achannel; } Serial.print(outchar); } // end 100 long line Serial.print(','); Serial.print(count); Serial.print(','); Serial.print(Ltime_delta/1000); Serial.print("ms, "); Serial.print(Ain[count]); Serial.print(','); #if defined(useCUSTOM) ACval = Ain[count]*ACfactor*ACVdif; Serial.print(ACval);Serial.print(ACstr); #endif Serial.println(); } //end if chartline } // end chartlineprint //_________________________________________________________________________ void setup() { Serial.begin(115200); } //_________________________________________________________________________ void loop() { if (Serial.available() > 0) { ainByte = Serial.read(); if ( ainByte == '+' ) { Serial.println("get +"); } if ( ainByte == '-' ) { Serial.println("get -"); } } // main loop timed sampling Ltime_now = micros(); Ltime_diff = (Ltime_now - Ltime_last); if ( Ltime_diff > sample_main_usec ) { Ltime_last = Ltime_now; get_time_u(); Ain[0] = analogRead(Achannel); if (diag ) { Serial.print(Ltime_diff); Serial.print(" read ");} if (diag ) { Serial.println(Ain[0]); } chartlineprint(0); if ( (Ain[0] >= VZo) || (Ain[0] <= VZu) ) { readsamples(); print_chart(); if (diagv ) { Serial.print("velo= "); Serial.println(Amax-Amin); } } } } // end loop //_________________________________________________________________________