amurison718
Published © GPL3+

Build a Better Bear

How much can a SparkFun Pro Micro do?

IntermediateFull instructions provided1,007
Build a Better Bear

Things used in this project

Story

Read more

Schematics

EMIC 2 Speech chip

Light Sensor

DF Mini Player

Eyes

Feet Sensor

Feet Sensor

Adafruit FX board

Horns

Motor

Paw

Tilt

Code

Code snippet #1

Plain text
// initialise variables and constants
// Arduino pins 

#define redpin  5
#define greenpin 6 
#define bluepin  9


void eyes(int r, int g, int b)
{ 
// writes to pins to change eye colours
analogWrite(redpin, r);
analogWrite(greenpin, g);
analogWrite(bluepin, b); 
}

void rollseyes()
{
// loops to change eye colours

int colour;
eyes(0, 0, 0);

for (colour = 0; colour < 255; colour++)
 { 
// blue fades, red brightens
 eyes(colour, 0, 255 - colour);
 delay(10);
}

for (colour = 0; colour < 255; colour++)
{
// red fades, green brightens    
eyes(255 - colour, colour, 0); 
delay(10);
}

for (colour = 0; colour < 255; colour++)
{
// green fades, blue brightens    
eyes(0, 255 - colour, colour);
delay(10);
 }
}

void setup() 
{
/* output pins */
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
}

void loop() 
{

rollseyes();

}

Code snippet #2

Plain text
#include <softwareserial.h>
SoftwareSerial FXserial(7, 8); // RX | TX


void growl()
{
// Use software serial to tell Adafruit FX card
// to play a sound
  FXserial.println("#03");
  delay(500);
}

void fart()
{
// Use software serial to tell Adafruit FX card
// to play a sound
  FXserial.println("#02");
  delay(500);
}

void burp()
{
// Use software serial to tell Adafruit FX card
// to play a sound
  FXserial.println("#04");
  delay(500);
}

void laugh()
{
// Use software serial to tell Adafruit FX card
// to play a sound 
  FXserial.println("#00");
  delay(500);
}

void setup() {
// Initialise Bobbs on start up</p><p>// Serial communications with  
// sound card
  FXserial.begin(9600);
}

void loop(){
	fart();
	burp();
	laugh();
	growl();
}</p>

Code snippet #3

Plain text
/*  New file order - clear SD, download new files, YAFS */

#include "SoftwareSerial.h"

# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

SoftwareSerial MP3player(10, 9); // RX, TX</

void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void play(int file)
{
  int state = LOW;
while (state == LOW)
  {
    state = digitalRead(busy);
  }

  execute_CMD(0x03, 0, file);
  delay(100);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Execute the command and parameters
{
  // Calculate the checksum (2 bytes)
  word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
  // Build the command line
  byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
                            Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte
                          };
  //Send the command line to the module
  for (byte k = 0; k < 10; k++)
  {
    MP3player.write( Command_line[k]);
  }

  delay(100);

}

void setup() {

  MP3player.begin(9600);
  setVolume(21);
  play(34);
}

void loop() {
// play a random track between 1 and 49
      track = random(1, 50;

}

Code snippet #4

Plain text
#include <softwareserial.h>
<SoftwareSerial emicserial(2, 3); // RX | TX

void speak(String message)
{
// Send message to speech chip 
  emicserial.print("S");
  emicserial.print(message);
  emicserial.write("\n");

}

void setup() {

// Serial communications with speech chip
  emicserial.begin(9600);

// Set volume for speech
  emicserial.print('V16');
  emicserial.print('\n');

void loop() 
{
   speak("Hello");
}

Code snippet #5

Plain text
#include <SoftwareSerial.h>

SoftwareSerial BTserial(14, 15); // RX | TX

void instruction()
{
// Decode instructions from BT device and
// take action

  command = BTserial.read();
  int commandvalue = command.toInt();
  switch (commandvalue)
  {
    case 71: // G
      growl();
      break;
    case 84: // T
      getmessage();
      break;
    case 70: // F
      fart();
      break;
    case 66: //B
      burp();
      delay(500);
      speak("Better out than in");
      break;
    case 69: // E
      speak("All the colours of the bow man");
      rollseyes();
      break;
    case 83: // S
      shake();
      break;
    case 76: // L
      laugh();
      break;
  }

void setup() {

// Initialise Bobbs on start up
// Serial communications with BT device 
  BTserial.begin(9600);
}

void loop() {
// BT device instruction received?


  if (BTserial.available())
  {
    instruction();
  }

Code snippet #6

Plain text
#define motorpin  10

void motor(int turns)

{
// Active motor pin to turn on motor briefly
  for (int i = 0; i < turns; i++)
  {
    digitalWrite(motorpin, HIGH);
    delay(2);
    digitalWrite(motorpin, LOW);
  }
}

void setup() {

pinMode(motorpin, OUTPUT);

digitalWrite(motorpin, LOW);

}

void loop() {

motor(10);

}

Code snippet #7

Plain text
#define tiltpin  21

void tiltactions()
{
// Randomly select an action when Bobbs in tilted

  choice = random(6);
  eyes(255, 0, 0);
  growl();
  delay(750);
  switch (choice)
  {
    case 0:
      speak("Whats happening");
      delay(750);
      break;
    case 1:
      speak("This is not cool");
      delay(750);
      break;
    case 2:
      speak("Human I need help");
      delay(750);
      break;
    case 3:
      speak("This sucks");
      delay(750);
      break;
    case 4:
      speak("I'm going to puke");
      delay(750);
      break;
    case 5:
      speak("Make it stop");
      delay(750);
      break;
  }
  horns(1, 10, 5); // colour, repeat, speed
}

void setup() {
pinMode(tiltpin, INPUT);
}

void loop() {

// Is Bobbs tilited?

  if (tilt > 500)
  {
    tiltcount += 1;
    if (tiltcount > 500)
    {
      tiltactions();
      delay(1000);
      timethen = timenow;
    }
  }
  else
  {
    tiltcount = 0;
  }

}

Code snippet #8

Plain text
#define lightpin  20
int light = 0;

void darkactions()

{
// Randomly select an action to perform when it's dark

  choice = random(5);
  eyes(0, 0, 255);

  switch (choice)
  {
    case 0:
      speak("Is that the moon");
      delay(1500);
      FXserial.println("#06");
      break;
    case 1:
      sleepnoise();
      sleepnoise();
      break;
    case 2:
      speak("Who turned off the lights");
      delay(1500);
      break;
    case 3:
      speak("Ooh creepy");
      delay(750);
      break;
    case 4:
      speak("It's dark");
      delay(750);
      break;
  }
  horns(3, 10, 5); // colour, repeat, speed
}

void setup() {

pinMode(lightpin, INPUT);

}

void loop() {

light = analogRead(lightpin);

// Is it dark?

  if (light > 900) // dark
  {
    darkactions();
    delay(1000);
    timethen = timenow;
  }

}

Code snippet #9

Plain text
#define feetpin 19

void feet()

{
// Interactive feet
// Bobbs asks for his feet to be tickled
// user must respond within a set period of
// time to activate eyes, horns and make Bobbs laugh</p><p>  int feetnow = 0;</p><p>  speak("Tickle my feet");
  eyes (0, 0, 0);</p><p>  timethen = millis();</p><p>  while ( millis() < timethen + 5000)
  {
    for (int i = 0; i < 200; i++)
    {
      feetnow = feetnow + analogRead(feetpin);
    }
    feetnow = feetnow / 200;</p><p>    if ( feetnow < 0)
    {
      FXserial.println("#00"); // play the laugh sound on the Adafruit board
      eyes(255, 215, 0);
      motor(100);
      horns(1, 10, 5); // colour, repeat, speed
    }
  }
}
void setup(){

pinMode(feetpin, INPUT);

}
void loop(){

feet();

}

Code snippet #10

Plain text
#define pawpin  18

void pawthing()

{
// Interactive paws
// Bobbs asks for his hands to be shaken
// The user must respond in time to activate
// motor, eyes and horns

  int paw = 0;
  speak("Give me high five");
  eyes (0, 0, 0);

  timethen = millis();
  while ( millis() < timethen + 5000)
  {
    paw = analogRead(pawpin);

   if (paw < 650)
    {

        speak("Purrr");
        horns(2, 5, 5);
      }

      eyes(255, 215, 0);
      delay(500);
      motor(100);
    }
  }


void setup() {

pinMode(pawpin, INPUT);
}

Code snippet #11

Plain text
#define hornpin 16
#define hornresetpin 4

void setup{
pinMode(hornpin, OUTPUT);
pinMode(hornresetpin, OUTPUT);

digitalWrite(hornresetpin, LOW);

}


void resethorns(){
// Reset decade counter chip to turn off horn lights
  digitalWrite(hornresetpin, HIGH);
  delay(10);
  digitalWrite(hornresetpin, LOW);
}

void horns(int col, int repeat, int speed)
{
// Sets the colour of the horn lights by
// cycling through the decade counter pins
// really quickly and stopping at the 
// chosen colour
// parameters are colour, number of times to
// repeat the sequence and speed</p><p>  int start;
  int end;
  int limit;

 for (int i = 0; i < repeat; i++)
  {
    resethorns();

    switch (col)
    {
      case 0:
        limit = 10;
        break;
      case 1: //red
        limit = 3;
        break;
      case 2: //green
        limit = 3;
        for (int k = 0; k < 3; k++) // skip red
        {
          digitalWrite(hornpin, LOW);
          delay(1);
          digitalWrite(hornpin, HIGH);
          delay(1);
        }
        break;
      case 3: // blue
        limit = 3;
        for (int k = 0; k < 7; k++) // skip red and green
        {
          digitalWrite(hornpin, LOW);
          delay(1);
          digitalWrite(hornpin, HIGH);
          delay(1);
        }
        break;
    }

   for (int j = 0; j < limit; j++)
    {
      digitalWrite(hornpin, LOW);
      delay(speed);
      digitalWrite(hornpin, HIGH);
      delay(speed);
    }
  }

 resethorns();
}

Credits

amurison718

amurison718

4 projects • 2 followers

Comments