I am trying to create a code that collects event information from two photoelectric sensors and puts it into excel. Our current code, which is attached, is not collecting the information when it passes the sensor, instead it is looping and missing events. Please help us figure out how to change this code to only record the event when it records, instead of looping in this manner.
unsigned long int micros_time; //variable to hold the time
float voltage; //variable to hold the voltage form A0
int Counter1;
int Counter2;
void setup() {
Serial.begin(128000); //Fastest baudrate
Serial.println("CLEARDATA"); //This string is defined as a
attachInterrupt(digitalPinToInterrupt(2), Count1, RISING);
attachInterrupt(digitalPinToInterrupt(3), Count2, RISING);
Counter1=0;
Counter2=0; // commmand for the Excel VBA
// to clear all the rows and columns
Serial.println("LABEL,Computer Time,Time(Micros Sec.),Counter1,Counter2");
//LABEL command creates label for
// columns in the first row with bold font
}
void loop() {
micros_time = micros();
voltage = 5.0 * analogRead(A0) / 1024.0; //Convert RawBinary to equivalent voltage. 10-bit ADC on Arduino.
{
}
By setting th counters equal to each other it was the only way that it would spit out data to excel. By putting the “!” In front of the equals sign it would not spit out the data that we are attempting to collect. The software “works” but it does not work the way that we want to which is why I posted on here.
We still need it to time stamp when it goes through the sensor. It is currently timestamping every 100 micro seconds regardless of anything going through the sensor
That is a ridiculous assertion. You must have misunderstood something. There is absolutely no use in setting a variable equal to itself. The other line I wrote does EXACTLY the same thing. If you want to claim they are doing something different then you need to provide vidence for that. Show the two codes and their outputs.
It is time stamping every 100ms because that stupid if condition is always true and never false. So it tells it every pass of loop to time stamp. That IS the problem you came about. That line IS the problem.
Counter1 and Counter2 need to be declared volatile as they used in ISRs.
Reading the values of Counter1 and Counter2 must be done inside critical sections as they are
each more than one byte.
The test I presume you want is something like:
int prev_counter1 = 0 ; // state needed to detect change
void loop()
{
noInterrupts ();
int current_counter1 = Counter1 ; // sample in a critical section so value isn't mangled
interrupts ();
if (current_counter1 != prev_counter1) // detect change
{
... // business logic here
prev_counter1 = current_counter1 // update state
}
...
}