Help for my automatic crib rocker

Hi,
This is my first Arduino project and as I will have a little girl in a few weeks, I wanted to try to do an automatic crib rocker !
It roughly works but sometimes the servo starts without reason and I guess it’s a problem of stability of the signal. But as I told before, I really new in the field so I would be happy to get your critical help! Any advise is welcome!
My electronic schema is attached and my code is below :

#include <Servo.h>
Servo servo1;

int micPin = 0; //mic. amp to pin 0 analog.
int pot_vitPin = 4; // pot analog 4.
int pot_anglePin = 5; // 10K pot on pin 5 analog.
int swPin = 3; // mode switch analog
int pot_vit_v = 0; // value from pot vitesse
int pot_angle_v = 0; //value from pot angle
int pos_init = 80; // servo in rest. pos 80.
int compteur_cris =0;

void setup()
{
servo1.attach(10);
Serial.begin(9600);
servo1.write((pos_init));
}
////////////////////////////////////
void loop()
{
  
if(analogRead(swPin)>=1000) {cberce();}
if(analogRead(swPin)<1000) {
  if (ecoute()==true){
  compteur_cris = compteur_cris +2;
}
  if (compteur_cris>0){
    compteur_cris--;}
    delay(10);
    if (compteur_cris>=6){
    berce(10);
    compteur_cris = 4;}
  }
}
//////////////////////////////////////
void cberce()
{while (analogRead(swPin)>=1000){
pot_angle_v=analogRead(pot_anglePin);
pot_angle_v = map(pot_angle_v, 0, 1023, 0, 80); // to control speed...
pot_vit_v=analogRead(pot_vitPin);
pot_vit_v = map(pot_vit_v, 0, 1023, 0, 20);
int pos_max = pos_init+pot_angle_v;
int pos_min = pos_init-pot_angle_v;
for (int i=pos_min; i<=pos_max; i+= 1){
servo1.write(i);
delay(pot_vit_v);}
for (int i=pos_max; i>=pos_min; i-=1){
servo1.write(i);
delay (pot_vit_v);}
}
servo1.write(pos_init);
}
////////////////////////////////
void berce(int nb_berce)
{ 
  for (int j=0; j<nb_berce; j++) {
  pot_angle_v=analogRead(pot_anglePin);
pot_angle_v = map(pot_angle_v, 0, 1023, 0, 80); // to control speed...
pot_vit_v=analogRead(pot_vitPin);
pot_vit_v = map(pot_vit_v, 0, 1023, 0, 20);
int pos_max = pos_init+pot_angle_v;
int pos_min = pos_init-pot_angle_v;
for (int i=pos_min; i<=pos_max; i+= 1){
servo1.write(i);
delay(pot_vit_v);}
for (int i=pos_max; i>=pos_min; i-=1){
servo1.write(i);
delay (pot_vit_v);}
  }
  servo1.write(pos_init);
}
//////////////////////
bool ecoute()
{
int trigger =0;

for (int i=0; i <= 250; i++){
  if (analogRead(micPin) <= 20){
  trigger++;}
 
  if (trigger >= 10){
    return true;}
    delay(10);
}
delay(10);
return false;
}

Thaks for your help !

In the schema, Détect Mic means Sound Sensor like this one : http://www.dx.com/p/3-6v-sound-sensor-module-for-arduino-153949#.VHsqYmeab3E

It roughly works but sometimes the servo starts without reason and I guess it's a problem of stability of the signal.

Interesting project!

Suggestion: Try using Serial.print (to a PC) to display (capture to a log file, Teraterm can do this) the control variables and input value for further analysis... Problem resolution. It can be really difficult to second-guess why a control program malfunctions if you are not able to view a snapshot of the values (variables) that went into the algorithm.

Ray

try to write in a more readable style that supports the program flow, I did it for you
(using CTRL-T in the IDE is very fast)

some small changes made too

#include <Servo.h>

Servo servo1;

const int micPin = A0; 
const int pot_vitPin = 4; // pot analog 4.
const int pot_anglePin = 5; // 10K pot on pin 5 analog.
const int swPin = 3; // mode switch analog

int pot_vit_v = 0; // value from pot vitesse
int pot_angle_v = 0; //value from pot angle
int compteur_cris =0;

const int pos_init = 80; // servo in rest. pos 80.

void setup()
{
  servo1.attach(10);
  Serial.begin(9600);

  servo1.write(pos_init );
}

////////////////////////////////////
void loop()
{
  if(analogRead(swPin)>=1000) 
  {
    cberce();
  }
  else 
  {
    if (ecoute()==true)
    {
       compteur_cris = compteur_cris + 2;
    }
    if (compteur_cris > 0)
    {
      compteur_cris--;
    }
    delay(10);
    if (compteur_cris >= 6)
    {
      berce(10);
      compteur_cris = 4;
    }
  }
}

//////////////////////////////////////
void cberce()
{
  while (analogRead(swPin) >= 1000)
  {
    pot_angle_v=analogRead(pot_anglePin);
    pot_angle_v = map(pot_angle_v, 0, 1023, 0, 80); // to control speed...

    pot_vit_v=analogRead(pot_vitPin);
    pot_vit_v = map(pot_vit_v, 0, 1023, 0, 20);

    int pos_max = pos_init + pot_angle_v;
    int pos_min = pos_init - pot_angle_v;

    for (int i=pos_min; i<=pos_max; i+= 1)
    {
      servo1.write(i);
      delay(pot_vit_v);
    }
    for (int i=pos_max; i>=pos_min; i-=1)
    {
      servo1.write(i);
      delay (pot_vit_v);
    }
  }
  servo1.write(pos_init);
}

////////////////////////////////
void berce(int nb_berce)
{ 
  for (int j=0; j<nb_berce; j++) 
  {
    pot_angle_v=analogRead(pot_anglePin);
    pot_angle_v = map(pot_angle_v, 0, 1023, 0, 80); // to control speed...

    pot_vit_v=analogRead(pot_vitPin);
    pot_vit_v = map(pot_vit_v, 0, 1023, 0, 20);

    int pos_max = pos_init+pot_angle_v;
    int pos_min = pos_init-pot_angle_v;

    for (int i=pos_min; i<=pos_max; i+= 1)
    {
      servo1.write(i);
      delay(pot_vit_v);
    }
    for (int i=pos_max; i>=pos_min; i-=1)
    {
      servo1.write(i);
      delay (pot_vit_v);
    }
  }
  servo1.write(pos_init);
}


//////////////////////
bool ecoute()
{
  int trigger =0;

  for (int i=0; i <= 250; i++)
  {
    if (analogRead(micPin) <= 20)
    {
      trigger++;
    }
    if (trigger >= 10)
    {
      return true;
    }
    delay(10);
  }
  delay(10);
  return false;
}

note that berce and cberce are almost identical functions, you might want to merge them into one.

I expect your code also react on noise…

I noticed with Serial.println that my signal on the switch is not stable at all. Any comment to help me to stabilize the signal ?

The sound detector has an adjustable threshold and an indicator LED. You can check from that if it is randomly going off, as well as information in the serial print.

tibel6: as I will have a little girl in a few weeks, I wanted to try to do an automatic crib rocker!

You have much to learn, and I am not talking about coding! :astonished:

Paul__B: You have much to learn, and I am not talking about coding! :astonished:

For sure but I have to find a way to spend my time before ;-P

ChilliTronix: The sound detector has an adjustable threshold and an indicator LED. You can check from that if it is randomly going off, as well as information in the serial print.

The problem is not on the sound detector. In fact, there is a toggle switch (pin A3) which decides whether the servo should rock continuously crock() or listen to the detector. When the switch is closed, I get value between 850 and 1023, usually around 950 and when the switch is open I get value between 650 and 1000, usually around 800. As you noticed, sometimes it overlaps but I don't know why ? When my switch is open, value should be near 0, isn't it ?

Firstly with a switch you should be reading digital not analogue second you should use either a pull down resistor or the internal pullup otherwise the pin will float.

tibel6:
For sure but I have to find a way to spend my time before :stuck_out_tongue:

I would be practicing nappy changing and stocking up on sleep. 8)

I agree with ChilliTronix. Do some research in what he says. I ran into a similar issue and converted all inputs as “input_pullup” whenever possible.

Also if that doesn’t work research a bit more on sampling the input into an array and getting the mode of the input. More than likely a simple array of 9 will return the true value and eliminate the noise such as a furnace turning on or a dropped glass from the other room.

Search “Arduino MODE” you should see lots of examples sensing values into an array.

Attached is the code I grabbed from my research with a bit of tweaking for what I needed.

Take it as it is with standard disclaimers. It did what I needed it to do and eventually I removed it. But it did work.

Mode sample.txt (5.13 KB)

bonjour tibel6,

ton sujet parait ancien mais j'ai le meme projet, si tu as abouti, peux tu partager ton code,

merci d'avance

mes topics: https://forum.arduino.cc/index.php?PHPSESSID=1be7oeicj2n80ce6d3v54ifcq4&topic=524950.0 https://forum.arduino.cc/index.php?topic=524951.0