Touch sensor Part 1

So as you can probably tell from my emphasis on entertainment robots, communication and interaction with robotics is one of my favourite aspects and so when it came to purchasing additional sensors for Sparki I went straight for those that would increase the level of human physical interaction.

So I purchased a touch sensor.

416ogj2n4UL

This is pretty awesome in that it only has 3 pins and connects easily to Sparki’s expansion port.

GND to the GND port, VCC to the VCC port and one labelled SIG. Now the output can either be connected to one of Sparki’s analogue ports, or it can be connected to one of the digital ports, either way it still works fine.

Expansion-Port-Diagram
Sparki’s Expansion Port diagram from http://arcbotics.com/products/sparki/parts/expansion-port/

So what did I use this new sensor for??? Well two things mainly and you will notice this is part 1 so I will explain the other use in part 2.

My first aim was to be able to physically communicate and interact with Sparki using this new additional sensor and so I wrote a really simple looping code (that gets a bit boring!) in which touching Sparki’s sensor invokes one of three reactions, much like the use of touch sensor in children’s electronic toys. Here is the code:

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

void setup()
{
  sparki.servo(0);
}

void loop()
{
sparki.beep();
delay(250);
sparki.beep();
sparki.clearLCD();
sparki.drawString(10, 1, "Hello, human");
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawString(10, 1, "Shake my hand");
sparki.updateLCD();
delay(3000);
int shake=analogRead(8);
 sparki.print(shake);
      sparki.updateLCD();
      delay(1000);
       if(shake>100)
      {
      sparki.beep();
      sparki.gripperClose(6);
      delay(1000);
      sparki.gripperOpen(6);
      delay(1000);
    sparki.clearLCD();
sparki.drawString(10,2, "yey friends!");
sparki.updateLCD();
delay(3000);
      }
      else
      {
          sparki.clearLCD();
sparki.drawString(10,2, "Nevermind");
sparki.updateLCD();
delay(3000);
      }
           for (int i = 0; i < 5; i=i+1)
  {
      sparki.clearLCD();
sparki.drawString(10, 1, "Let's shake again");
sparki.updateLCD();
delay(3000);
int shake=analogRead(8);
 sparki.print(shake);
      sparki.updateLCD();
      delay(1000);
       if(shake>100)
      {
      randomSeed(analogRead(1));
  int response = random(1, 10);
  sparki.clearLCD();
  sparki.print(response);
  sparki.updateLCD();
  delay(1000);
  if (response<=3)
  {
    sparki.clearLCD();
  sparki.drawString(20,2,"This is my happy song");
  sparki.updateLCD();
  delay(2000);
      sparki.beep(440, 600);
			delay(400);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(740, 200);
			delay(200);
			sparki.beep(440, 200);
			delay(200);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(540, 200);
			delay(400);
			sparki.beep(740, 200);
  }
  if ((response>3) && (response<=6))
  {
    sparki.clearLCD();
  sparki.drawString(10, 1, "Ok lets see who gets the highest number");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  sparki.drawString(10, 1, "1 2 3 4 5 6 7 8 9 10");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  sparki.drawString(10, 1, "Here is your number");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  randomSeed(analogRead(1));
  int response1 = random(1, 10);
  sparki.clearLCD();
  sparki.print(response1);
  sparki.updateLCD();
  delay(1000);
   sparki.clearLCD();
  sparki.drawString(10, 1, "This is my number");
  sparki.updateLCD();
  delay(1000);
   randomSeed(analogRead(1));
  int response2 = random(1, 10);
  sparki.clearLCD();
  sparki.print(response2);
  sparki.updateLCD();
  delay(1000);
  if (response1>response2)
  {
    sparki.clearLCD();
    sparki.drawString(20,2, "Well done your number was the highest");
    sparki.updateLCD();
    sparki.beep();
    delay(2000);
  }
  if (response1<response2)
  {
        sparki.clearLCD();
    sparki.drawString(20,2, "Yey! I won! Victory dance!");
    sparki.updateLCD();
    delay(1000);
    sparki.beep();
  sparki.moveRight(14);
  sparki.moveLeft(7);
  
  sparki.moveLeft(7);
  sparki.moveRight(14);
  sparki.moveLeft(7);
  sparki.beep();
  sparki.moveLeft(7);
  sparki.moveRight(14);
  
  }
  if (response1==response2)
  {
      sparki.clearLCD();
    sparki.drawString(20,2, "It was a draw!");
    sparki.updateLCD();
    delay(2000);
  }
}
if (response>6)
{
  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.beep();
              
              sparki.RGB(RGB_BLUE);
              delay(500);
              
              sparki.RGB(RGB_INDIGO);
              delay(500);
            
              sparki.RGB(RGB_VIOLET);
              delay(500);
              sparki.beep();
            
              sparki.RGB(RGB_WHITE);
              delay(500);
              
              sparki.RGB(RGB_OFF);
              delay(500);
		}

}
 else
      {
          sparki.clearLCD();
sparki.drawString(10,2, "Nevermind");
sparki.updateLCD();
delay(3000);
      }
  }
}

Obviously this code could be improved greatly but it gives a clear indication of my goals for Sparki. I will be using similar code to work on my conversation and entertainment robot ideas that you will see in a few weeks, particularly paired with the sound sensor I also purchased.

There is no video of this code (it would be pretty hard to film and use the touch sensor simultaneously) but here is a photo of the connected sensor 🙂

DSC_0427

Advertisements

3 thoughts on “Touch sensor Part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s