This loop checks if a certain pin went HIGH between the rising and falling edge. If that certain pin high during this period, then switch ON a Led.
I have tried to have an interrupt fired when the pin goes high (i.e. when rising edge is detected). Now I am trying in the ISR to check if that pin went HIGH. But I need to store this result and use it outside the ISR (i.e. after falling edge has been detected).
bitoff_arduino:
I have tried to have an interrupt fired when the pin goes high (i.e. when rising edge is detected). Now I am trying in the ISR to check if that pin went HIGH.
If the interrupt was triggered by a rising edge why waste time checking for a HIGH - what else could it be?
bitoff_arduino:
This loop checks if a certain pin went HIGH between the rising and falling edge.
Well, of course it does go high, as otherwise it would not have a rising or falling edge. Your pin was LOW, then went HIGH, then LOW again. That's how you get a rising and falling edge.
Normally you would detect it using a state change detection:
bool state = LOW;
void loop() {
if (digitalRead(pin) == HIGH && state = LOW) {
// Rising edge detected!
state = HIGH; // Record current state.
}
if (digitalRead(pin) == LOW && state == HIGH) {
// Falling edge detected!
state = LOW; // Record current state.
}
}
Thank you for your suggestions. I have slightly changed my approach.
The problem to be solved is:
I have a device which detects presence of red and green dye in flowing microliter droplet and outputs digital signal if a particular dye is present.
So if both red and green are present in a droplet it outputs two digital signals. If only red or green is present it outputs only one corresponsing signal. The more the concentration of the dye higher the pulse width of the digital signal.
In this case all my droplets will have green signal but some will have red missing. Now my task is to find the droplet in which the red signal is absent, and trigger a camera to take images of that particular droplet. The camera takes digital pulses.
Here is what I have done. The thing I have a digital trigger exactly opposite to my aim. I have a digital signal when I have both red and green signals. But I need to have a signal when only green is present and red is absent.
See the attached picture for results. The green and red are the output signals from the device. White signal is the output (which will be used to trigger the camera) of the following code.
const int redChannel = 9;
const int greenChannel = 10;
const int OutputPin = 5;
int pulseCounter = 0;
int redState;
int greenState;
boolean storeState = LOW;
void setup() {
pinMode(redChannel, INPUT);
pinMode(greenChannel, INPUT);
pinMode(OutputPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
greenState = digitalRead(greenChannel);
while (greenState == HIGH) {
redState = digitalRead(redChannel);
if (redState == HIGH){
storeState = HIGH;}
greenState = digitalRead(greenChannel);
}
if (storeState == HIGH){
digitalWrite(OutputPin, HIGH);
delayMicroseconds(200);
digitalWrite(OutputPin, LOW);}
storeState = LOW;
}
Not too hard, and you're quite close. Timing is important here of course.
Do you want to trigger the camera always exactly when the green signal is finished, or a certain time after the signal begins?
You need to do some state checking.
Set two flags when you see a green signal: that you see a green signal, and that you did not (yet) see a red signal.
If you see a red signal, reset that second flag.
If the green signal stop and the "no red" flag is still set, you haven't seen red and trigger the camera.
bool haveGreen;
bool noRed;
void loop() {
if (digitalRead(greenChannel) && haveGreen == false) { // Detect start of the green pulse.
haveGreen = true;
noRed = true;
}
if (digitalRead(redChannel) && noRed) { // Start of the red pulse. May or may not happen.
noRed = false;
}
if (digitalRead(greenChannel) == LOW && haveGreen) { // End of the green pulse.
haveGreen = false;
if (noRed) { // Green is finished and we didn't see a red pulse.
digitalWrite(OutputPin, HIGH);
delayMicroseconds(200);
digitalWrite(OutputPin, LOW);
}
}
}
Also note how this code is not blocking: you could do other things in the meantime.
Thank you so very much. This is what i have been trying to do for days. I have attached the result.
wvmarle:
Do you want to trigger the camera always exactly when the green signal is finished, or a certain time after the signal begins?
The camera should take the photo of the droplet of interest, i.e. the one with no red. So ideally it should be triggered the green signal is still high. Is that possible in this solution, since we make a decision of no Red was found after the green has gone down?
But this will work too, after some optics adjustments.
Then you have to record when the green pulse started (millis() or micros() depending on the required time scale), wait until you're sure there's no red pulse coming, and take your photo.
Or if that doesn't work, take a photo of every single droplet (the moment you get the green pulse, or after a fixed interval), and have the Arduino provide a signal so the camera can mark the images as "hasRed" and "doesntHaveRed" afterwards.