Hi, I have a project which uses an ISR on a laser trigger. after 2 laser triggers occur in a row, 3 arrays should update with ADC information. I then communicate this information to a Raspberry Pi over Serial. My problem is the arrays are not updating every time 2 triggers occur. I know this because the timestamp of the two triggers and the amount of triggers that has occurred are also sent serially, and they update every time. I believe the mechanism by which I refill the arrays is not working. Anyway, below is my code, I call every variable in the ISR that updates a volatile variable:
volatile int countevent = 0, count2 = 0,count3 = 0;// countevent lets shift register know an event (2 crossings) has happened so save the data, timer counts how long since last interrupt
int i = 0, j = 0, eventnum = 0, evnum; //loop variable
volatile unsigned long timing = 0, timestamp = 0;
unsigned long timesince, timemid = 0; //timing times interrupt, timestamp gives time between 2 interrupts, timesince measures whether a false trigger occured
float velocity;
char LF = 10; //tells arduino xojo is done with command
uint16_t p1ch[16], p2ch[16], bch[16],vdiffp1[16],vdiffp2[16];
const String MyName = "Y1O";
String CMD;
void setup() {
// put your setup code here, to run once:
//timer2.setup();
//delay(3000);
attachInterrupt(digitalPinToInterrupt(3), myISR, RISING);//interrupt to store data from ADC in serial buffer after trigger event
//serial number written to eeprom
//EEPROM.put(0,"X01");
Serial.begin(115200); //Begin Serial Monitor 115200 baud rate
SPI.begin();
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE1)); //2MHz speed, most significant bit first, data capture on rising clock
}
//ISR
void myISR() {
if(count2 <= 1){
timestamp = micros() - timing;//timer2.get_count()-timing;
timing = micros();//timer2.get_count();
SPI.begin();
PORTB &= 0xFB;//select ADC
for(i = 0; i < 24; i++) { // artificially slow down interrupt
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
}
digitalWrite(CS, LOW);
SPI.transfer16(0); //transfer 16 bits of dummy data, enough to fill Serial buffers
digitalWrite(CS, HIGH);
SPI.end(); //allows for manual toggle of CLK to read data from Serial Buffer
digitalWrite(RST, LOW);
digitalWrite(RST, HIGH);
countevent++;//if 2 interrupts occur within 1 ms, an "event" has occured, gather shift register data
count2++;
}
}
//bit bang data from Serial Buffer A
void SRA() {
SPI.begin();
p1ch[3] = SPI.transfer16(0);//order daisy-chained serial buffers' data comes in
p1ch[2] = SPI.transfer16(0);
p1ch[1] = SPI.transfer16(0);
p1ch[0] = SPI.transfer16(0);
p1ch[7] = SPI.transfer16(0);
p1ch[6] = SPI.transfer16(0);
p1ch[5] = SPI.transfer16(0);
p1ch[4] = SPI.transfer16(0);
p1ch[11] = SPI.transfer16(0);
p1ch[10] = SPI.transfer16(0);
p1ch[9] = SPI.transfer16(0);
p1ch[8] = SPI.transfer16(0);
p1ch[15] = SPI.transfer16(0);
p1ch[14] = SPI.transfer16(0);
p1ch[13] = SPI.transfer16(0);
p1ch[12] = SPI.transfer16(0);
SPI.end();
}
//convert binary to decimal voltage
void SRConverter() {
if(count2 == 2) {
SRA();
count2 = 0;
}
}
void loop() {
SRConverter();
while(Serial.available() >0) {
String CMD = Serial.readStringUntil(LF);
if(CMD == MyName) {
digitalWrite(CTS, HIGH);
Serial.print(CMD);
Serial.flush();
for(j = 0; j < 16; j++) {
Serial.print(",");Serial.print(p1ch[j]);
Serial.flush();
Serial.print(",");Serial.print(p2ch[j]);
Serial.flush();
Serial.print(",");Serial.print(bch[j]);
Serial.print(",");
p1ch[j] = 0;
p2ch[j] = 0;
bch[j] = 0;
}
Serial.print(timestamp);
Serial.println(count3);
Serial.flush();
digitalWrite(CTS, LOW);
}
In SRConverter(), SRB and backGroundSignal are the exact same things as SRA, only filling up the other 2 arrays, so I left that out. SR converter only runs when count2 == 2, which happens after 2 triggers, and then is set back to 0 by SR converter, so every time 2 triggers occur, next time I poll the Arduino, the arrays should update. Any ideas?