Hiya! First post after lurking around for a while, here goes!
I'm building a somewhat simple, random 'Hotkey Presser' by using a Teensy (which mimics a USB keyboard) and the Arduino programming environment. Basically, I'm imitating this guy: http://blog.makezine.com/archive/2011/04/the-awesome-button.html, but I've replaced his words by letters and numbers and added some more input buttons. Here's the code:
// Defining first category of random keys (letters)
const byte randomLetters = 20;
char* letters[randomLetters] = {
"a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t" };
// Defining second category of random keys (numbers)
const byte randomNumbers = 10;
char* numbers[randomNumbers] = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
// Hello buttons, we will use you. Ha!
int button1 = 0;
int button2 = 1;
int button3 = 2;
void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(button1,INPUT_PULLUP);
pinMode(button2,INPUT_PULLUP);
pinMode(button3,INPUT_PULLUP);
delay(500);
}
void loop (){
if(digitalRead(button1) == LOW) //read buttonpress on button1
{
//generate a random letter, print it to the Mac:
Keyboard.print(letters[random(0,randomLetters)]);
Keyboard.print("");
delay(300); //little delay so we only get 1 letter at a time
}
if(digitalRead(button2) == LOW) //read buttonpress on button2
{
//generate a random letter, print it to the Mac
Keyboard.print(letters[random(0,randomLetters)]);
Keyboard.print("");
delay(300);
}
if(digitalRead(button3) == LOW) //read buttonpress on button3
{
//generate a random number, print it to the Mac
Keyboard.print(numbers[random(0,randomNumbers)]);
Keyboard.print("");
delay(300);
}
}
That works fine and all, but I need a little more functionality. For example, it should not be possible for two exactly the same letters to show up right after another (aa, bb, etc.). So I think this is what it needs to do:
1 - After randomizing, check if the current 'random result' matches the previous random result.
2 - If so, randomize and check again.
3 - If not, print the result, but remember it so we can compare the next result with it.
I was thinking of making oldResult and currentResult variables and somehow fill them with whatever the randomfunction gave and gives, but I miss the code knowhow to actually do this without getting quite a lot of errors. So could anybody help me out a little? : 0
Thanks in advance!