I am using LabView to control a Fluigent pump (2 channel pressure pump) by setting a pressure (800 or 0) based on when my Arduino is triggering a signal generator. The pump and signal generator need to be triggered at the same time, i.e. the signal generator is HIGH, the first channel of the pump should be ON and the second channel OFF, when the signal generator is LOW, the first channel goes OFF and second channel goes ON. This whole thing should be triggered by a physical switch connected to my Arduino.
I have a labView code that works, which I first tested just using the switch, so my serial communication is working.
My arduino code works for triggering the signal generator without a problem.
BUT when I try to combine all of the code in my Arduino, when I flip the switch the signal generator is triggered accordingly but my pump is never triggered.
Here is an image of my labView code and my arduino code below:
const int swGoStp = 8; // Go stop switch
const int Anapico = 9; // Signal Generator pulse
bool n = false;
void setup() {
Serial.begin(9600);
pinMode(swGoStp, INPUT);
pinMode(Anapico, INPUT);
pinMode(Anapico, OUTPUT);
}
void loop() {
if (digitalRead(swGoStp) == HIGH) { // Only run when switch is high
Serial.print("0\n");
delay(1000);
// Trap
digitalWrite(Anapico, HIGH); // Turn Anapico on
if (digitalRead(Anapico) == HIGH) { // Loading IDTs are ON --> Forward flow
// (Valve 1 & 4 OPEN, 2 & 3 CLOSED)
Serial.print("1\n");
delay(20000); // Forward imaging
digitalWrite(Anapico, LOW); // Turn Anapico off
delay(10000);
} else if (digitalRead(Anapico) ==
LOW) { // Loading IDTs are OFF --> Reverse flow (Valve 1 & 4
// CLOSED, 2 & 3 OPEN)
Serial.print("2\n");
}
} else if (digitalRead(swGoStp) == LOW) {
digitalWrite(Anapico, LOW);
}
}
Your 31 seconds of delay() is blocking all MCU functions. Rather than using delay(), start using millis() for timed events (not later, but now, and for the rest of your life). Your code will never have this problem again.
@jremington just covered that on another thread. He boiled it down to:
Thanks for that, sorry I am still pretty new to all of this, from what I understand millis() just tracks, it doesn't actually set any delays right? I require certain time delays between triggering the signal generator (Anapico) and my pump should be triggered at those same time intervals (well, roughly).
@cyta millis() just returns a timestamp. It's how you use it that matters. Instead of going to sleep, you loop, constantly doing things, then checking to see if now-then is > threshold, and if so, you do something. The joy is, instead of sleeping doing nothing, your code can do other functions while waiting for the event
So, essentially I can do something like this?
Another question, since I am using my Anapico pin as both an output and an input does it matter how I assign it at the beginning, and do I need to change it at some point?
const int swGoStp = 8; // Go stop switch
const int Anapico = 9; // Signal Generator pulse
bool n = false;
unsigned long previousMillis = 0;
unsigned long interval = 1000;
unsigned long ForwardImaging = 10000;
unsigned long ReverseImaging = 10000;
void setup() {
Serial.begin(9600);
pinMode(swGoStp, INPUT);
pinMode(Anapico, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (digitalRead(swGoStp) == HIGH) { // Only run when switch is high
Serial.print("0\n");
// Trap
digitalWrite(Anapico, HIGH); // Turn Anapico on
if (digitalRead(Anapico) ==
HIGH) { // Loading IDTs are ON --> Forward flow (Valve 1 & 4 OPEN, 2
// & 3 CLOSED)
Serial.print("1\n");
unsigned long imagingStart = millis();
while (millis() - imagingStart < ForwardImaging) {
// Forward imaging
}
digitalWrite(Anapico, LOW); // Turn Anapico off
unsigned long nextImaging = millis();
while (millis() - nextImaging < ReverseImaging) {
// Reverse imaging
}
} else if (digitalRead(Anapico) ==
LOW) { // Loading IDTs are OFF --> Reverse flow (Valve 1 & 4
// CLOSED, 2 & 3 OPEN)
Serial.print("2\n");
}
} else if (digitalRead(swGoStp) == LOW) {
digitalWrite(Anapico, LOW);
}
}
}