Pages: [1]   Go Down
Author Topic: How do I record button presses and play them back?  (Read 3850 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I saw this video on youtube of a guy controlling a milling machine using a NES control pad. I'll post a link soon.

I wondered whether the arduino could record a long sequence of button presses, and the amount of time they were pressed, then repeat the sequence at the press of a button. This would eliminate having to translate a CAD file into stepper motor commands, and in essence you would be manually programming the milling machine. You would also need a separate button for record and playback and for moving the machine to its reset position (0,0,0).

I have very limited programming experience, and was wondering what code would be needed to record the input pins while controlling it, then play the stored inputs as outputs to the stepper motors with a single button press. Sorry if this has already been done, but the search feature did not return exactly what I wanted.

Thanks!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the link to the NES control pad controlling a milling machine:


My idea must be really simple to do, but I don't know where to start. :-/
« Last Edit: January 21, 2011, 08:22:57 am by jimmimak » Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It not to dissimilar from a project i'm going to be doing next. A bit different i'm MIDI'ing a only pre MIDI keyboard. It's no standard analog and it's polyphonic so the usual CV rout wouldn't work. Anyway it  involves being able to 'record' the key-press on the keyboard and then also play them back in the same way you would on a games controller i guess. Just on a keyboard you have a lot more keys.

I've decided to unhook the keyboard or keys from the device and replace then with a transistor. The original keys are then fed into the Arduino and in normal mode echo'd straight to the transistor switch. So they act like normal in record mode i will be turning this information into MIDI and sending it to a dedicated MIDI recording device. For play back you are just re-triggering the transistor switches in the pattern you have recorded.

For you you probably want to record it strait into the arduino. this  shouldn't be to difficult as i guess they will be relatively short and not to complicated (in comparison to a piece of music say ) . You going to want to use some kind of array to store incoming key-presses in with a time stamp. Then a way of playing this back
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your project sounds interesting, I like the idea of using the transistors in place of keys.

Another thing that would be useful to me is creating commands for shapes such as circles, curves and angled straight lines as this would be hard to do with a control pad. Then these features could be recalled using different keys on the controller, but thats for future development.  

I'll try and find out about doing it as an array with time stamps. If you have any code for this would you mind posting it here?

I plan to eventually make a milling machine to mill circuit boards and bits of wood and plastic for different projects. I think this method would be the simplest - a cross between a manual and CNC machine!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I wondered whether the arduino could record a long sequence of button presses, and the amount of time they were pressed, then repeat the sequence at the press of a button.
The answer is yes, for some definitions of long and some number of buttons.

The number of buttons is limited by how you read them.

Each time a button release is detected, the button number and duration needs to be stored. If there are less than 255 keys, the key number can be stored in a byte. If the maximum time for a key to be help down is 32 seconds or less, the time can be stored in an int (2 bytes). Otherwise, you need to use a long (4 bytes).

So, from 3 to 6 bytes per press/release. With 2K of SRAM, and not using every last bit of it, you could store 300 to 600 button presses/times.

If you need to store more than that, you need some kind of external storage, such as an SD card.
Logged

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

You could go over a minute with millisecond accuracy using an unsigned int.
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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS -

I will only use 6 keys to control the 3-axis machine.

I guess the time and sample rate could be adjusted or the machine slowed down if I need more accuracy. Do I need to define the time divisions for each bit of information, or is this automatically set? Can you refer me to an example of the code for this?

I would prefer to keep everything running under one ATMega328 without SD cards.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I will only use 6 keys to control the 3-axis machine.
Computers have keyboards with keys. Arduinos have switches.

Reading a switch is easy enough:
int state = digitalRead(pin);

By recording the previous state, in an array, you can determine if this switch is pressed and was pressed (is still held down), is pressed and was released (start timing), is released and was released (forget about it), or is released and was pressed (record that button number and duration (now - then)).
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS -

"By recording the previous state, in an array"

Can you teach me how to do this?

I actually have some switches with keyboard-style tops on them so how would you define them?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can you teach me how to do this?
Code:
int prevState[6];
unsigned long pressStart[6];
int pinNum[] = {2, 3, 4, 5, 6, 7};

void loop()
{
   for(byte i=0; i<6; i++)
   {
      int state = digitalRead(pinNum[i]);
      if(state == HIGH && prevState[i] == LOW)
      {
         // The switch changed to pressed
         pressStart[i] = millis();
      }
      else if(state == HIGH && prevState[i] == HIGH)
      {
         // The switch is still pressed
      }
      else if(state == LOW && prevState[i] == HIGH)
      {
         // The switch was just released
         unsigned long releaseTime = millis();
         unsigned long downTime = releaseTime - pressStart[i];
         // Record i and downTime...
      }
      else if(state == LOW && prevState[i] == LOW)
      {
         // Switch is not and was not pressed
      }
   }
}

Quote
I actually have some switches with keyboard-style tops
It's just me, but I like to be technically accurate. They are still switches. Keys lock and unlock locks. Buttons hold shirts closed. Switches control the flow of current.
Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll gladly post up the code and project when ive done it. I think it would be useful for other people presuming i make something that works smiley-wink

Was going to include auto chord stuff on it as well and simple arpeggiator if i can do it all on one board.
Logged

Pages: [1]   Go Up
Jump to: