Skip to content

Instantly share code, notes, and snippets.

@pingud98
Created September 1, 2016 20:18
Show Gist options
  • Save pingud98/d054bb14f4215c16346524a624c7e51e to your computer and use it in GitHub Desktop.
Save pingud98/d054bb14f4215c16346524a624c7e51e to your computer and use it in GitHub Desktop.
A slightly modified and better version of the software trigger. Includes LED output on Pin 13 irrespective of communications.
uint16_t bufa; // 4 buffers of 256 readings
uint16_t bufb; // 4 buffers of 256 readings
bool firstrun = 0;
int t; //time since last event
float maxcount;
float slowfreq; //second frequency measure,
//int f;
int evt; //event
void setup() {
Serial.begin(115200);
Serial.println("Cosmic Pi Status: Alive");
int t = analogRead(0);
pinMode(13, OUTPUT);
//int evt; //events
ADC->ADC_MR |= 0x80; // these lines set free running mode on adc 4 and adc 6 (pin A0 and A1 - see Due Pinout Diagram thread)
ADC->ADC_CR = 2;
ADC->ADC_CHER = 0x50; // this is (1<<7) | (1<<6) for adc 3 and adc 6
evt=0;
//f = 1;
maxcount = 0;
}
void loop() {
int q0 = 0, q1 = 0;
int a1, a3;
//for (int i = 0; i < 5000; i++) {
while ((ADC->ADC_ISR & 0x50) != 0x50); // wait for two conversions (pin A3[4] and A1[6])
a1 = ADC->ADC_CDR[4]; // read data on A3 pin
a3 = ADC->ADC_CDR[6]; // read data on A1 pin
bufa = a1;
bufb = a3;
if (bufb > 600) { //software trigger threshold
if (bufa > 600) { //software trigger threshold
t=millis()-t;
if (t > 0) {
//f =((f+(1000/t))/2);
Serial.print("Cosmic Pi coincidence trigger event. Potential cosmic rays caught so far: "); Serial.print(evt); Serial.print(". Time since last event in ms: "); Serial.print(t); Serial.print(" Frequency: ");
//Serial.println(f);
maxcount = maxcount + (t/1000);
slowfreq = (1/(maxcount/evt));
//Serial.println(maxcount);
Serial.print(slowfreq);
Serial.println(" Hz");
evt++;
t=millis();
digitalWrite(13,HIGH);
delay(5);
digitalWrite(13,LOW);
}
}
}
//q0+=a1;
//q1+=a3;
//}
//t = micros() - t;
//Serial.print("5000 pairs of conversions in ");Serial.print(t);Serial.println(" micros");
//Serial.print("A0 total:");Serial.println(q0);
//Serial.print("A1 total:");Serial.println(q1);
//Serial.print("A0:");Serial.println("A1:");
/*if (firstrun) {
for (int i = 0; i < 5000; i++) {
if (bufb[i] > 920) { //software trigger
if (bufa[i] > 920) { //software trigger
//evt++;
Serial.print(evt);Serial.print(" Time since last trigger ");Serial.print(t);Serial.println(" micros");
//for (int j = i - 10; j < i + 40; j++) {
// Serial.print(j); Serial.print("; "); Serial.print(bufa[j]); Serial.print("; "); Serial.print(bufb[j]); Serial.println(";");
//}
}
}
}
t=micros();
}
firstrun = 1;
*/
}
@pingud98
Copy link
Author

pingud98 commented Sep 1, 2016

Needs the SAM3X libraries installed to compile! Target platform is the Arduino DUE (though actually it should work ok on the Uno/Mega too - untested).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment