Hello all!
I am having some problems with piece of software I am trying to get to work. I'm a manufacturing engineer, so all of this is a little bit foreign to me. And we're on a skeleton crew, so this got delegated to me, ha.
I am making an optical bonding station that includes a few switches, valves, and relays. The program is pretty basic and doesn't need to do much, but I'm having some issues. So this basic idea of the program is, it checks two switch inputs to initiate the process (lid switch and start button). Once those switches are made, the RUN indicator, and the vac valve go HIGH... Once proper vac has been achieved, the vac switch goes HIGH and we activate the air cylinder (press) and turn the vac valve OFF. Once the micro sees the bottom cylinder switch go HIGH (the cylinder is down), there is a 5 sec delay to allow the bonding to take place. After the delay, the air valve is turned OFF and the cylinder goes back up, the top cylinder switch (cylSwitch1) then goes HIGH (the cylinder is in the up position). Lastly, once the cylSwitch1 goes HIGH, we actuate the vacuum release valve and turn the RUN indicator OFF. End program.
I have two problems with the code though..
-
cylSwitch1 is HIGH the entire program, except when it goes down to press the optical material. So when the program begin, the cylSwitch1 input is HIGH, and the code skips over everything before it and actuates the vacuum release valve. I tried setting the pin to an output up until the switch is read, and then switching it back, but that did not seem to help anything.
-
If I keep cylSwitch1 LOW until the point is needed, it executes the code correctly up until the LAST if statement. When the program checks cylSwitch1 and cylSwitch2 states, the program gets caught in a loop and just keep flipping the vac release valve on and off, because the statement remains true. The cylinder isn't moving anymore, so that if statement is indefinitely true and it just loops that last if statement.
I'm sure it has something to do with how I have the program structured, and I am not a software guy, so I know this isn't pretty. But I've been working on this all week and have not made much process. I designed and laid out the PCBs for the bonding station, and have built the station, just can not get this last thing to work.
Any help or guidance would be greatly appreciated. I really just need the program to step through in order, so cylSwitch1 does not force the program to jump to the very end of the loop, and I need to figure out how to get out of the loop at the end, even though the last if statement is true. I need the program to move on haha.
This is going to be used in a production setting, so when the program finishes, pressing the start button again (and closing the lid on the system) will begin the process over again. Again, I'm sure this isn't the right way to do what I'm trying, but I'm trying. Any and all help is greatly appreciated!!
Thanks very much everyone! Code below...
const int lidSwitch = 5;
const int startButton = 6;
const int cylSwitch1 = 7;
const int cylSwitch2 = 8;
const int vacSwitch = 9;
const int greenLED = 4;
const int vacValve = 12;
const int airValve = 2;
const int vacRelease = 3;
int lidVal = 0;
int startVal = 0;
int vacVal = 0;
int cyl1Val = 0;
int cyl2Val = 0;
void setup() {
pinMode(lidSwitch, INPUT);
pinMode(startButton, INPUT);
pinMode(cylSwitch2, INPUT);
pinMode(vacSwitch, INPUT);
pinMode(greenLED, OUTPUT);
pinMode(vacValve, OUTPUT);
pinMode(airValve, OUTPUT);
pinMode(vacRelease, OUTPUT);
}
void loop() {
pinMode(cylSwitch1, OUTPUT);
lidVal = digitalRead(lidSwitch);
startVal = digitalRead(startButton);
if (lidVal == HIGH && startVal == HIGH) {
digitalWrite(greenLED, HIGH);
digitalWrite(vacValve, HIGH);
delay(100);}
vacVal = digitalRead(vacSwitch);
if (vacVal == HIGH) {
digitalWrite(vacValve, LOW);
digitalWrite(airValve, HIGH);
delay(100);}
cyl2Val = digitalRead(cylSwitch2);
if (cyl2Val == HIGH){
delay(5000);
digitalWrite(airValve, LOW);
delay (100);}
delay(100);
pinMode(cylSwitch1, INPUT);
cyl2Val = digitalRead (cylSwitch2);
cyl1Val = digitalRead (cylSwitch1);
if (cyl2Val == LOW && cyl1Val == HIGH) {
digitalWrite (vacRelease, HIGH);
delay (1000);
digitalWrite (vacRelease, LOW);
digitalWrite (greenLED, LOW);
pinMode(cylSwitch1, OUTPUT);
}
}




