servo reacting on soundsensor

Hi, I have a setup of 3 FC-04 sound sensors and a servo motor. When one of the sensors hears a sound, the servo is then to move into the right angle. However, this only happens when the second and third sensor hear a sound. When the first sensor hears a sound it moves to 0 degrees but after a phew seconds it moves back to 180 degrees.

I would appriciate the help.

#include <Servo.h> 

const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;

const int senOut1 = 5;
const int senOut2 = 6;
const int senOut3 = 7;

Servo myservo;

int val1 = 0;
int val2 = 0;
int val3 = 0;

int pos = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(senOut1, INPUT);
  pinMode(senOut2, INPUT);
  pinMode(senOut3, INPUT);
  myservo.attach(11);
  Serial.begin(9600);
}

void loop() {
  val1 = digitalRead(senOut1);
  val2 = digitalRead(senOut2);
  val3 = digitalRead(senOut3);
  
  digitalWrite(LED1, val1);
  digitalWrite(LED2, val2);
  digitalWrite(LED3, val3);
  
  
  if (val1) {
    Serial.println("val1");
    if(pos>0) {
      for(int i=pos;i>0;i--) {
        myservo.write(i);
        delay(15);
      }
    }
    pos = 10;
  }
  if (val2) {
    Serial.println("val2");
    if(pos<90) {
      for(int i=pos;i<90;i++) {
        myservo.write(i);
        delay(15);
      }
    }
    if(pos>90) {
      for(int i=pos;i>90;i--) {
        myservo.write(i);
        delay(15);
      }
    }
    pos = 90;
  }
  if (val3) {
    Serial.println("val3");
    if(pos<180) {
      for(int i=pos;i<180;i++) {
        myservo.write(i);
        delay(15);
      }
    }
    pos = 180;
  }
  
  Serial.print("val1: ");
  Serial.println(val1);
  
  Serial.print("val2: ");
  Serial.println(val2);
 
  Serial.print("val3: ");
  Serial.println(val3);
  
 
  Serial.print("servo: ");
  Serial.println(myservo.read());
  
  Serial.print("pos: ");
  Serial.println(pos);
  
  delay(200);
  
}

Hi and welcome to the forum.
Congrats on using code tags on your first post. That shows that you paid attention and did some research before you posted.
Karma++

Is sensor 1 hears a sound, you move the servo to 0.
Is sensor 2 hears a sound, you move the servo to 90.
Is sensor 3 hears a sound, you move the servo to 180.

Is that right?

What would happen if sensor 1 AND sensor 3 hear a sound?

Thank you for replying and yes, you are right.
If sensor 1 and 3 hear a sound when the servo is on a 90 degree angle, the servo then moves to either 0 degrees or 180 degrees depending on which of the sensors hears a sound first. this also happens when the servo has an angle of 180 degrees.

But as I explained in my post, when the servo reaches 0 degrees, it moves back to 180 degrees.

What do you see in the printout of values ?

BartvdBos:
But as I explained in my post, when the servo reaches 0 degrees, it moves back to 180 degrees.

I think this the expected behavior if both sensor 1 and sensor 3 hear a sound during the same loop.

UKHeliBob:
What do you see in the printout of values ?

+1

This can't be the issue, because even when there is no sound the servo keeps going back to 180.

What do you see in the printout of values ?

After restart:
val1: 0
val2: 0
val3: 0
servo: 90
pos: 0

From 180 to 90:
val1: 0
val2: 0
val3: 0
servo: 179
pos: 180
val2
val1: 0
val2: 1
val3: 0
servo: 91
pos: 90

From 90 to 0 and back to 180:
val1: 0
val2: 0
val3: 0
servo: 91
pos: 90
val1
val1: 1
val2: 0
val3: 0
servo: 1
pos: 0
val1 (<-- it is not supposed to do this!)
val2
val3
val1: 1
val2: 1
val3: 1
servo: 179
pos: 180

(<-- it is not supposed to do this!)

Why not?
If more than one sensor is sensing sound, then each sensor that does will indicate this by returning the appropriate value.

I suspect that some noise was detected by all 3 sensors during that loop.

You showed us some print results, along with some annotation.
One notes is that you are getting an unexpected result when all 3 sensors return true.
I suspect that this a legitimate result, but we can talk through it more if you disagree. I could be misunderstanding any number of things that are clear to you, and only through continued discussion will it all become clear.

Is that the only unexpected result you get?

Thank you for sticking with me this long.

Your suspicion is understandeble but there really is no sound in the whole room. That's why I think there is something wrong with my code but I can't seem to find out what. This is the only thing that doesn't work like I want it to.

What kind of sensors are you using? Sound is generally an analog thing, not a digital thing.

Your suspicion is understandeble but there really is no sound in the whole room.

Unless you are testing in a specially designed anechoic chamber, the likelihood that there really is NO sound in the room is about the same as Donald Trump's chances of re-election.

That might have been an overstatement of me :slight_smile: . What I meant is that the motor and the sensors are a fair distance apart and the sensors are not highly sensitive.

and the sensors are not highly sensitive.

We still don't know what kind they are, or if digitalRead() is a meaningful way to get data from them.

Do you mean the sound sensors? They are 3 FC-04 models. It is not possible to get any analog data from them because I have to physically set the threshold.

The results on your serial monitor indicate that all 3 sensors did indicate sound reception above your threshold.

What sound is being sensed?
How far apart are the 3 sensors?
Is it conceivable in your environment that more than 1 sensor can detect sound above the threshold?
If so, you might need to deal with that in code.

They are 3 FC-04 models.

Clicking on that "link" produces no action.