NEED HELP WITH DC MOTOR LIMIT SWITCH CODE

So i am trying to control 3 dc motors with relays and limit switches ,but something always gone wrong ,
i’ve tryed several diferent codes but … nothing.
Here my code for now:

int const S3Pin = 13;
int const K3Pin = 2;
int const S1Pin = 12;
int const K2Pin = 3;
int const S2Pin = 11;
int const K1Pin = 4;
int const S4Pin = 10;
int const K2rpin = 5;// relay output for reverse direction
int const K3rpin = 6; //relay output for reverse direction
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(S3Pin, INPUT);
  pinMode(K3Pin, OUTPUT);
  pinMode(S1Pin, INPUT);
  pinMode(K1Pin, OUTPUT);
  pinMode(S2Pin, INPUT);
  pinMode(K2Pin, OUTPUT);
  pinMode(K2rpin, OUTPUT);//relay output for reverse direction
  pinMode(K3rpin, OUTPUT);//relay output for reverse direction
  pinMode(S4Pin, INPUT);
  digitalWrite(K1Pin,LOW);
  digitalWrite(K2rpin,LOW);
  digitalWrite(K2Pin,LOW);
  digitalWrite(K3rpin,LOW);
  if (digitalRead(S3Pin) == LOW) {   //this lines i hope to find Zero Point of machine
    digitalWrite(K3Pin, HIGH);      //this lines i hope to find Zero Point of machine
  }
  while (digitalRead(S3Pin) == LOW)  //this lines i hope to find Zero Point of machine
  {
  }
  while(1);
  digitalWrite(K3Pin, LOW);   //this lines i hope to find Zero Point of machine

}


void loop() {
  if(digitalRead(S1Pin)==HIGH and (S3Pin)==LOW)
  {digitalWrite(K2Pin, HIGH);
  }
  
  
  while(digitalRead(S3Pin)==LOW)
  {
  }
  digitalWrite(K2Pin, LOW);
  delay(500);
  digitalWrite(K3Pin, HIGH);
 while(digitalRead(S4Pin)==LOW)
 {
 }
 digitalWrite(K3Pin, LOW);
  digitalWrite(K2rpin,HIGH);
  delay(15000);
  digitalWrite(K2rpin,HIGH);
  digitalWrite(K3rpin,HIGH);
  while(digitalRead(S3Pin)==LOW)
  {
    }
  digitalWrite(K3rpin, LOW);
  while(digitalRead(S1Pin)==HIGH and (S3Pin)==LOW)
  {digitalWrite(K1Pin,LOW);
   }
   delay(100);
   loop;
  }

So can someone tell me where i gone wrong i help me build it .

Why haven't you got any debug prints?

I am new in Programing and today is my first attemp to do somethink like this so i have no idea what debug print is .Can you please explane or change a litle bit of my code i will understand it and do the rest :slight_smile: Thanks .

A debug print is a Serial.print that indicates a value or program branch within your code.

So can you explain me plase how exactly i should use this command , i am sorry but i am begginer .
If you can change a little bit of my code to see how to use it in code i will do the rest . Thanks :slight_smile:

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);  // other speeds are available, and recommended
  pinMode(S3Pin, INPUT);
  pinMode(K3Pin, OUTPUT)

Then, anywhere you want to see a value, or check which branch of an "if" you took, put a Serial.print (F("The value of x is ")); Serial.println (x);
This stuff is all explained in the examples built into the IDE

BTW this while(1); is going to stop EVERYTHING.

,but something always gone wrong

or its more usual counterpart

it doesn't work

is just about one of the most useless things you can post here.

You need to tell us what happened that you didn't expect and what didn't happen that you did expect.

void loop() {
  int S3State = digitalRead(S3Pin)
  int S1State = digitalRead(S1Pin)
  Serial.print(S1State)
  while(digitalRead(S1Pin)==LOW)
  {
  }
  Serial.print(S3State)and(S1State)
  if(digitalRead(S1Pin)==HIGH and (S3Pin)==LOW)
  {digitalWrite(K2Pin, HIGH);
  }

So is this right?
I am sorry for that i dont explain what happends,but i was thinking that my code is one big mystake :D.
So in void setup that i wrote expect to when S3Pin gone HIGH K3 stops and this must do it only once
but i does it always.
in void loop () when it wait for
"S" to be active to give power ti "K" OUTPUTS and i dont know why , what am i missing?

Hi,
I think we need to see how you have you project wired up, can you post a picture of your project and can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Also explain what you want the sketch to do and what it isn't doing.

Thanks.. Tom... :slight_smile:

Here is my machine MACHINE
K1 K2 K3 is conveyors (DC MOTORS) and S1 S2 S3 S4 is Switches (to indicate when PCBs is on place :))
here is my code :

int const S3Pin = 10;
int const S5Pin = 12;
int const K3Pin = 5;
int const S1Pin = 8;
int const K2Pin = 3;
int const S2Pin = 9;
int const K1Pin = 2;
int const S4Pin = 11;
int const K2rpin = 4;// relay output for reverse direction
int const K3rpin = 6; //relay output for reverse direction
void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  pinMode(S5Pin, INPUT);
  pinMode(S3Pin, INPUT);
  pinMode(K3Pin, OUTPUT);
  pinMode(S1Pin, INPUT);
  pinMode(K1Pin, OUTPUT);
  pinMode(S2Pin, INPUT);
  pinMode(K2Pin, OUTPUT);
  pinMode(K2rpin, OUTPUT);//relay output for reverse direction
  pinMode(K3rpin, OUTPUT);//relay output for reverse direction
  pinMode(S4Pin, INPUT);
  delay(10000);
  digitalWrite(K1Pin, HIGH);
  digitalWrite(K2rpin, LOW);
  digitalWrite(K2Pin, LOW);
  digitalWrite(K3rpin, LOW);
  digitalWrite(K3Pin, HIGH);
  while (digitalRead(S3Pin) == LOW); //this lines i hope to find Zero Point of machine
  {
  };
  digitalWrite(K3Pin, LOW);    //this lines i hope to find Zero Point of machine
}
void loop() {
  
  while (digitalRead(S1Pin) == LOW);
  {
  };
  digitalWrite(K2Pin, HIGH);
  while (digitalRead(S2Pin) == LOW);
  {
  };
  digitalWrite(K2Pin, LOW);
  digitalWrite(K3rpin, HIGH);
  while (digitalRead(S4Pin) == LOW);
  {
  };
  digitalWrite(K3rpin, LOW);
  delay(500);
  digitalWrite(K2rpin, HIGH);
  while (digitalRead(S5Pin) == LOW);
  {
  };
  digitalWrite(K2rpin, LOW);
  delay (500);
  digitalWrite(K3Pin, HIGH);
  while (digitalRead(S3Pin) == LOW);
  {
  };
  digitalWrite(K3Pin, LOW);
  delay(500);
}

and everything works now :slight_smile: ,except one -
I need to do when S1 is active and S3 in Not Active K1 stops , but this must be always cheked no meter where the program is , and not to stop void loop(), the scketch must always cheking this situation ,but also continue doing the code , i tryed with

while (digitalRead(S1Pin)==HIGH and digitalRead(S3Pin)==LOW)
{digitalWrite(K1Pin,LOW);
}

but i doesnt work ... K1 is still active no meter S1Pin and S3Pin
so i guess i need some loop in void setup, but loop only for this function .

Thanks for help. :slight_smile:

 while(digitalRead(S1Pin)==LOW);
  {
  }
  if(digitalRead(S1Pin)==HIGH)

The only way you can get to the line starting with the “if” is if the condtion on S1Pin is not LOW - there is no need to test to see if it’s HIGH

Thanks :slight_smile: i will change it :slight_smile: But for other my question , can you tell me how to do it ?:slight_smile:

I found how to do it :
i

nt const S3Pin = 10;
int const S5Pin = 12;
int const K3Pin = 5;
int const S1Pin = 8;
int const K2Pin = 3;
int const S2Pin = 9;
int const K1Pin = 2;
int const S4Pin = 11;
int const K2rpin = 4;// relay output for reverse direction
int const K3rpin = 6; //relay output for reverse direction
void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  pinMode(S5Pin, INPUT);
  pinMode(S3Pin, INPUT);
  pinMode(K3Pin, OUTPUT);
  pinMode(S1Pin, INPUT);
  pinMode(K1Pin, OUTPUT);
  pinMode(S2Pin, INPUT);
  pinMode(K2Pin, OUTPUT);
  pinMode(K2rpin, OUTPUT);//relay output for reverse direction
  pinMode(K3rpin, OUTPUT);//relay output for reverse direction
  pinMode(S4Pin, INPUT);
  delay(10000);
  digitalWrite(K1Pin, HIGH);
  digitalWrite(K2rpin, LOW);
  digitalWrite(K2Pin, LOW);
  digitalWrite(K3rpin, LOW);
  digitalWrite(K3Pin, HIGH);
  while (digitalRead(S3Pin) == LOW); //this lines i hope to find Zero Point of machine
  {
  };
  digitalWrite(K3Pin, LOW);    //this lines i hope to find Zero Point of machine
    
  
      } 
void loop() {
  

  while (digitalRead(S1Pin) == LOW);
  {
  };
  digitalWrite(K2Pin, HIGH);
  while (digitalRead(S2Pin) == LOW)
  {if(digitalRead(S1Pin)==HIGH&&digitalRead(S3Pin)==LOW){
    digitalWrite(K1Pin,LOW);}
    else{}
  }
  ;
  digitalWrite(K2Pin, LOW);
  digitalWrite(K3rpin, HIGH);
  while (digitalRead(S4Pin) == LOW)
  {if(digitalRead(S1Pin)==HIGH&&digitalRead(S3Pin)==LOW){
    digitalWrite(K1Pin,LOW);}
    else{}
  }
  
  digitalWrite(K3rpin, LOW);
  delay(500);
  digitalWrite(K2rpin, HIGH);
  while (digitalRead(S5Pin) == LOW)
  {if(digitalRead(S1Pin)==HIGH&&digitalRead(S3Pin)==LOW){
    digitalWrite(K1Pin,LOW);}
    else{}
  }
  digitalWrite(K2rpin, LOW);
  delay (500);
  digitalWrite(K3Pin, HIGH);
  while (digitalRead(S3Pin) == LOW){if(digitalRead(S1Pin)==HIGH&&digitalRead(S3Pin)==LOW){
    digitalWrite(K1Pin,LOW);}
    else{}
  }
  digitalWrite(K3Pin, LOW);
  if (digitalRead(S3Pin)==HIGH)
         {digitalWrite(K1Pin,HIGH);
  delay(500);
         }
  
}

:slight_smile:

Hi,
Thanks for the diagram, but we need a circuit diagram, showing how you have connected the limit switches and the motors and how you are supplying power to the Arduino and controlling the motors.

Thanks Tom... :slight_smile:
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please attach it to your post, google drive is hopeless..Won't zoom properly, and I'm running Chrome.

Monday i will upload the curcuit diagram . :slight_smile: