How to use the microphone to control multiple motors

HI everyone!

I’m a Beginner of Arduino~
For my project, I intent to use a microphone sensor to control 3 different type of motor(minimotor, servomotor&stepmotor), Unfortunately,no matter how many times I tried, my gadget failed to work…
Can anyone give me a hand?
THANKS~~

``
#include <Servo.h>
//servo’s pin set;
Servo servo;
int pos = 0;
int servoPin = 7;
//minimotor’ pin set;
int minimotorPin = 12;
//stepmotor’s pin set;
int stepPin1 = 8;
int stepPin2 = 9;
int stepPin3 = 10;
int stepPin4 = 11;
//sensor of sound’s pin;
int soundPin = 5;
//value;
int soundValue;
int soundMin = 0;
int soundMid = 1;
int soundMax = 2;

void setup()
{
//Initialize pin mode;
servo.attach(7);
servo.write(90);
//minimotor;
pinMode(minimotorPin, OUTPUT);
//stepmotor’s pinMode
pinMode(stepPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(stepPin3, OUTPUT);
pinMode(stepPin4, OUTPUT);
//initialize pinMode
digitalWrite (stepPin1, LOW);
digitalWrite (stepPin2, LOW);
digitalWrite (stepPin3, LOW);
digitalWrite (stepPin4, LOW);
//sound
pinMode(soundPin, INPUT);
Serial.begin(9600);
//define 3 class range;
for(soundValue = 0; soundValue < 300;){
soundValue = soundMin;
}

for(soundValue = 301; soundValue < 500;){
soundValue = soundMid;
}

for(soundValue = 501; soundValue <2000;){
soundValue = soundMax;
}
}

void loop()
{
soundValue = analogRead(soundPin);
Serial.println(soundValue);
delay(200);

switch(soundValue)
{
case ‘0’:
spin0();
break;

case ‘1’:
spin1();
break;

case ‘2’:
spin2();
break;

default:
Serial.print(soundValue);
Serial.println(" was received but not expected");
break;
}
}

void spin0()
{
digitalWrite(minimotorPin, HIGH);
Serial.println(“hello minimotor!”);
delay(750);
digitalWrite(minimotorPin, LOW);
delay(750);
}
void spin1()
{
Serial.println(“hello servomotor!”);
digitalWrite(servoPin, HIGH);
for(pos = 0; pos <= 90; pos += 1)
{
servo.write(pos);
delay(15);
}
delay(750);
}
void spin2()
{
Serial.println(“hello stepmotor!”);

digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
delay(187);

digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
delay(187);

digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
delay(187);

digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
delay(187);
}

  //define 3 class range;
  for(soundValue = 0; soundValue < 300;){
  soundValue = soundMin;
  }
  
  for(soundValue = 301; soundValue < 500;){
    soundValue = soundMid;
  }
  
  for(soundValue = 501; soundValue <2000;){
    soundValue = soundMax;
  }

What are you imagining that does? I believe the first one creates an infinite loop does it not? The code should lock right there.

If you want to map some values, you’ll have to do the math every time. Or create a function to do it. But you can’t just tell a computer that when some variable equals this make that variable equal that and expect the two to track with each other.

Right before that switch statement you should have a series of if statements that set your value for you.

Like:

if ( soundValue >500 && soundValue <= 2000){
     soundValue = soundMax;
}
else if ( soundValue > 300 && soundValue <= 500){
     soundValue = soundMid;
}
else if (soundValue <= 300){
     soundValue = soundMin;
}

Also the values in the switch statement shouldn’t be in the single quotes like that. That means interpret as ASCII. So ‘0’ == 48. You’re not setting any values to 48 so you won’t get far using 48 for a case value.

Hello and welcome,

Or simply:

void loop()
{ 
  soundValue = analogRead( soundPin );

  switch ( soundValue )
  {
    case 0 ... 300 :
      spin0();
      break;

    ...
  }

  ...
}

Oh I always forget gcc will let you do that.

a BIG thank you for your help!! I've modified my code, and the gadget became workable, too.