Been looking for a project to do with my Arduino for a while and had thoughts on some sort of desktop robot thing that would move about and you could interact with n stuff but was unsure what exactly I wanted to do. Then I saw this instructable:

http://www.instructables.com/id/Twitch-Your-Robotic-Pet/

Loved it straight away and imediately got on to sourcing the components. These wernt expensive at all and pretty soon had all the parts I needed.

I decided not to use the sound module for mine, not yet. I was going to build this bit by bit to get the hang of it and learn what exactly was going on.

First thing was to get the Matrix working, if I cant then the project is dead in the water right!

Following this tutorial https://learn.adafruit.com/adafruit-led-backpack/1-2-8x8-matrix theres a fair bit of soldering to be done, probably the hardest was soldering the matrix to its backpack but steadily does it and its done.

Now to run one of the basic examples and wire it up. Not working. Took an age to figure this out until by chance I spotted these pins.

I was getting confused about which pins to use from the tutorial but spotting these I connected it up and to my amazement, It's working.

Next to try out the servos. These were pretty easy. Using the basic examples, I got these moving how I wanted. Now on to the twitch code.

I stripped out the code to do with playing sounds and receiving input. Had to swap the servo library with servo.h but apart from that it all seemed to be working!

It was running really fast for some reason, so I added a delay in the main loop and Stumpy was in action.

Was so pleased with this, such a great project and all the credit thus far goes to the original author.

Refactoring

Now it was working and I had some idea of how it all worked, I took a look at some refactoring of the code. Mainly with a view to adding some new features. One of which was to ad the ability to run a sequence of animations, so for example Stumpy could look around and return to center or nod or shake his head. Im thinking this could be achieved through an array of set positions with a pause inbetween, possibly!

Anyways, the main loop of code in the original consists of 4 blocks of code, one for each mood. Each block is about 95% the same with some different values for eye movement ranges etc.Im thinking I could cut this down to one block and and array of moods. This block would just work on the element of the array for that mood. Each mood would have an eye sequence and some other stuff.

struct Mood{
    int8_t xmin;
    int8_t xmax;
    int8_t ymin;
    int8_t ymax;
    int8_t follow;
    uint8_t  eye[5][8];
  };

Well, after some work, I've got my plan in place and a quite sizeable array of mood structures with just one block of code in the loop controlling this.

Sequences

Ive added a struct which is an eye x and y pos, speed and delay and wether to follow. I can have an array of these to define an animation sequence. Would have liked each mood struct to have its own set of sequences but memory doesnt allow so im creating these as global and a mood can use which ever ones we decide.

struct EyeSequence{
    int8_t Xpos;
    int8_t Ypos;
    int8_t sp;
    int8_t hold;
    bool follow;
  };
 
struct SequenceSet{
    int8_t seqNum;
    int8_t totFrames;
    EyeSequence Set[4];
  };

With this in place I managed to create some basic animations such as nod 'yes' and shake 'no'.

Wasnt going to use the touch sensor as the original author used. Instead added some buttons that I could use to interact with Stumpy.

1 press for a 'pat', this will improve his mood (or make him angry) and a 1 second hold for a 'magic 8 ball' style yes or no response.

 

Note. Twinky was his working title and has since been renamed

 

Adding sound

Time to think about adding sounds. Ive included all the relevant libs and its taken the memory useage well over 110%.

I've been looking at ways of cutting down my memory useage and while i can make small gains here and there its not enough. A major rethink is required.

I think all of this is coming from my lovely mood and animation structures. Admitedly there is a bit of wastage there as I'm defining arrays the size of the largest animation sequence even though I might be using a smaller one.

Nothing more to it other than chop it all back and rebuild bit by bit.

virtual pet blog