Serial Communication Triggering Event Timing

Triggering sounds based on Serial commands using a capacitive touch board with arduino. I have the serial communication working properly, but sounds are being played after my finger releases from the board rather right when I touch it.

Arduino Code

#include <Wire.h>
#include "Adafruit_MPR121.h"

Adafruit_MPR121 cap = Adafruit_MPR121();

uint16_t currtouched = 0;

void setup() {

void loop() {

currtouched = cap.touched();
if (currtouched & (1<<1))
  } else if (currtouched & (1<<2))
  } else if (currtouched & (1<<3))
  } else if (currtouched & (1<<4))
  } else 



#include "ofApp.h"
void ofApp::setup(){
    vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
    sound.load("Pacman Eating Cherry.mp3");
    int baud = 57600;

void ofApp::update(){
    while (serial.available())
        myByte = serial.readByte();
        printf("myByte is %d \n", myByte);
        if (myByte == 49)
  ; //in the console 49 prints correctly once '1' is pressed on the capacitive board, but the sound is not played until my finger is released.


#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

		void setup();
		void update();
		void draw();
		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    ofSerial serial;
    ofSoundPlayer sound;
    int myByte = 0;

My final goal is to play a sound while myByte = 49, and stop playing it once the finger is released. To reiterate, myByte is correctly being set to 49 once my finger touches the board, but the sound is not played until I move my finger off the board and myByte goes from 49 to 48.


Im not too sure how the sound library works, but what I am thinking is since you have your finger on the board, you are sending repetitive 1s (or 49s) and every time you check if mybyte is 49, you are triggering the sound to play from the start perhaps?

Change your check to
if (myByte == 49)
printf(“myByte is %d \n”, myByte); //<-------------add this;

and see if your console is logging repetitive readings. My guess is that you are only able to hear your sound on the last check on 49 since there are no more signals causing it to ‘restart’ again.

1 Like

Ah yes, completely skipped my mind. That was definitely it. Was able to create a quick fix by setting sound.setMultiPlay(true). Thanks for the help!

1 Like