Need some form of debounce for my code and have no idea how to implement one

edited July 2016 in Arduino

As above i'm currently working on an arduino project (physical edrum trigger/piezo) i currently have an uno talking to processing 3 through a serial connection and then displaying a simple graph, when the inByte is greater than 1 (pull down resistor is at 0 untriggered) it sends midi notes to ezdrummer 2, i have all this working my problem is when i hit the drum it triggers multiple times within a short period of the strike, anyone know what would be the best solution for a debounce?

my current code below.

import processing.serial.*;

import themidibus.*; 

MidiBus myBus; 

Serial myPort;        
int xPos = 1;         
float inByte = 0;

void setup () {
  MidiBus.list();
  // set the window size:
  size(900, 400);

  println(Serial.list());
  myBus = new MidiBus(this, 1, 3); 

  myPort = new Serial(this, Serial.list()[1], 115200);

  myPort.bufferUntil('\n');

  background(0);
}

void draw () {
  // draw the line:
  stroke(127, 34, 255);
  line(xPos, height, xPos, height - inByte);

  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    background(255,255,255);
  } else {
    // increment the horizontal position:
    xPos++;
  }
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int and map to the screen height:
    inByte = int(inString);

    println(inByte);
    int a = 56;
    inByte = map(inByte, 0, 1023, 0, 127);
    int test = int(inByte);
    if(inByte > 2){
      myBus.sendNoteOn(1, 38, test); // Send a Midi noteOn
      delay(1);
      inByte--;
      myBus.sendNoteOff(1, 38, test); // Send a Midi nodeOff
    }
  }
}

Answers

Sign In or Register to comment.