Double Clap for Micro Servo Motor

Hi, I’m new to Arduino Programming, but already familiar with C++ and VBA. I’m struggling with a simple code that after hearing 2 Claps, turns a Micro Servo Motor from 0º to 180º, and returns after one second.

Problem is, my Servo (9g) keeps spinning (which “broke” and I had to open it, and it is still spinning freely).

What is wrong in my code? Thanks in advance :slight_smile:

#include <Servo.h>
#define sensorPin 2

const int pinoServo = 6;
Servo s;
int pos;
int ValorUltimoSom;
int ValorSom;
long TempoUltimoSom = 0;
long TempoPrimeiroSom = 0;
long MudancaUltimaLuz = 0;

void setup() {
  s.attach(pinoServo);
  s.write(0);
  pinMode(sensorPin, INPUT);
}

void loop() {
  ValorSom = digitalRead(sensorPin);
  TempoPrimeiroSom = millis();

  if (ValorSom == 1) {

    if (
      (TempoPrimeiroSom > TempoUltimoSom + 200) &&
      (ValorUltimoSom == 0) &&
      (TempoPrimeiroSom < TempoUltimoSom + 800) &&
      (TempoPrimeiroSom > MudancaUltimaLuz + 1000)
    ) {

      for (pos = 0; pos < 180; pos += 1) {
        s.write(pos);
        delay(50);
      }
      delay(1000);
      for (pos = 180; pos >= 0; pos -= 1) {
        s.write(pos);
        delay(50);
      }
      MudancaUltimaLuz = TempoPrimeiroSom;
    }

    TempoUltimoSom = TempoPrimeiroSom;
  }

  ValorUltimoSom = ValorSom;
}

notfrajola:
Problem is, my Servo (9g) keeps spinning (which "broke" and I had to open it, and it is still spinning freely).

If it is a regular 180° servo and it is spinning freely then you just need to buy a new servo - it sounds as if the internal mechanism has become separated from the feedback potentiometer.

...R

Hi, thanks for the response.

It’s not actually spinning freely, it got stuck. And after I opened it and removed one gear, the motor kept spinning alone.

If I change the low angle/position in the code from 0º to 30º, and manually put the other gear to around 30º, it stops spinning the motor. And if I clap twice, nothing happens :frowning:

I’m so confused

Try it with a different servo. It sounds as though you have ruined that one by taking a gear out so the feedback to the motor driver no longer has any connection with what the motor is actually doing.

If you don't know what you're doing it's often a good idea not to do it!

Steve

notfrajola:
And after I opened it and removed one gear, the motor kept spinning alone.

And what did you think would happen? (Or was any thinking done?)

Get a new servo.

...R