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) ;
}
}
}
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.
@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.
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.