2 input 1 output flip-flop

Hi I'm quite new to Arduino and I'm doing small project which is moving the object between two sensors by using stepper motor with screw attached. I have a problem to write a flip flop logic to run this circuit. What I want to archive is: - the motor moves in one direction till its hits the sensor 1 (input) - when this happen I want to change the direction of the motor (output) and keep going till it reaches sensor 2 (input) - then change direction again till it reaches sensor 1 (input) - I want to do this continuously.

Below is the code I have so far:

int pulses = 13;
int sensor2 = 12;
int sensor1 = 11;
int motor_dir = 7;


void setup() {    
  
  // initialize the digital pins 
  
  pinMode(pulses, OUTPUT); 
  pinMode(sensor2, INPUT);
  pinMode(sensor1, INPUT);
  pinMode(motor_dir, OUTPUT);

}

void loop() {
  
  
  digitalWrite(pulses, HIGH);   
  delay(25);              
  digitalWrite(pulses, LOW);    
  delay(25);           
}

This code just get to motor to run in one direction.I was trying to use IF...ELSE functions but the motor just change direction when one of the sensors is on and then witching back and hits sensor again. Could any one please point me what is the best method to archive working sequence I want to do?

Moderator edit: code tags added

That code doesn't have any inputs, so I don't know how your sensors can cause anything to happen.

moving the object between two sensors by using stepper motor

With a motor shield? The code you posted does not look like what is needed to drive a stepper motor.

That code has nothing to actually read the sewitches.

I'd have something like this (in psuedo code:

stepdirection = forward void loop if(stepdirection is forward) while(digitalRead(limitSwitchOne) == HIGH) // assumes low means pressed step in step direction step direction = backwards else while(digitalRead(limitSwitchTwo) == HIGH) step in step direction step direction = forwards

The thing to realise is that a flip-flop has state. Thus you need a variable in your code to hold the state, perhaps

  boolean forwards = true ;

Then when your motor hits one of the sensors you set the state to the correct one

  if (digitalRead (sensor1))
    forwards = false ;
  if (digitalRead (sensor2))
    forwards = true ;

or something like that. Then drive the motor in the direction indicated by the state, testing all the time for sensors...

Ok I get it sorted

Thank You

int pulses = 13;
int sensor2 = 12;
int sensor1 = 11;
int motor_dir = 7;

void setup() {    
  
  // initialize the digital pins 
  
  pinMode(pulses, OUTPUT); 
  pinMode(sensor2, INPUT);
  pinMode(sensor1, INPUT);
  pinMode(motor_dir, OUTPUT);
Serial.begin(9600);

}

void loop() {
  
  
  digitalWrite(pulses, HIGH);   // set the LED on
  delay(5);              // wait for a second
  digitalWrite(pulses, LOW);    // set the LED off
  delay(5);              // wait for a second

//sensors logic

if(digitalRead(sensor1) == HIGH && digitalRead(sensor2) == LOW)
{
digitalWrite(motor_dir, HIGH);
}
  else if(digitalRead(sensor1) == LOW  && digitalRead(sensor2) == HIGH)
{
digitalWrite(motor_dir, LOW);
}

}

Moderator edit: code tags. :0

I'm just wondering is there any other method to do it using switch/case or variables?

if(digitalRead(sensor1) == HIGH && digitalRead(sensor2) == LOW)
{
digitalWrite(motor_dir, HIGH);
}
  else if(digitalRead(sensor1) == LOW  && digitalRead(sensor2) == HIGH)

Unless there is ever a situation where both switches can be pressed at the same time, then the limit tests only need to involve one switch.

I'm just wondering is there any other method to do it using switch/case or variables?

Using variables:

int stateOne = digitalRead(sensor1);
int stateTwo = digitaRead(sensor2);
if(stateOne == HIGH)
{
}
else if(stateTwo == HIGH)
{
}

A switch statement is just a shorthand for if/else if/else if/else if/..., generally only useful when there are three or more cases. For just two cases, it actually takes more code than if/else if.

Martinez76:
Ok I get it sorted

Give it a try, but I don’t think that will give the behaviour you asked for unless you have mechanically linked the two limit switches together. PaulS’ comment showed you how to achieve the behaviour you described.

By the way, if you replace the stepper motor with an ordinary DC motor you could probably achieve this behaviour just with a couple of switches and a twin pole changeover relay, no electronics or software needed. But the Arduino approach with a stepper motor will work fine too, if you read, understand and follow PaulS’ advice.