Pages: [1]   Go Down
Author Topic: Simple programmable toy?  (Read 796 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 18
Nth. Canty - NZ
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As a kid I remember seeing "robots" that had a simple keypad program system... I think they were put in a program mode, and it stored keypad presses and then played them back as motor drive... eg forward three, forward_right 2, back 4 etc....

I was thinking of making a keypad of 6 directional and perhaps 2 other momentary on buttons, set up as analogue inputs with different resistance per button so I could do the input with only 2 or 3 analogue inputs..

I hope to drive an old RC car - nice & simple and a great way to get the kids interested too.

One of the things I'm not sure of is how to create a program mode and an execute mode.... I was thinking of defining a set of 30 or so variables
instruction 1
instruction 2 etc.

As for input I was thinking of holding one of the directional buttons and pressing a "program" button to save the variable (one of 6 for the possible directions) and then an execute button or switch to "play" the variables back to the motor and (I think) voicecoil steering in the donor toy.. but how to create that I have no idea...

The other thing is the playback, my skills are old and very rusty and I was thinking of just creating a giant, ugly "if" series testing each variable until the drive command matched up...

Does anyone have some friendly advice or perhaps knowledge of a link to this sort of project?
Logged

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 159
Posts: 2895
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sounds like a pretty ambitious project as a "first" project for someone who
has been away from this stuff for a while. I'd try a few simple sketches first
to learn Arduino and get into the swing of things.

Then get a keypad, and follow the info on the Arduino site. Then go from there.
Life is all a big learning curve. One thing at a time.

http://www.arduino.cc/playground/Main/KeypadTutorial


Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 18
Nth. Canty - NZ
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cheers bud, yeah I have been playing heaps...

I'm pretty sure I have the logic for the motor & steering control figured... but the storage & playback of an array has me a little bamboozled.
Logged

Offline Offline
Sr. Member
****
Karma: 6
Posts: 429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

BigTrak Jr?
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 18
Nth. Canty - NZ
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats exactly the sort of thing I'm hoping to build!  Thanks Lakes.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would suggest using arrays and not variables. This will make things a lot easier.
Suppose you store each command in 'char' variable. Then to store up to 30 commands you need an array of 'char's:

char commands[30];
char commands_num = 0; // this is the actual number of commands

storing commands will look something like this:

// receive the new command here
char new_command = ...
// add the command to you commands array
if ( commands_num < 30 )
{
    commands[commands_num] = new_command;
    ++commands_num;
}
else
{
 // no space left for commands
}


Executing the commands will look like:
for(int i=0; i<commands_num; ++i)   
{
   char current_command = commands;
   // put here the code to execute the current_command
}
Note, the executing runs from the beginning of the array till the number of commands stored in it in the program mode.

HTH
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25899
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please use code tags when posting code, so that it doesn't degenerate into italics.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A note about command processing.

You may use if..else  or  switch  statements of course. I suggest more elegant solution using arrays.

If you understand the solution with arrays, then processing commands can be implemented in the similar way.
Suppose you have 3 commands. Each command will have its unique id starting form 0 (zero) to 2.
You can store a function that will execute the command in the array;

Code:
// This function will execute command 0
void exec_command0()
{
}

void exec_command1()
{
}
// This function will execute command 2
void exec_command2()
{
}

// Note that all above functions have the same prototype: they receive void and return void
// We now need to define a type of this functions
typedef void (*command_executor_type)();

// Now we define array of those functions
command_executor_type exec_commands[] =
{
    exec_command0,
    exec_command1,
    exec_command2
};

Now, in order to run the command, according it's id
1. first access the corresponding array entry using '[ ]'. for command with id 0 (zero) it would be
exec_commands[0]
2. The entry, stored in the entry of array is function (actually the address of function, but that does not matter). We call functions using '()':
exec_commands[0]();


Code:
void loop()
{
    char curr_command = 0; // get command id here 0-2
    
    // execute the command
    exec_commands[curr_command]();
}

Logged

Pages: [1]   Go Up
Jump to: