Makey Makey on Leonardo - Rapid fire keystrokes

This project seems to have been popular a few years ago, so it was a lot of work hunting down the code to get this running. I have set up a voltage divider circuit using almost 1 megaohm resistors (they are chunky).

The only issue is that I get a burst of values, which would be hard for playing something like a piano. One suggestion I had was to put a delay somewhere in here, but I also read that it’s not the right solution.

I also don’t understand what the two if statements are for. The first one says it’s for sensitivity, and the other one is some sort of logic statement?

#include <Keyboard.h>

#include <MovingAverageFilter.h>
//original values were 200 and then 600 
//
MovingAverageFilter movingAvarageFilter1(20);
MovingAverageFilter movingAvarageFilter2(20);

boolean check = false;

void setup() {

  Serial.begin(115200);     
}

void loop() {        

  float output1 = movingAvarageFilter1.process(analogRead(0));       
  float output2 = movingAvarageFilter2.process(analogRead(1));

  if (output1 < 600 ) {   // you can change this parameter to fine tune the sensitivity
    if (!check){         
      Keyboard.print("d");         
      Serial.println(output1); 
      check = !check;   
    }         
  }

  if (output1 > 300) {     
    if (check){   
      check = !check;   
    }     
  }

  if (output2 < 600 ) {   // you can change this parameter to fine tune the sensitivity
    if (!check){         
      Keyboard.print("s");         
      Serial.println(output2);           
      check = !check;   
    }         
  }

  if (output2 >300) {     
    if (check){               
      check = !check;   
    }     
  }

}

I also don’t understand what the two if statements are for.

There are 8 if statements in the code. Which 2 don’t you understand?

We have no idea what you have connected to the analog pins.

yes a circuit would help.

and correct I made a mistake, I am confused about these four if statements

 if (output1 < 600 ) {   // you can change this parameter to fine tune the sensitivity
    if (!check){         
      Keyboard.print("d");         
      Serial.println(output1); 
      check = !check;   
    }         
  }

  if (output1 > 300) {     
    if (check){   
      check = !check;   
    }     
  }

Move the Serial.println() statement up above the first if statement in that snippet. Make adjustments to the still mysterious thing connected to the analog pin. See what gets printed.

It should soon become obvious what the first if statement is doing.

The second is testing a boolean variable. If the value read from the analog pin has gone above the threshold (the 600), and it was not above before (check is false), check is set to true and some other stuff happens.

check is set to false when the value read from the analog pin drops below 300.

Explicit assignments, rather than toggling, might have made that clearer.

ok i have a better idea of what's going on in here now, thank you. i'm just wondering how i can get it to limit the burst of keystrokes/prints now?

The Makey Makey doesn't use analog inputs. It uses 22 MOhm pull-up resistors and counts the number of '1' bits in the last 24 samples (about 60 samples per second). Default threshold is 55% (13 samples ON).

You can find the firmware here:

johnwasser:
The Makey Makey doesn't use analog inputs. It uses 22 MOhm pull-up resistors and counts the number of '1' bits in the last 24 samples (about 60 samples per second). Default threshold is 55% (13 samples ON).

You can find the firmware here:
https://github.com/sparkfun/MaKeyMaKey/tree/master/firmware/Arduino/makey_makey

Thanks - do you think I would still be able to find a way to end the burst of keystrokes with the method i've used above? It seems like i got so far with the direction I went in, just needs one final step. i am tempted to keep tinkering with delays on this one even though i didn't have much luck with them before.

mrtunes:
Thanks - do you think I would still be able to find a way to end the burst of keystrokes with the method i’ve used above?

Sure. I think the problem is that you send a keystroke whenever the value is between 300 and 600. Typically you would want a dead zone between “Pressed” and “Released”. I could not find the MovingAverageFilter library in the library manager so I installed “movingAvg” and used that instead.

#include <Keyboard.h>
#include <movingAvg.h>


// Original values were 200 and then 600
const int PressedMaxThreshold = 412;
const int ReleasedMinThreshold = 612;
const byte PinCount = 2;


const byte InputPins[PinCount] = {A0, A1};
const char KeyCodes[PinCount] = {'d', 's'};


struct TouchInput
{
  byte analogPin;
  char keycode;
  movingAvg filter = movingAvg(20);
  boolean wasPressed = false;
};


TouchInput Pins[PinCount];


void setup()
{
  Serial.begin(115200);


  for (int i = 0; i < PinCount; i++)
  {
    Pins[i].analogPin = InputPins[i];
    Pins[i].keycode = KeyCodes[i];
    Pins[i].filter.begin();
  }
}




void loop()
{
  for (int i = 0; i < PinCount; i++)
  {
    float currentAverage = Pins[i].filter.reading(analogRead(Pins[i].analogPin));
    boolean previousState = Pins[i].wasPressed;
    boolean currentState = previousState; // Default if in the dead zone


    if (currentAverage < PressedMaxThreshold)
    {


      currentState = true;      // Pressed


    }
    else if  (currentAverage > ReleasedMinThreshold)
    {


      currentState = false;      // Released
    }


    if (currentState != previousState)
    {
      if (currentState)
        Keyboard.press(Pins[i].keycode);
      else
        Keyboard.release(Pins[i].keycode);
    }
    Pins[i].wasPressed = currentState;
  }
}