Sound Sensor Inclusion Help

Hi everybody,

I am a complete beginner to coding so i apologise for any silly questions.

I am in the process of creating an animatronic set of eyes for University. I have used a good code (attached) to make the eyes move organically but I would like to use a sensor the start the movement. To that end I have purchased a Linker Sound Sensor and cable from Maplin and need help including it into the code (attached) and how to attach it to the Arduinno Uno.

I have found some sample code below but don’t know how to include it code for sensor.

Any advice or help anyone can offer would be greatly appreciated, I’ve spent ages trying to work it out :slight_smile:

#include <math.h>

#define pi    3.14159265358979323846
#define twopi (2*pi)
float circleradius = 30; // No more than 30 either side 
float stepnumber = 360;
float stepangle;

//======================================

#include <Servo.h> //include servo library for servo control 

Servo horServo; //servo for left/right movement
Servo vertServo; //servo for up/down movement

byte randomhor; //define random horizontal position variable
byte randomvert; //define random vertical position variable
int randomdelay; //define random delay variable

#define HLEFTLIMIT 0 //define left limit on horizontal (left/right) servo
#define HRIGHTLIMIT 70 //define right limit on horizontal (left/right) servo

#define VTOPLIMIT 0//define top limit on vertical (up/down) servo
#define VBOTLIMIT 70 //define bottom limit on horizontal (up/down) servo

void setup() 
{ 
  horServo.attach(8); //horizontal servo on pin 8
  vertServo.attach(9); //vertical servo on pin 9
  randomSeed(analogRead(0)); //Create some random values using an unconnected analog pin


  //=====================================================
  //Roll Eyes :D
  //=====================================================
  stepangle = twopi/stepnumber;
  for(int i = 0; i<stepnumber; i++){
    float angle = i*stepangle;
    float x = sin(angle)*circleradius;
    float y = cos(angle)*circleradius;

    x = map(x, 1-circleradius, circleradius, 0, 2*circleradius);
    y = map(y, 1-circleradius, circleradius, 0, 2*circleradius);

    horServo.write(x); //write to the horizontal servo
    vertServo.write(y); //write to the horizontal servo

    delay(10);
  }
  //=====================================================
} 

void loop() 
{ 
  randomhor = random(HLEFTLIMIT, HRIGHTLIMIT); //set limits
  randomvert = random(VTOPLIMIT, VBOTLIMIT); //set limits
  randomdelay = random(400, 700); //moves every 400 to 700 milliseconds

  horServo.write(randomhor); //write to the horizontal servo
  vertServo.write(randomvert); //write to the vertical servo
  delay(randomdelay); //delay a random amount of time (within values set above)
}

So…managed to get the code to verify but not sure if it will start the simulation…

#include <math.h>

#define pi 3.14159265358979323846
#define twopi (2*pi)
float circleradius = 30; // No more than 30 either side
float stepnumber = 360;
float stepangle;
//======================================

const int ledPin = 12; // the number of the LED pin
const int thresholdvalue=400;//The threshold to turn the led on
//======================================

#include <Servo.h> //include servo library for servo control

Servo horServo; //servo for left/right movement
Servo vertServo; //servo for up/down movement

byte randomhor; //define random horizontal position variable
byte randomvert; //define random vertical position variable
int randomdelay; //define random delay variable

#define HLEFTLIMIT 0 //define left limit on horizontal (left/right) servo
#define HRIGHTLIMIT 70 //define right limit on horizontal (left/right) servo

#define VTOPLIMIT 0//define top limit on vertical (up/down) servo
#define VBOTLIMIT 70 //define bottom limit on horizontal (up/down) servo

void setup()
{
pinMode(ledPin, OUTPUT);
horServo.attach(8); //horizontal servo on pin 8
vertServo.attach(9); //vertical servo on pin 9
randomSeed(analogRead(0)); //Create some random values using an unconnected analog pin

//=====================================================
//Roll Eyes :smiley:
//=====================================================
stepangle = twopi/stepnumber;
for(int i = 0; i<stepnumber; i++){
float angle = i*stepangle;
float x = sin(angle)*circleradius;
float y = cos(angle)*circleradius;

x = map(x, 1-circleradius, circleradius, 0, 2circleradius);
y = map(y, 1-circleradius, circleradius, 0, 2
circleradius);

horServo.write(x); //write to the horizontal servo
vertServo.write(y); //write to the horizontal servo

delay(10);
}
//=====================================================
}

void loop()
{
int sensorValue = analogRead(A0);//use A0 to read the electrical signal
if(sensorValue>thresholdvalue)
digitalWrite(ledPin,HIGH);//if the value read from A0 is larger than 400,then light the LED
delay(20);
digitalWrite(ledPin,LOW);

randomhor = random(HLEFTLIMIT, HRIGHTLIMIT); //set limits
randomvert = random(VTOPLIMIT, VBOTLIMIT); //set limits
randomdelay = random(200, 600); //moves every 400 to 600 milliseconds

horServo.write(randomhor); //write to the horizontal servo
vertServo.write(randomvert); //write to the vertical servo
delay(randomdelay); //delay a random amount of time (within values set above)
}

 if(sensorValue>thresholdvalue)
  digitalWrite(ledPin,HIGH);//if the value read from A0 is larger than 400,then light the LED
  delay(20);
  digitalWrite(ledPin,LOW);

Do you expect to see a LED that lights up for 20ms?

Hi AWOL,

No I don't need an LED to light up I am attempting to use the sensor to activate the eye mechanism i.e.:

I need to make a noise and the code will start to make the eyes move :)

Found this code too but don't know if it'll do the trick to start the programme running ")

const int SENSOR = 0; // sensor connected to pin 0 int val = 0; // stores the value connected to sensor

void setup() { Serial.begin(9600); // opens serial port to communicate with CPU // data gets sent back to computer at 9600 // bits per second }

void loop() { val = analogRead(SENSOR); // read the value of the sensor

Serial.println(val); // print the value of the sensor to the // serial port delay(100); // delay 100 milliseconds between each send }

Does this make it any clearer what your code is doing?

void loop() 
{ 
  int sensorValue = analogRead(A0);//use A0 to read the electrical signal
  if(sensorValue>thresholdvalue)
  {
     digitalWrite(ledPin,HIGH);//if the value read from A0 is larger than 400,then light the LED
  }
  delay(20);
  digitalWrite(ledPin,LOW);
  
  randomhor = random(HLEFTLIMIT, HRIGHTLIMIT); //set limits
  randomvert = random(VTOPLIMIT, VBOTLIMIT); //set limits
  randomdelay = random(200, 600); //moves every 400 to 600 milliseconds

  horServo.write(randomhor); //write to the horizontal servo
  vertServo.write(randomvert); //write to the vertical servo
  delay(randomdelay); //delay a random amount of time (within values set above)
}