Limit switch and ultrasonic sensor

Good Evening All,

I am currently trying to do the ImechE design challenge at Uni and need some guidance on my code and project. Unfortunately I have very little coding experience.
Below is a rough schematic of the ciricuit:
image

Essentially, the circuit contains the following components:
DC motor
L298N motor driver board
1 x limit switch
1 x arduino board.

this is my plan for the project:
step 1) switch on arduino, motor rotates clockwise.
step 2) Once the limit switch is present the motor polarity is changed.
step 3) when the distance that the ultrasonic sensor measures is greater than 2m it ignores the limit switch and changes the polarity again.
step 4) when the distance is 1m it stops the motor.

The overall specification can be found at the following URL:

(please click on the 2023 2nd year document).

However, I have some questions:

  1. is the use of a component correct for what I am trying to do?
  2. Would the code shown below work:
// Constants and variables are set for the Arduino pins and the corresponding functions
int trigPin = 6; // TrigPin to D6
int echoPin = 5; // EchoPin to D5
int in1 = 10; // Left Side MOSFET Motor Controller to D10
int in2 = 9; //Left Side MOSFET Motor Controller to D9
int SW = 13; // Microswitch connect to D13
 float d; // Declaration of the distance variable used within the loop() function
 int S; // Declaration of the switch variable used within the loop() function, in the form of binary values

 int Phase = 0; // Variable that counts phases

 void setup() //Function to be executed once.
 {
 pinMode(in1, OUTPUT); // Sets pin in1 in OUTPUT mode
 pinMode(in2, OUTPUT); // Sets pin in2 in OUTPUT mode
 pinMode(trigPin, OUTPUT);// Sets the pin trigPin in OUTPUT mode
 pinMode(echoPin, INPUT); // Sets the pin echoPin in INPUT mode
 pinMode (SW, INPUT_PULLUP); // Sets the pin SW in INPUT_PULLUP mode
 Serial.begin(9600);// Starts the serial communication


 }

 void loop() { //Specified function loops indefinitely until Arduino is turned off
  
   switch(Phase){ //Switches between the multiple cases in the phases of the order coded
    
      case 0: // Case 0 ensures that when the switch has not been activated, the motor turns clockwise and travels up the pipe. 
      rotateCW();
   d = distanceRead();  //Distance measured by the ultrasonic sensor
   S = digitalRead (SW); //Switch input read
 
     if (S==0) //Switch is OFF - '0' and ON - '1'. The motor rotates ant-clockwise when the switch is pressed
     
     {
  Phase = 1;
     }
     Serial.print(Phase);
     
 break; //End of phase.

//Set distance here for when the model has gone up and back to the bottom to reverse polarity again

case 1: // Case 1: Anticlockwise rotation of the motor if the distance read is greater than or equal to 236cm
rotateACW();
 d=distanceRead();
 if(d >=236)
 {
 
 Phase = 2;
 Serial.print(Phase);
 }

 break;
//Set distance here for distance from datum to red line

 case 2: // Case 2: Clockwise rotation of the motor
 //if the distance read is less than or equal to 106cm, stop the motor
 rotateCW();
 d=distanceRead();
 if(d<=106)
 {
 stopMotor(); 
 Phase = 3;
 Serial.print(Phase);
 }

 break;


 default:
 Serial.println("Trial Completed"); // Print the message once the while loop has terminated
 while(1); //Stops the loop()
 }

 }
//***************** Function declaration ********************
//Function names and values are declared
   int rotateCW(){
 digitalWrite(in1, LOW);
 digitalWrite(in2, HIGH);
   }


 int rotateACW(){
 digitalWrite(in1, HIGH);
 digitalWrite(in2, LOW);
 }


 int stopMotor(){
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
 }

 int distanceRead(){
 float c; //Declaration of the speed of sound in air variable   
 float duration;  //Declaration of the duration of the ultrasonic wave variable
 float distance; //Declaration of the distance variable within the function declaration
 float T = 20.5; // Room temperature is in celsius

 digitalWrite(trigPin, LOW);// Clears the trigPin
 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);

digitalWrite(trigPin, LOW); // Sets the trigPin on LOW state

c = (331.4 +0.6*T)/10000; // Reference: http://hyperphysics.phyastr.gsu.edu/hbase/Sound/souspe.html
duration = pulseIn(echoPin, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds

distance = duration*c; //Displayed distance as a result of time passed and set value of speed of sound wave travel

return distance;
 }

Any help will be greatly appreciated.

// Constants and variables are set for the Arduino pins and the corresponding functions
const int trigPin = 6; // TrigPin to D6
const int echoPin = 5; // EchoPin to D5
const int in1_pin = 10; // Left Side MOSFET Motor Controller to D10
const int in2_pin = 9; //Left Side MOSFET Motor Controller to D9
const int SW_pin = 13; // Microswitch connect to D13

void setup() //Function to be executed once.
{
  pinMode(in1_pin, OUTPUT); // Sets pin in1 in OUTPUT mode
  pinMode(in2_pin, OUTPUT); // Sets pin in2 in OUTPUT mode
  pinMode(trigPin, OUTPUT);// Sets the pin trigPin in OUTPUT mode
  pinMode(echoPin, INPUT); // Sets the pin echoPin in INPUT mode
  pinMode (SW_pin, INPUT_PULLUP); // Sets the pin SW in INPUT_PULLUP mode
  Serial.begin(9600);// Starts the serial communication

  Serial.print(0);
  rotateCW();
  while (digitalRead (SW_pin));
  Serial.print(1);
  rotateACW();
  while (distanceRead() < 200);
  Serial.print(2);
  rotateCW();
  while (distanceRead() > 100);
  Serial.print("stop");
  stopMotor();
}

void loop() {} 

//***************** Function declaration ********************
int rotateCW() {
  digitalWrite(in1_pin, LOW);
  digitalWrite(in2_pin, HIGH);
}

int rotateACW() {
  digitalWrite(in1_pin, HIGH);
  digitalWrite(in2_pin, LOW);
}

int stopMotor() {
  digitalWrite(in1_pin, LOW);
  digitalWrite(in2_pin, LOW);
}

int distanceRead() {
  unsigned long duration;  //Declaration of the duration of the ultrasonic wave variable

  digitalWrite(trigPin, LOW);// Clears the trigPin
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW); // Sets the trigPin on LOW state

  duration = pulseIn(echoPin, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds

  return int(distance / 58);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.