user52910: I am pretty new to Arduino.
I've created a code with the help of many YouTube tutorials to control a servo with a button. I decided it has gotten a little un-exciting so I've been trying to add a sound sensor module to it so that if you clap or whatever, it will control the servo in the same way the servo did. Is it possible to control the servo with the button and the sound sensor so that I have to ways of turning on the servo at the same instance.
The code I currently have just runs the servo constantly and does not stop, you'll probably see what I mean when I attach the code below.
I'm just looking for help on this issue as I don't have a lot of experience with this, but my guess is it is not possible.
    #include <Servo.h>;

    const int buttonPin = 8;
    int soundSensor = 2;
    Servo servo;

    int buttonState = 0;

    void setup()
    {
      servo.attach(3);
      servo.write(80);

      pinMode(buttonPin, INPUT);

      pinMode (soundSensor, INPUT);
    }


    void loop() {
      int statusSensor = digitalRead (soundSensor);

      if (statusSensor == 1)
      {
        servo.write(0);
        delay(1000);
        servo.write(180);
        delay(500);
      }
      else
      {
        servo.write(80);
      }

      buttonState = digitalRead(buttonPin);
      if (buttonState == HIGH)
      {
        servo.write(80);
      }
      if (buttonState == LOW)
      {
        servo.write(0);
        delay(1000);
        servo.write(180);
        delay(500);
      }
    }


user52919: Just cobled this together,
untested but should give you some hints in the right direction.
In the main loop it checks the condition of the button and also the sound detector.
If either are active it sets a bool to true.
at the end of the loop, if either are true it calls the servo function that then powers the servo.
hopefully you will be able to pull this apart to add more triggers
#include <Servo.h>;

const int buttonPin = 8;
int soundSensor = 2;
Servo servo;

int buttonState = 0;
int statusSensor = 0;

bool runServo = false;  // bool setting to start the servo

void setup()
{
  servo.attach(3);
  servo.write(80);

  pinMode(buttonPin, INPUT);
  pinMode (soundSensor, INPUT);
}

void loop() {
  statusSensor = digitalRead (soundSensor);
  buttonState = digitalRead(buttonPin);

    runServo = false;  // start of loop reset the runServo flag

  if (statusSensor == 1)
  {
    runServo = true;
  }

  if (buttonState == HIGH)
  {
    runServo = true
  }

  if (runServo == true)
  {
    servoFunction();//call the servoFunction code
  }

};


void servoFunction(){
    servo.write(0);
    delay(1000);
    servo.write(180);
    delay(500);

}      

Spriggsy

52910: <p>I am pretty new to Arduino.</p>

<p>I've created a code with the help of many YouTube tutorials to control a servo with a button. I decided it has gotten a little un-exciting so I've been trying to add a sound sensor module to it so that if you clap or whatever, it will control the servo in the same way the servo did. Is it possible to control the servo with the button <em>and</em> the sound sensor so that I have to ways of turning on the servo at the same instance.</p>

<p>The code I currently have just runs the servo constantly and does not stop, you'll probably see what I mean when I attach the code below.
I'm just looking for help on this issue as I don't have a lot of experience with this, but my guess is it is not possible.</p>

<pre class="lang-cpp prettyprint-override"><code>    #include &lt;Servo.h&gt;;

    const int buttonPin = 8;
    int soundSensor = 2;
    Servo servo;

    int buttonState = 0;

    void setup()
    {
      servo.attach(3);
      servo.write(80);

      pinMode(buttonPin, INPUT);

      pinMode (soundSensor, INPUT);
    }


    void loop() {
      int statusSensor = digitalRead (soundSensor);

      if (statusSensor == 1)
      {
        servo.write(0);
        delay(1000);
        servo.write(180);
        delay(500);
      }
      else
      {
        servo.write(80);
      }

      buttonState = digitalRead(buttonPin);
      if (buttonState == HIGH)
      {
        servo.write(80);
      }
      if (buttonState == LOW)
      {
        servo.write(0);
        delay(1000);
        servo.write(180);
        delay(500);
      }
    }
</code></pre>
 52919: <p>Just cobled this together,
untested but should give you some hints in the right direction.</p>

<p>In the main loop it checks the condition of the button and also the sound detector.
If either are active it sets a bool to true.
at the end of the loop, if either are true it calls the servo function that then powers the servo.
hopefully you will be able to pull this apart to add more triggers</p>

<pre><code>#include &lt;Servo.h&gt;;

const int buttonPin = 8;
int soundSensor = 2;
Servo servo;

int buttonState = 0;
int statusSensor = 0;

bool runServo = false;  // bool setting to start the servo

void setup()
{
  servo.attach(3);
  servo.write(80);

  pinMode(buttonPin, INPUT);
  pinMode (soundSensor, INPUT);
}

void loop() {
  statusSensor = digitalRead (soundSensor);
  buttonState = digitalRead(buttonPin);

    runServo = false;  // start of loop reset the runServo flag

  if (statusSensor == 1)
  {
    runServo = true;
  }

  if (buttonState == HIGH)
  {
    runServo = true
  }

  if (runServo == true)
  {
    servoFunction();//call the servoFunction code
  }

};


void servoFunction(){
    servo.write(0);
    delay(1000);
    servo.write(180);
    delay(500);

}      
</code></pre>

<p>Spriggsy</p>
