Problem with adding ultrasonic sensor code with two direction of dc motor

Hello,
i am trying to add the third position (ultrasonic sensor ) to the two position of dc motor directions (Forward & Reverse), but it does not work .Since the position 1&2 worked together and ultrasonic code worked alone.

here is my code for (forward direction & Ultrasonic sensor) only:

// defines pins numbers
const int trigPin = 6;
const int echoPin = 5;
// defines variables
long duration ;
int distance;

int SPEED; // speed of motor
int switchPos3 = 4; // pin for position 3
int switchPos1 = 2; // pin for position 1

int ENA = 8; // enable pin for L298n H bridge motor driver
int In1 = 9; // input pin for L298n H bridge motor driver
int In2 = 7; // input pin for L298n H bridge motor driver

int potval =A0;// analog pin for potentiometer

// variable will change
int switchState1 = 0;
int switchState3=0;

void setup() {
// put your setup code here, to run once:
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication

pinMode (switchPos3, INPUT);
pinMode (switchPos1, INPUT);

pinMode(In1, OUTPUT);
pinMode (In2, OUTPUT);
pinMode(ENA, OUTPUT);

}
void posOne()
{
switchState1 = digitalRead (switchPos1); // read the switch pin
if ( switchState1 == HIGH )
{
SPEED = analogRead(A0); // Read potentiometer value
SPEED = SPEED * 0.249;
Serial.println(SPEED);

digitalWrite (In1, HIGH); // forward direction set pin states HIGH
digitalWrite (In2, LOW); // forward direction set pin states LOW
digitalWrite (ENA, HIGH); // forward direction set pin states HIGH
Serial.println("Position 1 = on "); // print to serial monitor
// Serial.print("Position1: "); // print to serial monitor

}
else {
//Serial.println(“off”);

digitalWrite (In2, LOW); // off
digitalWrite (In1, LOW); // off
digitalWrite (ENA, LOW); // off

}
}
void posThree(){
switchState3 = digitalRead (switchPos3); // read the switch pin
if ( switchState3 == HIGH )
{

// put your main code here, to run repeatedly:
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;

//Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
if (distance >=5 && distance <=30)
{
analogWrite(ENA,SPEED); // Send PWM signal to L298N Enable pin
digitalWrite (In1,LOW ); // forward direction set pin states HIGH
digitalWrite (In2, HIGH); // forward direction set pin states LOW
digitalWrite (ENA, HIGH); // forward direction set pin states HIGH
}
else {

digitalWrite (ENA,LOW); // off
digitalWrite (In2, LOW); // off
digitalWrite (In1, LOW); // off
}

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

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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:

mgbanah:
but it does not work

Not a very helpful description of the problem. Does the program do anything? What exactly? What did you expect it to do that's different? What do all your Serial.prints show you is happening?

Steve

Actually, I am using a 4 position selector switch to control dc motor in three modes:
Position 1 (forward direction), position 2 (reverse direction) ,and position 3 backup assist (ultrasonic sensor). When I upload code for position 1&2 together the dc motor will work for two position.Then , I ran the code for position 3 (ultrasonic sensor) by itself. The problem is when I add position 3 to 1&2 , it just work for position 1 &2 and there is no action for position 3. Here are my circuit & code.

 for position 1&2

 int switchPos1 = 2; // pin for position 1
    int switchPos2 = 4; // pin for position 2
   
    int ENA = 8; //  enable  pin for L298n H bridge motor driver
    int In1 = 9;  // input pin for   L298n H bridge motor driver
    int  In2 = 7;   // input pin for   L298n H bridge motor driver
    int led1 =12; // input for headlight
    int led2 =13; // input for taillight
    int SPEED ;  // speed of motor
    int potval =A0;// analog pin for potentiometer


 
    // variable will chaange 
      int switchState1 = 0;
      int switchState2 = 0;
    

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600); // Starts the serial communication

 
   
   Serial.begin (9600); // Arduino to begin serial communication
    //set inputs
  pinMode (switchPos1, INPUT);
  pinMode (switchPos2, INPUT);
   

 // set outputs
  pinMode(In1, OUTPUT);
  pinMode (In2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode (led1,OUTPUT);
}
void posOne()
 {
  switchState1 = digitalRead (switchPos1);  // read the switch pin
  if ( switchState1  == HIGH )
    {
     SPEED = analogRead(A0); // Read potentiometer value
      SPEED = SPEED * 0.249;
       Serial.println(SPEED);
   
          digitalWrite (led1,HIGH);   // headlight HIGH
           digitalWrite (In1, HIGH);  // forward direction set pin states  HIGH 
           digitalWrite (In2, LOW);   // forward direction set pin states  LOW
          digitalWrite (ENA, HIGH);  // forward direction  set pin states  HIGH
          Serial.println("Position 1 = on ");    // print  to serial monitor
          // Serial.print("Position1: ");  // print  to serial monitor
        
    }
 else {
    //Serial.println("off");
     digitalWrite (led1,LOW);   // off
     digitalWrite (In2, LOW);   // off
     digitalWrite (In1, LOW);   // off
    // digitalWrite (ENA, LOW);    // off
       
   }
   }
      void posTwo(){
       switchState2 = digitalRead (switchPos2);
     if ( switchState2 == HIGH)
     {
      SPEED = analogRead(A0); // Read potentiometer value
      SPEED = SPEED * 0.249;
       Serial.println(SPEED);
        analogWrite(ENA, SPEED); // Send PWM signal to L298N Enable pin
      digitalWrite (led2,HIGH);    //taillight HIGH
      digitalWrite (In1, LOW);     // reverse  direction set pin states  LOW
      digitalWrite (In2, HIGH);   // reverse  direction  set pin states  HIGH
      digitalWrite (ENA, HIGH); // reverse  direction  set pin states  HIGH
      Serial.println("Position 2 = on");        // print  to serial monitor
      //Serial.print("Position2: ");  // print  to serial monitor
        }
    else 
    {
     Serial.println("off");    // print  to serial monitor
      digitalWrite (led2,LOW);   // off
      digitalWrite (In1, LOW);  // off
      digitalWrite (In2, LOW);  //off
       //digitalWrite (ENA, LOW);  // off
       }
   }
 
     
      void loop() {
      // put your main code here, to run repeatedly:
          posOne();
        posTwo();
        
    

   }
 for position3

// defines pins numbers
const int trigPin = 6;
const int echoPin = 5;
// defines variables
long duration ;
int distance;

 int switchPos3 = 4; // pin for position 1
 int ENA = 8; //  enable  pin for L298n H bridge motor driver
    int In1 = 9;  // input pin for   L298n H bridge motor driver
    int  In2 = 7;   // input pin for   L298n H bridge motor driver
   int switchState3=0;
    int SPEED;  // speed of motor
  
void setup() {
  
  // put your setup code here, to run once:
   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication

 pinMode (switchPos3, INPUT);

  pinMode(In1, OUTPUT);
  pinMode (In2, OUTPUT);
  pinMode(ENA, OUTPUT);
}

void loop() {
   switchState3 = digitalRead (switchPos3);  // read the switch pin
  if ( switchState3  == HIGH )
  {
   

  // put your main code here, to run repeatedly:
   // Clears the trigPin
    digitalWrite(trigPin, LOW);
     delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
   // Reads the echoPin, returns the sound wave travel time in microseconds
   duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
       distance= duration*0.034/2;

       //Prints the distance on the Serial Monitor
    Serial.print("Distance: ");
     Serial.println(distance);
}
if (distance >=5 )//&& distance <=30)
{
 analogWrite(ENA,SPEED); // Send PWM signal to L298N Enable pin
        digitalWrite (In1,LOW );  // forward direction set pin states  HIGH 
           digitalWrite (In2, HIGH);   // forward direction set pin states  LOW
          digitalWrite (ENA, HIGH);  // forward direction  set pin states  HIGH
}
  else {
    
    digitalWrite (ENA,LOW);   // off
     digitalWrite (In2, LOW);   // off
     digitalWrite (In1, LOW);   // off
    }
  

  
  }

int switchPos3 = 10; // pin for position 3

You've just posted the bits of code that work. How is that going to help us tell what is wrong with your combined code that doesn't work?

POST THE COMPLETE CODE WITH ALL 3 FUNCTIONS IN ONE PROGRAM!

Also according to your Fritzy thing you have pull-up resistors on the switch...so all 3 outputs are going to be permanently HIGH. I don't think you mean that.

Steve

Thanks, I figured it out

mgbanah:
Thanks, I figured it out

What was the solution?
Tom.... :slight_smile: