// Define pin assignments for components
const int fwdButtonPin = 2; // Pin for the forward button
const int revButtonPin = 3; // Pin for the reverse button
const int lhsSwitchPin = 4; // Pin for the left-hand side reed switch
const int rhsSwitchPin = 5; // Pin for the right-hand side reed switch
const int motorRSPin = 6; // Pin for the RS signal of the motor
const int motorLSPin = 7; // Pin for the LS signal of the motor
const int y1Pin = 8; // Pin for controlling Y1
const int aPlusPin = 9; // Pin for the A+ reed switch
const int aMinusPin = 10; // Pin for the A- reed switch
const int vacPin = A5; // Pin for controlling the vacuum
const int y2Pin = 11; // Pin for controlling Y2
const int bPlusPin = 12; // Pin for the B+ reed switch
const int bMinusPin = 13; // Pin for the B- reed switch
const int startButtonPin = A4; // Pin for the START button
const int stopButtonPin = A3; // Pin for the STOP button
const int AMswitch = A2; // Pin for the A/M Switch
void setup()
{
// Initialize pins
pinMode(AMswitch, INPUT);
pinMode(fwdButtonPin, INPUT);
pinMode(revButtonPin, INPUT);
pinMode(lhsSwitchPin, INPUT);
pinMode(rhsSwitchPin, INPUT);
pinMode(motorRSPin, OUTPUT);
pinMode(motorLSPin, OUTPUT);
pinMode(y1Pin, OUTPUT);
pinMode(aPlusPin, INPUT);
pinMode(aMinusPin, INPUT);
pinMode(vacPin, OUTPUT);
pinMode(y2Pin, OUTPUT);
pinMode(bPlusPin, INPUT);
pinMode(bMinusPin, INPUT);
pinMode(startButtonPin, INPUT);
pinMode(stopButtonPin, INPUT);
// Initial state for motor control
digitalWrite(motorRSPin, LOW);
digitalWrite(motorLSPin, LOW);
digitalWrite(y1Pin, LOW);
digitalWrite(vacPin, LOW);
digitalWrite(y2Pin, LOW);
// Serial communication for debugging (optional)
Serial.begin(9600);
}
void loop()
{
// Define AM switch state (Manual=1, Auto=0)
int AM = digitalRead(AMswitch);
// Check the state of buttons and reed switches
int FWD = digitalRead(fwdButtonPin);
int REV = digitalRead(revButtonPin);
int LHS = digitalRead(lhsSwitchPin);
int RHS = digitalRead(rhsSwitchPin);
int Start = digitalRead(startButtonPin);
int Stop = digitalRead(stopButtonPin);
int Aplus = digitalRead(aPlusPin);
int Aminus = digitalRead(aMinusPin);
int Bplus = digitalRead(bPlusPin);
int Bminus = digitalRead(bMinusPin);
// Choose the mode based on the state of AM
if (AM == 1) {
AutoMode(); // Call the function for Automatic Mode
} else if (AM == 0) {
ManualMode(); // Call the function for Manual Mode
}
// Add a small delay to prevent rapid state changes
delay(100);
// Print state for debugging (optional)
Serial.print(" Mode: "); Serial.print(AM);
Serial.print(" |Inputs:");
Serial.print(Aplus);
Serial.print(Aminus);
Serial.print(Bplus);
Serial.print(Bminus);
Serial.print(" |Outputs:");
delay(100);
}
void AutoMode() // Commands for Automatic Mode (AM == 1)
{
// Check the state of buttons and reed switches
int FWD = digitalRead(fwdButtonPin);
int REV = digitalRead(revButtonPin);
int LHS = digitalRead(lhsSwitchPin);
int RHS = digitalRead(rhsSwitchPin);
int Start = digitalRead(startButtonPin);
int Stop = digitalRead(stopButtonPin);
int Aplus = digitalRead(aPlusPin);
int Aminus = digitalRead(aMinusPin);
int Bplus = digitalRead(bPlusPin);
int Bminus = digitalRead(bMinusPin);
digitalWrite(FWD,0);
digitalWrite(REV,0);
if (Start == 1) {
// If START is pressed, move the car automatically to the LHS
digitalWrite(motorRSPin, 0);
digitalWrite(motorLSPin, 1);
} else if (Stop == 1 || LHS == 1 || RHS == 1) {
// If STOP is pressed, pause the system
digitalWrite(motorRSPin, 0);
digitalWrite(motorLSPin, 0);
}
// Pick-and-Place Sequence based on the states of switches
if (LHS == 1) {
// Step 1: Extend Y1
digitalWrite(y1Pin, 1);
}
else if (Aplus == 1) {
// Step 2: Trigger A+, Turn ON VAC, then Retract Y1
delay(1000);
digitalWrite(vacPin, 1);
delay(2000);
digitalWrite(y1Pin, 0);
}
else if (Aminus == 1) {
// Step 3: Trigger A-, activate Y2
digitalWrite(y2Pin, 1);
}
else if (Bplus == 1) {
// Step 4: Trigger B+, Turn OFF VAC, Retract Y2
digitalWrite(vacPin, 0);
delay(1000);
digitalWrite(y2Pin, 0);
}
else if (Bminus == 1) {
// Step 5: Trigger B-, move cart to RHS
digitalWrite(motorRSPin, 1);
digitalWrite(motorLSPin, 0);
}
if (Stop == 1) {
digitalWrite(Aplus&&Aminus&&Bplus&&Bminus,0);
}
}
void ManualMode() // Commands for Automatic Mode (AM == 0)
{
// Check the state of buttons and reed switches
int FWD = digitalRead(fwdButtonPin);
int REV = digitalRead(revButtonPin);
int LHS = digitalRead(lhsSwitchPin);
int RHS = digitalRead(rhsSwitchPin);
int Start = digitalRead(startButtonPin);
int Stop = digitalRead(stopButtonPin);
int Aplus = digitalRead(aPlusPin);
int Aminus = digitalRead(aMinusPin);
int Bplus = digitalRead(bPlusPin);
int Bminus = digitalRead(bMinusPin);
// Commands for Manual Mode (AM == 0)
// Pick-and-Place Sequence based on the states of switches
if (LHS == 1) {
// Step 1: Extend Y1
digitalWrite(y1Pin, 1);
}
else if (Aplus == 1) {
// Step 2: Trigger A+, Turn ON VAC, then Retract Y1
delay(1000);
digitalWrite(vacPin, 1);
delay(2000);
digitalWrite(y1Pin, 0);
}
else if (Aminus == 1) {
// Step 3: Trigger A-, activate Y2
digitalWrite(y2Pin, HIGH);
}
else if (Bplus == 1) {
// Step 4: Trigger B+, Turn OFF VAC, Retract Y2
digitalWrite(vacPin, 0);
delay(1000);
digitalWrite(y2Pin, 0);
}
if (Stop == 1) {
digitalWrite(Aplus&&Aminus&&Bplus&&Bminus,0);
}
}
I/Os (kinda long, but please bear with me)
For context, this project is a "Pneumatic Pick-and-Place System" with 4 outputs:
- 2 cylinders that extend/retract if air is applied/removed = 8 & 11
- 1 vacuum to suck/release the object= A5
- 1 DC Motor (w/ H-bridge) on a conveyor that carries an object = 6 & 7 for motor pins direction
And 11 inputs:
- Forward = 2
- Reverse = 3
- LHS (left-hand side) = 4
- RHS (right-hand side) = 5
- Reed Switches (A+, A-, B+,B-) = 9,10,12,13
If cylinderA/B is extended = A+/B+
If cylinderA/B is retracted = A-/B-
these reed switches act as sensors to continue the sequence - Start = A4
- Stop = A3
- Auto/Manual (AM) Switch = A2
Heres a more comprehensive step-by-step process of my project
P-n-P Project Description.pdf (167.8 KB)
Regarding the PDF, I have already accomplished Sections B, C, and D. I can do the necessary commands whether I am in AUTO mode or MANUAL mode as shown in the code.
The parts that don't get how to do are Section A and the additional conditions shown before the Illusion (Stop Conditions), specifically:
- Pressing STOP should pause the pick-and-place sequence in Section D when I pressed it.
- Pressing STOP Twice should initialize the entire system completely.
- Pressing START after pausing should continue the sequence.
- Switching from Manual mode to Auto mode should also initialize the entire system completely.
Help would be very much appreciated. Thanks
