Start & stop arduino controlled dc motoro through sound sensor

I am working on a code to start and stop dc motor controlled by Arduino UNO through an sparkfun sound sensor. The motor starts with a clap but then it is in an infinite loop of changing direction through b-bridge.

Here is my code: It starts the motor with a clap but how to stop it.

//DC motor
const int pwm = 3;  //initializing pin 3 as pwm
const int in_1 = 8 ;
const int in_2 = 7 ;

//Sound sensor
int soundSensor=2;
//int LED=4;
boolean LEDStatus=false;


void setup() {
  pinMode(soundSensor,INPUT);
//  pinMode(LED,OUTPUT);

  pinMode(pwm,OUTPUT) ;   //we have to set PWM pin as output
  pinMode(in_1,OUTPUT) ;  //Logic pins are also set as output
  pinMode(in_2,OUTPUT) ;


}

void loop() {

  int SensorData=digitalRead(soundSensor);
  if(SensorData==1){

    if(LEDStatus==false){
      LEDStatus=true;

      digitalWrite(in_1,HIGH) ;
      digitalWrite(in_2,LOW) ;
      analogWrite(pwm,255) ;
        //Clockwise for 3 secs
      delay(1500) ;    

//For brake
     digitalWrite(in_1,HIGH) ;
      digitalWrite(in_2,HIGH) ;
      delay(500) ;

//For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH
      digitalWrite(in_1,LOW) ;
      digitalWrite(in_2,HIGH) ;
      delay(1500) ;

    }
    else {
        LEDStatus=false;
        digitalWrite(in_1,HIGH) ;
        digitalWrite(in_2,HIGH) ;

    }


  }

 }

Your code is going to be deaf for 99.99% of the time.
You need to get rid of the delays.

can you please help by editing in the code

Edit?
It needs to be rewritten.

Yes, please if possible. I think it's not a big code. Can you please put a similar example too.

Start here - it's a very useful tutorial.

can you please see why this piece of code is not running the motor. Pins 7 & 8 go to input 1 & 2 of the H-Bridge.

//DC motor
const int pwm = 3; //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 7 ;

//Sound sensor
//int soundSensor=2;
//boolean LEDStatus=false;

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const int motorUPinterval = 500;
const int motorUPduration = 3000;

byte motorState = LOW;

void setup() {

Serial.begin(9600);
Serial.println("starting......");

pinMode(pwm,OUTPUT) ; //we have to set PWM pin as output
pinMode(in_1,OUTPUT) ; //Logic pins are also set as output
pinMode(in_2,OUTPUT) ;

}

void loop() {

currentMillis = millis();
runDCmotor();

}

void runDCmotor (){

if(motorState = LOW) {

if(currentMillis - previousMillis >= motorUPinterval) {
motorState = HIGH;
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ;

}
}

else if(currentMillis - previousMillis >= motorUPduration)
motorState = LOW;
previousMillis += motorUPduration;
Serial.println("starting......");

}

      if(motorState = LOW) {

Why are you assigning LOW to motorState using an if statement?

OK, It was assignment operator used instead of comparison operator in line if(motorState = LOW). It's solved

@PaulS: ThanX for the reply, I saw it now.

I am trying to start a method tu run a dcd motor controlled by an h-bridge through a sound sensor. But my code start running without getting any sound input. I tried but couldn't figure out problem.

Here is my code:

//DC motor
const int pwm = 3;  //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 7 ;


//Sound sensor
int soundSensor=2;
boolean sensStatus=false;


//millis
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const int motorUPinterval = 500;
const int motorUPduration = 3000;
byte motorState = LOW;

 
void setup() {
 
  Serial.begin(9600);
    
  pinMode(pwm,OUTPUT);   //we have to set PWM pin as output
  pinMode(in_1,OUTPUT);  //Logic pins are also set as output
  pinMode(in_2,OUTPUT);

  pinMode(soundSensor,INPUT); //sound sensor
  
}

void loop() {

  currentMillis = millis();

  //sound sensor to start motor running by a clap
  int sensorData = digitalRead(soundSensor);
    if(sensorData == 1)
      {  
       if(sensStatus == false)
        { 
          sensStatus = true;
          runDCmotor();
          }
          else {
                sensStatus = false;                
             }
        }
      } 

 void runDCmotor (){

      if(motorState == LOW) {

          if(currentMillis - previousMillis >= motorUPinterval) {
                      motorState = HIGH;
                      digitalWrite(in_1,HIGH) ;
                      digitalWrite(in_2,LOW) ;
                      analogWrite(pwm,255) ;       
                          
            }
       }

       else if(currentMillis - previousMillis >= motorUPduration)
               motorState = LOW;
               previousMillis += motorUPduration;
       
  } 

void runNeoPixel(){
  
      //NeoPixel ring needs to run parallel to motor running, still to implement & it all start with sound sensor
  }

Is it safe to assume that the sound sensor will produce +5VDC when it detects a sound? What threshold? Have you checked the output of the sensor? Are you using a pull-down resistor on pin 2?

Maybe you can post your circuit and a copy of the datasheet for the sound sensor? There dpesn't appear to be anything wrong with your code.

If in doubt, make better use of this

 Serial.begin(9600);

Maybe this is part of your problem

if(currentMillis - previousMillis >= motorUPduration)
motorState = LOW;
previousMillis += motorUPduration;

@DKWatson: You were right. The power supply was problem. I was using USB data cable cable to upload source and test code. Now I switched to power adapter & it started working.

Though still some problem is it don't start with first sound signal detect. When it detect 2nd tap, it works. Also if 1st two taps are fast enough, it doesn't even then strat, & 3rd tap starts it. Can you figure out what's wrong here.

I am not using a pull down physical resister, only making pin value LOW in code.

@AWOL: i tried commenting it & making value HIGH, but it didn't work.

Can you check to see if the first clap is causing the sensor to generate a signal? The other issue may be that we don't know how long the sensor maintains its signal level. Any time you are polling a digital input, the possibility exists that when the pin is checked it has already reset. Better strategy to consider using an interrupt.

Nikko2:
@AWOL: i tried commenting it & making value HIGH, but it didn't work.

I wasn't commenting on the value, but the apparent lack of braces.

Also:

  pinMode(in_1,OUTPUT);  //Logic pins are also set as output
  pinMode(in_2,OUTPUT);
  1. in_1 and in_2 are confusing names for outputs.

  2. When you are tempted to suffix variable names with numbers, it is time to learn about arrays.