Stopping a motor on a microswitch.

Hello everyone,

I am programming a car roof that can open and close.

I want my motors to stop on a microswitch.

This is my code:

int knop1 = 11; //Button 1 roof open.
int knop2 = 12; //Button 2 roof close.
int knop3 = 13; //Button 3 sunshade close.
int knop4 = 19; //Button 4 sunshade open.

int M1L = 35; //Motor roof open.
int M1R = 36; //Motor roof close.
int M2L = 37; //Motor sunshade open.
int M2R = 38; //Motor sunshade close.

int sensor1 = 34; //sensor, roof open. (microswitch)
int sensor2 = 33; //sensor, roof close. (microswitch)
int sensor3 = 32; //Sensor, sunshade open. (microswitch)
int sensor4 = 31; //Sensor, sunshade close. (microswitch)

int buttonStatus1 = 0;
int buttonStatus2 = 0;
int buttonStatus3 = 0;
int buttonStatus4 = 0;

void setup ()
{
pinMode (knop1, INPUT);
pinMode (knop2, INPUT);
pinMode (knop3, INPUT);
pinMode (knop4, INPUT);

pinMode (M1L, OUTPUT);
pinMode (M1R, OUTPUT);
pinMode (M2L, OUTPUT);
pinMode (M2R, OUTPUT);

pinMode (sensor1, INPUT);
pinMode (sensor2, INPUT);
pinMode (sensor3, INPUT);
pinMode (sensor4, INPUT);

digitalWrite(knop1, LOW);
digitalWrite(knop2, LOW);
digitalWrite(knop3, LOW);
digitalWrite(knop4, LOW);

digitalWrite(sensor1, LOW);
digitalWrite(sensor2, LOW);
digitalWrite(sensor3, LOW);
digitalWrite(sensor4, LOW);

Serial.begin(9600);

}

void loop()
{
buttonStatus1 = digitalRead(knop1);
buttonStatus2 = digitalRead(knop2);
buttonStatus3 = digitalRead(knop3);
buttonStatus4 = digitalRead(knop4);

{ if (buttonStatus1 == HIGH && buttonStatus2 == LOW && buttonStatus3 == LOW && buttonStatus4 == LOW) //Dak open.
do
{
digitalWrite(M1L, HIGH);
digitalWrite(M1R, LOW);
}while(digitalRead (sensor2 == LOW));
}
{ if (buttonStatus1 == LOW && buttonStatus2 == HIGH && buttonStatus3 == LOW && buttonStatus4 == LOW) //Dak dicht.
do
{
digitalWrite(M1L, LOW);
digitalWrite(M1R, HIGH);
}while(digitalRead (sensor1 == LOW));
}
{ if (buttonStatus1 == LOW && buttonStatus2 == LOW && buttonStatus3 == HIGH && buttonStatus4 == LOW) //Zonnenscherm open.
do
{
digitalWrite(M2L, HIGH);
digitalWrite(M2R, LOW);
}while(digitalRead (sensor4 == LOW));
}
{ if (buttonStatus1 == LOW && buttonStatus2 == LOW && buttonStatus3 == LOW && buttonStatus4 == HIGH) //Zonnenscherm dicht.
do
{
digitalWrite(M2L, LOW);
digitalWrite(M2R, HIGH);
}while(digitalRead (sensor3 == LOW));
}
}

Thanks,
Siem

siemvanharen:
Hello everyone,

I am programming a car roof that can open and close.

I want my motors to stop on a microswitch.

You have not told us what the program actually does or what you want it to do that is different.

To make it easy for people to help you 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

Although I have not been able to study your code carefully I see that you have some blocking WHILE loops and they may well prevent the program from being responsive - if that is the problem.

…R

Thanks for the fast response!

The roof and the sunshade have to get closed and opend.
The microswitches are the locations where the roof and sunshade have to stop moving.
Right now the roof keeps moving.
The roof and the sunshade are getting started moving with a push of de button "close" or "open".

The buttons work and the roof and sunshade are moving but they are not stopping at the microswitches like they need to be.

int knop1 = 11;     //Button 1 roof open.
int knop2 = 12;     //Button 2 roof close.
int knop3 = 13;     //Button 3 sunshade close.
int knop4 = 19;     //Button 4 sunshade open.

int M1L = 35;       //Motor roof open.
int M1R = 36;       //Motor roof close.
int M2L = 37;       //Motor sunshade open.
int M2R = 38;       //Motor sunshade close.

int sensor1 = 34;   //sensor, roof open. (microswitch) 
int sensor2 = 33;   //sensor, roof close. (microswitch)
int sensor3 = 32;   //Sensor, sunshade open. (microswitch)
int sensor4 = 31;   //Sensor, sunshade close. (microswitch)

int buttonStatus1 = 0;
int buttonStatus2 = 0;
int buttonStatus3 = 0;
int buttonStatus4 = 0;

void setup ()
{
pinMode (knop1, INPUT);
pinMode (knop2, INPUT);
pinMode (knop3, INPUT);
pinMode (knop4, INPUT);

pinMode (M1L, OUTPUT);
pinMode (M1R, OUTPUT);
pinMode (M2L, OUTPUT);
pinMode (M2R, OUTPUT);

pinMode (sensor1, INPUT);
pinMode (sensor2, INPUT);
pinMode (sensor3, INPUT);
pinMode (sensor4, INPUT);

digitalWrite(knop1, LOW);
digitalWrite(knop2, LOW);
digitalWrite(knop3, LOW);
digitalWrite(knop4, LOW);

digitalWrite(sensor1, LOW);
digitalWrite(sensor2, LOW);
digitalWrite(sensor3, LOW);
digitalWrite(sensor4, LOW);

Serial.begin(9600);

}

void loop()
{
buttonStatus1 = digitalRead(knop1); 
buttonStatus2 = digitalRead(knop2);
buttonStatus3 = digitalRead(knop3);
buttonStatus4 = digitalRead(knop4);

{ if (buttonStatus1 == HIGH && buttonStatus2 == LOW && buttonStatus3 == LOW && buttonStatus4 == LOW) //Dak open.
  do
  {
  digitalWrite(M1L, HIGH); 
  digitalWrite(M1R, LOW);
  }while(digitalRead (sensor2 == LOW)); 
}  
{ if (buttonStatus1 == LOW && buttonStatus2 == HIGH && buttonStatus3 == LOW && buttonStatus4 == LOW) //Dak dicht.
  do
  {
  digitalWrite(M1L, LOW); 
  digitalWrite(M1R, HIGH);
  }while(digitalRead (sensor1 == LOW));
}
{ if (buttonStatus1 == LOW && buttonStatus2 == LOW && buttonStatus3 == HIGH && buttonStatus4 == LOW) //Zonnenscherm open.
  do
  {
  digitalWrite(M2L, HIGH); 
  digitalWrite(M2R, LOW);
  }while(digitalRead (sensor4 == LOW));
}
{ if (buttonStatus1 == LOW && buttonStatus2 == LOW && buttonStatus3 == LOW && buttonStatus4 == HIGH) //Zonnenscherm dicht.
  do
  {
  digitalWrite(M2L, LOW); 
  digitalWrite(M2R, HIGH);
  }while(digitalRead (sensor3 == LOW));
}
}

thanks!
Siem

Syntax

digitalRead(pin)

Parameters

pin

: the number of the digital pin you want to read

digitalRead (sensor2 == LOW) ???

I tried but it didn't worked.

siemvanharen:
I tried but it didn’t worked.

You tried what?
Your code has several ‘while’ statements in which you have incorrect calls to digitalRead().
I also suggest you include print statements in your code so that you can simply press each microswitch and see if it is being correctly read.

IMHO you should change the whole style of the program to one which takes account of the state of the system. I reckon that will make the coding much simpler.

I suspect the states you need are Shut, Opening, WideOpen and Closing - they can be recorded in a variable as 'S' 'O' 'W' and 'C' or you could use an enum

Then your code can be something like

if (roofState == 'C') {
   if (closeSwitchState == LOW) { // assumes LOW means pressed
     // code to stop the motor
     roofState = 'S';
    }
}

and similar for the other states.

Do NOT use WHILE if you want a responsive system

...R

Thanks!
how do i need to add the roofstates?

siemvanharen:
Thanks!
how do i need to add the roofstates?

Sit back with a pen and paper and think things through.
Do the roof alone first then add the sunshade later.
You will find truth tables very helpful for this type of sketch.
For example you have two roof sensors

int sensor1 = 34;   //sensor, roof open. (microswitch) 
int sensor2 = 33;   //sensor, roof close. (microswitch)

sensor1sensor2
comment

low
low
roof may be opening or closing, may be a fault if too long in this state

low
high
roof is closed

high
low
roof is opened

high
high
fault

You will know if the roof is opening or closing depending on what control button was pressed.
You will probably want a timer to check the roof has not got stuck or a switch has broken while the roof is opening or closing.

siemvanharen:
Thanks!
how do i need to add the roofstates?

Just add it like you would any variable - something like

char roofState = 'X';

I have put ‘X’ into it because at the time of creating the variable the program won’t know the actual state of the roof. You will need to write some code in setup() to figure that out

…R

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your microswitches wired, do you have pullup or pulldown resistors?

Thanks.. Tom.. :slight_smile:

I am new to c language.
In the program i wrote, all the switches for opening and closing the roof and sunshade worked.
Is there a easy way to stop de motors on the limit switches?

Your best bet is to hardwire the limit switches into the motor circuit , so the switch itself cuts the power to the motor , adding the software etc makes it overly complex and less reliable

siemvanharen:
I am new to c language.
In the program i wrote, all the switches for opening and closing the roof and sunshade worked.
Is there a easy way to stop de motors on the limit switches?

Unless you do what @hammy has suggested I can't think of an easier way than what I already suggested. Yes it takes a little bit of learning, but isn't that part of the fun?

And, actually, @hammy's suggestion is not as simple as may sound. If the microswitch has cut off power how do you re-connect power to get the roof to go in the other direction?

...R

The microswitch has change over contacts and allows power to be connected in the reverse direction.

Example :Easiest Way To Reverse Electric Motor Directions - Robot Room

hammy:
The microswitch has change over contacts and allows power to be connected in the reverse direction.

Maybe. But does the OP know how to do that?

...R

siemvanharen:
I am new to c language.
In the program i wrote, all the switches for opening and closing the roof and sunshade worked.
Is there a easy way to stop de motors on the limit switches?

Please read post #10.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Thanks... Tom... :slight_smile:

Maybe something like this ..........

MotorF_R.jpg

MotorF_R.jpg

Hello,

Thanks for al the responses.
This is a picture of the electrical scheme.

Thanks,
Siem

Image from Reply #18 so we don't have to download it. See this Simple Image Guide

...R