Motor and microswitches

Hello, I am working on a university project on vending machines.
Right now I have a motor that moves back and forward to a bar.
when you push a button the motor gets to a position, stops, activates the solenoid, which pushes a can and then gest back to a home position.
what I really can't do is to tell the motor to get back until the microswitch is hitten and then stop and start reading again the code from the beginning.
here is the code:

#define cw 0
#define ccw 1

int black = 11;
int orange = 10;
int  yellow = 8;
int brown = 9;
int button1= 7;
int button2 =6;
int button3 = 5;
int solenoid= 3;
int microswitch= 4;

int currentStep = 0;

void setup() {
  
pinMode(black, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(brown, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(solenoid, OUTPUT);
pinMode(microswitch,INPUT);
Serial.begin(9600);
}


void loop() {
  // put your main code here, to run repeatedly: 
  
  
  int state = digitalRead (button1);
  if (state==LOW)
  {letsStep (ccw,100,20);
  delay(1000);
  digitalWrite (solenoid,HIGH);}
  delay (1000);
  homebutton();
  }
  
 void homebutton(){
   int state=digitalRead (microswitch);
   while (state==1)   
   {letsStep (cw,1,20);
   }
 }
  
  
  
   
void letsStep(int direc, int steps, int time){
  
for(int i=0; i<steps; i++)
{
  
  if (direc==cw)
  {
     currentStep-=1;
      if (currentStep<0)
      {
         currentStep=3; 
      } 
  }
  else if (direc==ccw)
  {
     currentStep+=1;    
      if (currentStep>3)
      {
         currentStep=0; 
      }
  }
  else
  {
     Serial.println("Error: Direction not set correctly");   
  }
  
  if( currentStep==0)
  {
      digitalWrite(black,LOW);
      digitalWrite(brown,HIGH);
      digitalWrite(orange,HIGH);
      digitalWrite(yellow,LOW);
  }
  else if( currentStep==1)
  {
      digitalWrite(black,LOW);
      digitalWrite(brown,HIGH);
      digitalWrite(orange,LOW);
      digitalWrite(yellow,HIGH);
  }  
  else if( currentStep==2)
  {
      digitalWrite(black,HIGH);
      digitalWrite(brown,LOW);
      digitalWrite(orange,LOW);
      digitalWrite(yellow,HIGH); 
  }
  else if( currentStep==3)
  {
      digitalWrite(black,HIGH);
      digitalWrite(brown,LOW);
      digitalWrite(orange,HIGH);
      digitalWrite(yellow,LOW);
  }
  else
  {
   Serial.println("Error: out of step # range");   
  }
  delay(time);
  Serial.println(currentStep);
  
  }
}

I am a novice so please forgive me for the many stupid errors that I might have written .
Thanks for you help

Hi and welcome.

Because this is a school project, the goal must be that you'll learn from it.
Your question isn't real clear to me, but i think you have no idea how to figure out the microswitch and need help with that.
You didn't tell us how the microswitch is wired, and that is an important thing to know.
We can find out how your motor is wired and that it is a stepper (but not which one) from the sketch.

Your sketch is reading that microswitch, and puts the read in a variable called 'state'.
After that you are testing the content of 'state' to change, and while it has not changed, you are driving the stepper.
Funny question: when do you think the breaking event to that while statement will be registered ?
Will 'state' ever change while you're in the while ?

I think that's the info you need to work out the right approach.

Further, find out about internal pullups so you can create a somewhat safer circuit, and save components at the same time.

hello thanks for the answer. I am really into this project and I am really keen on learning from it so I won't ask you directly the answer but just some help to understand where I am goign wrong.

my switch is wired up with a resistor between the 5 volt input and the cable that goes into the arduino pin, so I guess is a pull up configuration.

what I was thinking I could do was to use a while loop that would start as soon as the solenoid goes LOW.
this while loop registers the state of the switch and makes the motor spin backwards until it touches the switch ( therefore changes the stage of the switch) and stops. Once it stops the arduino should start reading the code from the beginning ( therefore to read if one of the selection button is pressed etc.)

I am a bit cofused on where I need to put this while loop in order to make it run just when the solenoid has pushed the can

thank you very much for your help

lodovica:
my switch is wired up with a resistor between the 5 volt input and the cable that goes into the arduino pin, so I guess is a pull up configuration.

Agreed, that is indeed a pull up configuration.
But you can use that without the external resistor.

what I was thinking I could do was to use a while loop that would start as soon as the solenoid goes LOW.
this while loop registers the state of the switch and makes the motor spin backwards until it touches the switch ( therefore changes the stage of the switch) and stops. Once it stops the arduino should start reading the code from the beginning ( therefore to read if one of the selection button is pressed etc.)

I understood that was your plan.
But it is not exactly what you are doing.
You are reading the switch once.
Then you are running the motor past the switch and until something breaks.
You are not reading the switch again.

I am a bit cofused on where I need to put this while loop in order to make it run just when the solenoid has pushed the can

Ok, that is more of a question than the first post.
You put this code in a separate function.
That means it is only run when called.
But you are calling it unconditional, so this function is always called at the end your really short loop.
You are checking a button.
If it was pressed, you go do something with a motor.
When you're done with that, you wait for a second.
And then you (think you) send that motor back to the home position, even when it was already there.

do you mean something loke this ?

void loop() {
  // put your main code here, to run repeatedly: 


  int state = digitalRead (button1);
  if (state==LOW)
  {
    letsStep (ccw,100,20);
    delay(1000);
    digitalWrite (solenoid,HIGH);
  }
  delay (1000);
  state= digitalRead(homebutton);
  if (state== HIGH)
  {motorhome;}
}


void motorhome(){
  while 
  (letsStep) (cw,1,20);}

but how do I interrupt the while loop when the microswitch has been touched and tell the arduino to start reading the loop from the beginning ? or does the while loop automatically stop as the condition becomes FALSE?

ok I have managed to stop the motor once the button is pushed, but which is the function to tell him to read again the code from the top ?

here is the code I wrote so far

void loop() {
  // put your main code here, to run repeatedly: 


  int state = digitalRead (button1);
  if (state==LOW)
  {letsStep (ccw,100,20);
  delay(500);
  digitalWrite (solenoid, HIGH);
  delay (500)
  while  (state=digitalRead(homebutton))
  if (state==HIGH)
  {motorhome;}
  if (state==LOW)
   {return;}
 }
}

void motorhome(){
  while
  (letsStep)(cw,1,20);}

No, that's not it.

You do not know the meaning of 'function' in C.
So i suggest you read up on that.
You are using functions in your original sketch, so i had to assume you know what those are.
Of course that was against better judgment.
This makes me think you are using code you were given or found yourself somewhere.

Also, the code you wrote so far is not what you're showing, as it is incomplete and will not compile.
Next time when you post code, post all of it.

You do not want to stop all control of that motor if the switch was pressed, only if you are traveling to the home position.

but how do I interrupt the while loop when the microswitch has been touched

Read your own question.
It has the answer in it.
Also this part is important:

myself:
You are reading the switch once.
Then you are running the motor past the switch and until something breaks.
You are not reading the switch again.

If you are in loop(), you are doing exactly that:
The code keeps being repeated over and over again until you leave that loop.
So that is what you should take advantage of.
You are leaving loop() when going home, and when making your motor run.
You are also leaving going home when doing so by running your motor.
You are always returning from running your motor (but without telling the code to return).
Your problem is that you are never leaving going home, once you are there, you've locked yourself in.
Think of a way to escape from going home, you're real close already and you should find out how by going through my hints.