0

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);
      }
    }
  • Yes, you can. You just have to change your thinking from "The button is being pressed" to "The button has been pressed". – Majenko Jan 11 at 23:25
  • you have to realize that controlling a servo, detecting a button press and detecting sound are all completely separate functions ..... you can have a boolean variable moveThatServo .... if the variable has a value true then the servo is moved and if it is false then the servo stops ..... you can have multiple buttons, some set moveThatServo to true and some set it to false .... you can have a sound detector that sets moveThatServo to true ...... you can write a timer function that does the same..... just use your imagination – jsotola Jan 12 at 1:28
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.