/*
AUTONOMOUS GRIPPER CODE
Specifies the timing and sequence of movemnts involving the x-axis pneumatic cylinder,
the y-axis pneumatic cylinder, and the venturi tube (vacuum) on the Autonomous Gripper using Arduino
Charlene Lam
2023-06-20
*/

// Constants

const int relayZCylPin = 11; // Pin connected to Relay for ZCyl control
const int relayXCylPin = 10; // Pin connected to Relay for XCyl control
const int relayVacuumPin = 9; // Pin connected to Relay for Vacuum control
const int startButtonPin = 7; // Pin connected to the start button

// Function to control the relays
void controlRelays(bool relayZCylState, bool relayXCylState, bool relayVacuumState) {
 digitalWrite(relayZCylPin, relayZCylState ? LOW : HIGH);
 digitalWrite(relayXCylPin, relayXCylState ? LOW : HIGH);
 digitalWrite(relayVacuumPin, relayVacuumState ? HIGH : LOW);
}

// Setup function
void setup() {
 Serial.begin(9600);
 pinMode(relayZCylPin, OUTPUT);
 pinMode(relayXCylPin, OUTPUT);
 pinMode(relayVacuumPin, OUTPUT);
 pinMode(startButtonPin, INPUT_PULLUP);
}

// Main loop
void loop() {
 controlRelays(false, false, true); // Home ZCyl, XCyl, and Vacuum
 delay(1000);
 Serial.println(digitalRead(startButtonPin)); // Start serial monitor
 delay(1000);

// Check if the start button is pressed to start the machine
 if (digitalRead(startButtonPin) == LOW) {
  // Run the machine sequence
  // Note: ZCyl extended = true, XCyl extended = true, vacuum off = true
  controlRelays(false, false, false); // Vacuum on
  delay(1000);
  controlRelays(true, false, false); // ZCyl extends
  delay(2000);
  controlRelays(false, false, false); // ZCyl retracts
  delay(1000);
  controlRelays(false, true, false); // XCyl extends
  delay(1000);
  controlRelays(true, true, false); // ZCyl extends
  delay(1000);
  controlRelays(true, true, true); // Vacuum off
  delay(1000);
  controlRelays(false, true, true); // ZCyl retracts
  delay(1000);
  controlRelays(false, false, true); // XCyl retracts
  }
}
  }
}