//1ch waveform sampler sketch //FASTADC set to 1 ups the sampling rate to 77kHz //Found this on the Arduino Forum here http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11 - Thanks to jmknapp #define FASTADC 0 // 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 //variable used to signal when we are to take a sample. int getSample; //360 locations is about the maximum on the memory of my Arduino int aval[360]; //Used to measure the length in time of the sample unsigned long t1,t2,t3; //stores the byte sent by the program when the button sample is pressed int incomingByte = 0; void setup() { //ADC sample rate code #if FASTADC //set prescale to 16 sbi(ADCSRA,ADPS2) ; cbi(ADCSRA,ADPS1) ; cbi(ADCSRA,ADPS0) ; #endif Serial.begin(115200); } void loop() { //Check if the java program has sent the command to start sampling if (Serial.available()>0) { incomingByte = Serial.read(); getSample = 1; } //If were ok to go with sampling... if (getSample == 1) { getSample =0; //Wait until the waveform is near the 0 line. //int raw=analogRead(0)-512; //while (raw<-10 || raw>10) raw=analogRead(1)-512; //Take the time at the start t1=millis(); //Take 360 samples for(int i=0; i<360; i++) { aval[i]=analogRead(0); } //Take end time t2=millis(); //Calculate overall time t3=t2-t1; //Send the time taken first so that java program can set time intervals //between each sample. Serial.print(t3); Serial.print("C"); //Send sampled data in one lump. for(int i=0; i<360; i++) { Serial.print(aval[i]); Serial.print("A"); } } }