Sparki equipped with sound sensor

Hi
So my sound sensor finally arrived for Sparki. Its a really cheap one lol but for what I wanted it to do it works fine after a bit of twiddling, although there is no point in reading this as a voltage, its much easier reading as a raw data.
So here is Sparki 1.1 with his first upgrade:



this is the cheap sound sensor I bought:


It has three pins, one to connect to the 3/5V VCC port, one to connect to the GND port, and one labelled OUT, with connects to one of the analogue ports, in my case A0. On Sparki’s expansion port this is labelled as CE. Obviously, these ports did not align directly with the pins on the sound sensor so I used some female to male flexible jumper wires to connect the board to the new sensor.
Once it was all connected I tucked the wires underneath the LCD and used a tiny piece of blu-tack to hold the wires of the sound sensor to the much larger wires connected to the ultrasonic range finder. This keeps it in place even when turning and moving the servo in different directions.

Here is the code that I have been using to “communicate” with Sparki using the sound sensor. Sparki’s replies are in the form of text printed on the screen and subsequent movements depending on information read by the sensor. It also uses the randomSeed function to read the disconnected A1 port which means that Sparki’s response will be one of three options apparently randomly selected.

#include <Sparki.h>  // include the sparki library

void setup()
{
  sparki.servo(0);
  delay(2000);
  int soundA = analogRead(0); // take a first reading from the sound sensor as I discovered that one reading is not enough to pick up the sound so now Sparki takes 3 readings
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundA); // print out the value read
  sparki.updateLCD();
  delay (1000);
  int soundB=analogRead(0);  // take a second reading
  delay(2000);
   sparki.print(soundB); //display this on screen
  sparki.updateLCD();
  delay (1000);
  int soundC = analogRead(0); //take the third reading
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundC); // print out the value read
  sparki.updateLCD();
  delay (1000);
  
  if ((soundB> 1.26) || (soundC>1.26)) //this gives two opportunities for Sparki to respond to the sound that has been registered. This number is very low but with the low quality sound sensor in my environment, it constantly reads 0 unless there is a noise and then it could be anywhere between 1 and 300
  {
  
  sparki.clearLCD();
  sparki.drawString(10, 1, "hi ash how are you?");
  sparki.updateLCD();
  delay(1000);
  int soundA = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundA);
  sparki.updateLCD();
  delay (1000);
  int soundB=analogRead(0);
  delay(2000);
   sparki.print(soundB);
  sparki.updateLCD();
  delay (1000);
  int soundC = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundC);
  sparki.updateLCD();
  delay (1000);
 
  if((soundB> 1.26) || (soundC>1.26))
  {
 delay(2000);  
    sparki.clearLCD();
  sparki.drawString(10, 1, "affirmative, you have had a good day");
  sparki.updateLCD();
  delay(1000);
  sparki.clearLCD();
  randomSeed(analogRead(1)); // this takes a reading from an unused analogue port as even though it is unused there is still electricity going through it
  int response = random(1, 10); // this translates the reading from the A1 port and transforms it into a number between 1 and 10. With this Sparki's responses can be set up as random
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(1000);
  if (response<3)
  {
  sparki.drawString(20, 2, "when I am happy I dance...I'm dancing!");
  sparki.updateLCD();
  delay(1000);
    sparki.servo(0);
  sparki.moveForward(3);
  sparki.RGB(RGB_GREEN);
  sparki.beep();
  sparki.moveLeft(90);
  sparki.servo(SERVO_RIGHT);
  sparki.RGB(RGB_RED);
  sparki.beep();
  sparki.moveForward(3);
  sparki.servo(SERVO_CENTER);
  sparki.RGB(RGB_GREEN);
  sparki.moveBackward(3);
  sparki.beep();
  sparki.moveRight(90);
  sparki.RGB(RGB_BLUE);
  sparki.servo(SERVO_LEFT);
  sparki.beep();
  delay(2000);
  }
  if ((response>=3) && (response<=6))
  {
    sparki.drawString(20, 2, "here is a bot hug");
    sparki.updateLCD();
    delay(1000);
    sparki.servo(0);
    sparki.ping();
    delay(2000);
    if (sparki.ping() < 10)
    {
      sparki.moveForward(10);
      sparki.gripperClose(4);
      delay(1000);
      sparki.gripperOpen(3);
      delay(1000);
    }
    else
    {
       sparki.servo(90);
    sparki.ping();
    delay(2000);
    if (sparki.ping() < 10)
    {
      sparki.servo(0);
      sparki.moveRight(90);
      sparki.moveForward(10);
      sparki.gripperClose(4);
      delay(1000);
      sparki.gripperOpen(3);
      delay(1000);
    }
    }
  }
  if(response>=7)
  {
    sparki.drawString(20, 2, "la la happy song");
  sparki.updateLCD();
  delay(1000);
  for (int i = 0; i < 3; i=i+1)
                        {delay(400);
			sparki.beep(840, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(640, 800);
			delay(200);
			sparki.beep(440, 800);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
                        }
  }
    }


else
   {
     delay(2000);
   sparki.clearLCD();
  sparki.drawString(10, 1, "oh dear maybe i can cheer you up?");
  sparki.updateLCD();
  delay(1000);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response = random(1, 10);
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(1000);
  if (response<3)
  {
  sparki.drawString(20, 2, "maybe my dance can make you smile");
  sparki.updateLCD();
  delay(1000);
    sparki.servo(0);
  sparki.moveForward(3);
  sparki.RGB(RGB_GREEN);
  sparki.beep();
  sparki.moveLeft(90);
  sparki.servo(SERVO_RIGHT);
  sparki.RGB(RGB_RED);
  sparki.beep();
  sparki.moveForward(3);
  sparki.servo(SERVO_CENTER);
  sparki.RGB(RGB_GREEN);
  sparki.moveBackward(3);
  sparki.beep();
  sparki.moveRight(90);
  sparki.RGB(RGB_BLUE);
  sparki.servo(SERVO_LEFT);
  sparki.beep();
  delay(2000);
  }
  if ((response>=3) && (response<=6))
  {
    sparki.drawString(20, 2, "a robot hug will make you smile");
    sparki.updateLCD();
    delay(1000);
    sparki.servo(0);
    sparki.ping();
    delay(2000);
    if (sparki.ping() < 10)
    {
      sparki.moveForward(10);
      sparki.gripperClose(4);
      delay(1000);
      sparki.gripperOpen(3);
      delay(1000);
    }

    else
    {
       sparki.servo(90);
    sparki.ping();
    delay(2000);
    if (sparki.ping() < 10)
    {
      sparki.servo(0);
      sparki.moveRight(90);
      sparki.moveForward(10);
      sparki.gripperClose(4);
      delay(1000);
      sparki.gripperOpen(3);
      delay(1000);
    }
    }
  }
  if(response>=7)
  {
    sparki.drawString(20, 2, "music is good for the soul");
  sparki.updateLCD();
  delay(1000);
  for (int i = 0; i < 3; i=i+1)
                        {delay(400);
			sparki.beep(840, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(640, 800);
			delay(200);
			sparki.beep(440, 800);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
                        }
  }
    }
   }
}
   
    void loop() // after the initial set up communication phase Sparki transitions to his entertainment mode
{
  sparki.servo(0);
  sparki.clearLCD();
  sparki.drawString(10, 1, "I am in entertainment mode");
  sparki.updateLCD();
  delay(1000);
	sparki.moveLeft(30);
	sparki.moveRight(30);
	delay(1000);
	sparki.beep(640, 200);
	delay(200);
	sparki.beep(440, 200);
	delay(100);
		int lightLeft = sparki.lightLeft();
		int lightCenter = sparki.lightCenter();
		int lightRight = sparki.lightRight();
		int edgeLeft = sparki.edgeLeft();
		int edgeRight = sparki.edgeRight();
                int threshold = 600;
		sparki.servo(0);
		sparki.RGB(0,220,0);
		if(((lightCenter>lightLeft)&&(lightRight>lightLeft)))
		{
  for (int i = 0; i < 3; i=i+1)
  {
			sparki.RGB(RGB_RED);
              delay(500);
            
              sparki.RGB(RGB_ORANGE);
              delay(500);
              
              sparki.RGB(RGB_YELLOW);
              delay(500);
              
              sparki.RGB(RGB_GREEN);
              delay(500);
              
              sparki.RGB(RGB_BLUE);
              delay(500);
              
              sparki.RGB(RGB_INDIGO);
              delay(500);
            
              sparki.RGB(RGB_VIOLET);
              delay(500);
            
              sparki.RGB(RGB_WHITE);
              delay(500);
              
              sparki.RGB(RGB_OFF);
              delay(500);
		}}
		else
		{
			delay(1000);
		}
		if(((lightLeft>lightRight)&&(lightCenter>lightRight)))
		{
			for (int i = 0; i < 3; i=i+1)
                        {delay(400);
			sparki.beep(840, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(640, 800);
			delay(200);
			sparki.beep(440, 800);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
		}}
		else
		{
			delay(1000);
		}
		if(((edgeLeft<threshold)&&(sparki.ping()>10)))
		{
			sparki.RGB(220,0,0);
			sparki.beep(840, 400);
			sparki.moveLeft(90);
			sparki.beep(440, 200);
			sparki.moveRight(90);
			sparki.beep(340, 200);
			delay(100);
			sparki.beep(540, 200);
			sparki.RGB(0,0,220);
			sparki.beep(440, 400);
			sparki.moveForward(5);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(740, 500);
			sparki.moveBackward(5);
			delay(200);
		}
		else
		{
		}
		if(((edgeRight<threshold)&&(sparki.ping()>10)))
		{
			for (int i = 0; i < 3; i=i+1)
                        {sparki.beep(740, 600);
			delay(400);
			sparki.beep(540, 400);
			delay(400);
			sparki.beep(540, 200);
			delay(200);
			sparki.beep(440, 200);
			delay(200);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(740, 200);
			delay(400);
			sparki.beep(740, 200);
		}}
		else
		{
		}
		sparki.servo(0);
		delay(1000);
		if((sparki.ping()<10))
		{
			sparki.moveForward(7);
			sparki.servo(90);
			delay(1000);
			if((sparki.ping()<10))
			{
				sparki.moveRight(90);
				sparki.moveForward(7);
				sparki.servo(-(90));
				delay(1000);
				if((sparki.ping()<10))
				{
					sparki.moveLeft(90);
					sparki.moveForward(7);
				}
				else
				{
					sparki.moveBackward(5);
					sparki.RGB(0,0,220);
					sparki.moveLeft(180);
				}
			}
			else
			{
				sparki.servo(-(90));
				delay(1000);
				if((sparki.ping()<10))
				{
					sparki.moveLeft(90);
					sparki.moveForward(7);
				}
				else
				{
				}
			}
		}
		else
		{
			sparki.servo(90);
			delay(1000);
			if((sparki.ping()<10))
			{
				sparki.moveRight(90);
				sparki.moveForward(7);
			}
			else
			{
				sparki.servo(-(90));
				delay(1000);
				if((sparki.ping()<10))
				{
					sparki.moveLeft(90);
					sparki.moveForward(7);
				}
				else
				{
					sparki.moveForward(5);
					sparki.RGB(0,0,220);
					sparki.moveRight(90);
					sparki.moveLeft(90);
				}
			}
		}
// after the entertainment sequence has been completed there is another opportunity to "talk" to Sparki and see him respond. To do this Sparki again reads the sound three times.
int soundA = analogRead(0); 
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundA);
  sparki.updateLCD();
  delay (1000);
  int soundB=analogRead(0);
  delay(2000);
   sparki.print(soundB);
  sparki.updateLCD();
  delay (1000);
  int soundC = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundC);
  sparki.updateLCD();
  delay (1000);
  
  if ((soundA> 1.26) || (soundB>1.26))
  {
//after re-reading we again use the randomSeed option to decide how Saprki should respond
  randomSeed(analogRead(1));
  int response = random(1, 10);
  if(response<3)
  {
    sparki.clearLCD();
    sparki.drawString(10, 1, "Hello! Do you like my awesome dance?");
    sparki.updateLCD();
    delay(1000);
      sparki.moveLeft(90);
  sparki.moveForward(5);
  sparki.beep(500);
  sparki.moveBackward(5);
  sparki.moveForward(5);
  sparki.moveBackward(5);
  sparki.beep(400);
  sparki.moveForward(5);
  sparki.moveRight(180);
  for (int i = 0; i < 5; i++)
  {
    sparki.beep(600);
  sparki.moveLeft(7);
  sparki.moveRight(14);
  sparki.moveLeft(7);
  }
  sparki.beep(400);
  sparki.moveBackward(5);
  sparki.moveForward(10);
  sparki.beep(600);
  sparki.moveBackward(5);
  sparki.moveLeft(90);
  sparki.RGB(RGB_GREEN);
  delay(2000);
  }
  if ((response<=3) && (response<=6))
  {
    sparki.clearLCD();
    sparki.drawString(10, 1, "Hello! Humans make me happy!");
    sparki.updateLCD();
    delay(1000);
    sparki.moveForward(5);
    sparki.beep(500);
    sparki.moveRight(10);
    sparki.moveLeft(10);
    sparki.beep(350);
    sparki.moveBackward(5);
    sparki.moveLeft(30);
    sparki.beep(600);
    sparki.moveRight(60);
    sparki.beep(400);
  }
  if(response>=7)
  {
    sparki.clearLCD();
    sparki.drawString(10,1, "Hello! I'm fun!");
    sparki.updateLCD();
    delay(1000);
    if (sparki.lightLeft()<sparki.lightRight())
    {
      sparki.moveForward(12);
      sparki.moveRight(90);
      sparki.moveBackward(12);
      sparki.moveLeft(90);
    }
    else
    {
      sparki.beep();
      sparki.moveBackward(12);
      sparki.moveRight(90);
      sparki.moveForward(12);
      sparki.moveLeft(90);
    }
  }
//at this stage of the entertainment mode there is the option to change the mode by confirming with Sparki that you would like him to change mode. Confirming this will instigate autonomous mode. If you do not wish to change mode remain quiet and the entertainment mode sequence will begin again
sparki.clearLCD();
sparki.drawString(20, 2, "Tell me if you would like to change my mode");
sparki.updateLCD();
delay(1000);
int soundA = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundA);
  sparki.updateLCD();
  delay (1000);
  int soundB=analogRead(0);
  delay(2000);
   sparki.print(soundB);
  sparki.updateLCD();
  delay (1000);
  int soundC = analogRead(0);
  delay (2000);
  sparki.clearLCD();
  sparki.print(soundC);
  sparki.updateLCD();
  delay (1000);
  
  if ((soundB> 1.26) || (soundC>1.26))
  {
    sparki.clearLCD();
    sparki.drawString(10, 1, "affirmative, mode is switched");
    delay(1000);
    sparki.drawString(30, 3, "I am now in autonomous mode");
    sparki.updateLCD();
    delay(2000);
    int modeA= 1;
    while (modeA<10)//I used this to make the autonomous mode a loop that will repeat multiple times and I used an increment counter to prevent it from creating an infinite loop, and to allow the mode to be exited after 10 completions, returning to the entertainment mode
    {
      modeA++;
    sparki.servo(0);
		  delay(1000);
 
                  while(sparki.ping()>10)
		  {
			sparki.moveForward();
			sparki.servo(0);
                    }
                        sparki.moveStop();// when Sparki detects an object or a wall less than ten cm in front of him he will measure the distances left and right and move in the direction of the largest clear space
                          sparki.servo(90);
                          delay(1000);
                          int R=sparki.ping();
                          sparki.ping();
                          delay(2000);
                          sparki.clearLCD();
                          sparki.println(R);
                          sparki.updateLCD();
                          delay(1000);
                          sparki.servo(-90);
                          delay(1000);
                          int L = sparki.ping();
                          sparki.ping();
                          delay(2000);
                          sparki.clearLCD();
                          sparki.println(L);
                          sparki.updateLCD();
                          delay(1000);
                          sparki.servo(0);
                          if((R>10) && (R>=L))
                          {
                          sparki.moveRight(90);
                          sparki.servo(0);
                          }
                          if ((L>10) && (L>=R))
                          {
                            sparki.moveLeft(90);
                            sparki.servo(0);
                          }
                         if ((R<10) && (L >10))
                         {
                           sparki.moveLeft(90);
                           sparki.servo(0);
                         }
                         if ((R<10) && (L < 10))
                         {
                           sparki.moveRight(180);
                           sparki.RGB(0,0,220);
                           sparki.servo(0);
                         }
}

                          
			}
}
 
  }