pick and place using arduino

hello,
i really need a help to configure the code for pick and place system using arduino. i already try plclib arduino but no really support because my input too much. i have pick and place system using three cylinder and 6 reedswitch smc. i want to ask how can i hold the signal reedswitch, if the cylinder moving reedswitch will turn of and arduino cannot determain the position that it stop.

here are my coding.

// Constants for pneumatic valves
const int cylinderA = 2;
const int cylinderB = 3;
const int cylinderC = 4;
//const int cylinderD = 5;
// ----------------------------------------
// Constants for sensors
const int reedswitchA1 = 6;
const int reedswitchA2 = 7;
const int reedswitchB1 = 8;
const int reedswitchB2 = 9;
const int reedswitchC1 = 10;
const int reedswitchC2 = 11;
//const int reedswitchD1 = 12;
//const int reedswitchD2 = 13;

void setup() {
// Pneumatic cylinders set as output
pinMode(cylinderA, OUTPUT);
pinMode(cylinderB, OUTPUT);
pinMode(cylinderC, OUTPUT);

// Sensors set as input
pinMode(reedswitchA1, INPUT);
pinMode(reedswitchA2, INPUT);
pinMode(reedswitchB1, INPUT);
pinMode(reedswitchB2, INPUT);
pinMode(reedswitchC1, INPUT);
pinMode(reedswitchC2, INPUT);

// Set pneumatic cylinders as they are retracted
digitalWrite(cylinderA, LOW);
digitalWrite(cylinderB, LOW);
digitalWrite(cylinderC, LOW);

}

void loop() {

if(digitalRead(reedswitchA1) == HIGH || digitalRead(reedswitchB1) == HIGH || digitalRead(reedswitchC1) == HIGH)
{
digitalWrite(cylinderA, LOW); // extend the first pneumatic cylinder to grap the workpice
}
else { //if not pressed
digitalWrite(cylinderA, LOW);
}

if(digitalRead(reedswitchA2) == LOW && digitalRead(reedswitchB1) == LOW)
{
digitalWrite(cylinderB, LOW); // Gripper will clamp workpiece

}

if(digitalRead(reedswitchB2) == LOW && digitalRead(reedswitchA2) == LOW )
{

digitalWrite(cylinderA, LOW); // retract first cylinder before go 90 degree
}

if(digitalRead(reedswitchB2) == LOW && digitalRead(reedswitchA1) == LOW && digitalRead(reedswitchC1) == LOW)
{
digitalWrite(cylinderC, LOW); // rotary cylinder go to 90 degree
}

if(digitalRead(reedswitchC2) == LOW && digitalRead(reedswitchA1) == LOW && digitalRead(reedswitchB2) == LOW)
{
digitalWrite(cylinderA, LOW); // cylinder first will extand to put the workpiece
}
if(digitalRead(reedswitchC2) == LOW && digitalRead(reedswitchB2) == LOW && digitalRead(reedswitchA2) == LOW)
{
digitalWrite(cylinderB,LOW); // gripper release the workpiece
}
if(digitalRead(reedswitchC2) == LOW && digitalRead(reedswitchB1) == LOW && digitalRead(reedswitchA2) == LOW)
{
digitalWrite(cylinderA, LOW); // Retract cylinder A
}
if (digitalRead(reedswitchC2) == LOW && digitalRead(reedswitchB1) == LOW && digitalRead(reedswitchA1) == LOW)
{
digitalWrite(cylinderC, LOW); // arm rotate 0 degree
}

}

cylinder_robot.ino (2.68 KB)

i want to ask how can i hold the signal reedswitch

You can't. A switch is an input device. You can not force the input to be a specific value.

It hardly seems likely that that is really what you want to do. Perhaps you would do well to explain what you want to do, rather than discuss how you think you should do it.

cylinderA, cylinderB, and cylinderC are lousy names for pins. Variables that hold pin numbers should have Pin somewhere in the name, to make it clear that the value IS a pin number. The cylinders serve specific purposes. The variable names should reflect the purpose, not the type of device.

A picture of your hardware, showing the cylinders and switches would be useful. It seems that you need to be using some while loops, to move a cylinder until it triggers a switch, rather than just a bunch of if statements.

Dear Paul.

thanks for your reply. here my problem is to maintain the output arduino that i set for pin 13, 12 and 11.
if i want to set the output pin maintain high until another sensor turn on to reset the output pin.

example

if (digitalRead (3,HIGH)) {
digitalWrite(13, HIGH) // How to maintain the pin still HIGH so when i off the sensor at pin 3 the output pin 13 need to be HIGH. when i on sensor at pin 4 the output pin 3 will turn off.
}
if(digitalRead(4,HIGH){
digitalWrite (13, LOW)
}

thanks

if (digitalRead(3) == HIGH) || pin3HasBeenHigh == true)
{
  digitalWrite(13, HIGH);
  pin3HasBeenHigh = true;
}

if (digitalRead(4) == HIGH)
{
  digitalWrite (13, LOW);
  pin3HAsBeenHigh = false;
}

If a pin is set for INPUT (so it can be used with digitalRead() ) then when you use digitalWrite(pin, HIGH) it has the same effect as pinMode(pin, INPUT_PULLUP). It does not set the pin HIGH.

If you want to set a pin HIGH you must change its mode to OUTPUT first. If you do that you must, of course, ensure that whatever the pin is connected to cannot draw more than about 20mA from the pin.

However, as @PaulS has said it seems a very strange requirement to want to make an input pin - the signal from a switch - hold a state that is the opposite of the switch state.

My guess is that your concept for your program is faulty.

For example you can use a variable to record the state of the switch and then check the variable rather than re-read the switch. That way the variable will still be HIGH even if the actual switch has gone back to LOW.

...R

Dear UKHeliBob

// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin 2
const int buttonPin2 = 3; // the number of the pushbutton pin 3
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int pin3HasBeenHigh = 0;
int state1;
int state2;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
}

void loop(){
state2 = digitalRead(buttonPin2);
state1 = digitalRead(buttonPin1);

if (state2 == HIGH) || pin3HasBeenHigh == true)
{
digitalWrite(ledPin, HIGH);
pin3HasBeenHigh = true;
}

if (state1 == HIGH)
{
digitalWrite (ledPin, LOW);
pin3HAsBeenHigh = false;
}
}

it's that correct i wrote the program because i cannot compile the code have error at ||.

@saripdol, please modify your posts and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

if (state2 == HIGH) || pin3HasBeenHigh == true)Check the number and position of the braces in this line

    pin3HAsBeenHigh = false;Check the name of the variable.

Thanks UKHeliBob..

some other question. can && operator combine with || operator in if statement.

example

int state1;
int state2;
int state3;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
void loop(){
state3 = digitalRead(buttonPin3);
state2 = digitalRead(buttonPin2);
state1 = digitalRead(buttonPin1);
if (state1 == HIGH || pin1HasBeenHigh == true && state3 == HIGH)
{
digitalWrite(ledPin1, HIGH);
pin1HasBeenHigh = true;
}

if (state1 == HIGH )
{
digitalWrite (ledPin1, LOW);
pin1HasBeenHigh = false;
}

}

some other question. can && operator combine with || operator in if statement.

You COULD have written some code to test that yourself. Of course they can.

dear paul

i already write the program with && and || operator but cannot compile.

if (state1 == HIGH || pin1HasBeenHigh == true && state3 == HIGH)

like this. can you show me the right way to write the program

i already write the program with && and || operator but cannot compile.

Sure you can. All you need to do is click on the Verify icon. Now, the compiler might complain about your code, AND tell you which line it is it doesn't like, and what it is about the line that it does not like.

If you share that information, AND ALL of your code, then we can help you understand what you are doing wrong.

Sometimes, it is necessary to use parentheses. Often, in fact...

thanks paul. it really work

hello all.

i want to ask if the if statement can works with stepper motor library. because my project need to works air cylinder and stepper motor. the stepper motor will be mount at the end cylinder. when cylinder full extend the reed sensor will turn on and give signal to arduino to on stepper motor. the stepper motor will turn on and rotate 3 revolution and stop. the cylinder will return back. can it be make using one arduino?

i want to ask if the if statement can works with stepper motor library.

Of course.

can it be make using one arduino?

Easily.

dear paul,

here i attach my code. i want run stepper motor when the cylinder 1 extend.

const int ledPin1 = 13; // solenoid 1 for cylinder
const int ledPin2 = 12; // solenoid for gripper
const int ledPin3 = 11; // solenoid for rottate cylinder
const int buttonPin1 = 2; // cylinder 1 reed switch A
const int buttonPin2 = 3; // cylinder 1 reed switch B
const int buttonPin3 = 4; // cylinder 2 reed switch A
const int buttonPin4 = 5; // cylinder 2 reed switch B
const int buttonPin5 = 6; // cylinder 3 reed switch A
const int buttonPin6 = 7; // cylinder 3 reed switch B
const int buttonPin7 = 8; // push start button
const int buttonPin8 = 9; // stop button

int state1;
int state2;
int state3;
int state4;
int state5;
int state6;
int state7;
int state8;
int pin3HasBeenHigh = 0;
int pin2HasBeenHigh = 0;
int pin1HasBeenHigh = 0;
int pin1AHasBeenHigh = 0;
int pin2AHasBeenHigh = 0;
int pin3AHasBeenHigh = 0;
void setup() {
// initialize the LED pin as an output:

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(buttonPin7, INPUT);
}
void loop(){

state1 = digitalRead(buttonPin1); // read close gripper
state2 = digitalRead(buttonPin2); // read open gripper
state3 = digitalRead(buttonPin3); // read extand cylinder
state4 = digitalRead(buttonPin4); // read cylinder retract
state5 = digitalRead(buttonPin5); // read rotary actuator 0 degree
state6 = digitalRead(buttonPin6); // read when rotate cylinder 90 degree
state7 = digitalRead(buttonPin7); // read signal start push button
state8 = digitalRead(buttonPin8); // read signal stop button

if (state7 == HIGH && state1 == HIGH && state4 == HIGH && state5 == HIGH || pin1HasBeenHigh == true )
{
digitalWrite(ledPin1, HIGH); // extand cylinder 1
pin1HasBeenHigh = true;
}
if (state3 == HIGH && state1 == HIGH && state5 == HIGH || pin2HasBeenHigh == true )
{
digitalWrite(ledPin2, HIGH); // open gripper
pin2HasBeenHigh = true;
}
if (state2 == HIGH && state3 == HIGH && state5 == HIGH || state8 == HIGH)
{
delay(500);
digitalWrite (ledPin1, LOW); // retract cylinder 1
pin1HasBeenHigh = false;
}
if (state2 == HIGH && state4 == HIGH && state5 == HIGH && pin1HasBeenHigh == false || pin3HasBeenHigh == true)
{
digitalWrite(ledPin3, HIGH); // rotate 90 degree
pin3HasBeenHigh = true;
}
if (state6 == HIGH && state4 == HIGH && state2 == HIGH || pin1AHasBeenHigh == true)
{
digitalWrite(ledPin1, HIGH); //extand cylinder 1

pin1AHasBeenHigh = true;
pin3HasBeenHigh = true;
}

i want run motor between this if statement so the motor stop and the gripper will close
if (state3 == HIGH && state2 == HIGH && state6 == HIGH )
{
delay(500);
digitalWrite(ledPin2,LOW); // close gripper
pin2HasBeenHigh = false;
pin3HasBeenHigh = true;
}
if (state6 == HIGH && state1 == HIGH && state3 == HIGH)
{
delay(500);
digitalWrite(ledPin1, LOW); // retract cylinder 1
pin1AHasBeenHigh = false;
pin3HasBeenHigh = true;
}
if (state6 == HIGH && state4 == HIGH && state1 == HIGH )
{
digitalWrite(ledPin3, LOW); // rotate origin
pin3HasBeenHigh = false;
}
}

can you help me with the code

can you help me with the code

To run the stepper motor? No. Your code makes little sense.

The name, buttonPin1, for instance, means NOTHING. If that pin has a switch connected to it that, when pressed, means that the gripper should be closed, then naming the variable closeGripperPin, and the value closeGripperState, makes the code MUCH easier to understand.

Running a stepper motor does not make sense, either. "Complete 4 revolutions" makes sense. "Step until the xxx limit switch has become pressed" makes sense. "Run the stepper" does not.

What, exactly, is it that "run the stepper" means to you?

What, exactly, is it that "run the stepper" means to you?

i mean rotate the stepper for 4 complete revolution. now i already reach the full io pin at arduino. i think i cannot use the stepper motor because no pin to attach. can i used analog pin to do the control for stepper?

@saripdol, Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum This is the second time of asking.

...R
Stepper Motor Basics
Simple Stepper Code

can i used analog pin to do the control for stepper?

Analog pins also serve as digital pins, so, yes, you can.