Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Serial Communication Triggering Event Timing

$
0
0

@Caleb_Carithers wrote:

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() {
  Serial.begin(57600);
}

void loop() {

currtouched = cap.touched();
if (currtouched & (1<<1))
  {
    Serial.print('1');
  } else if (currtouched & (1<<2))
  {
    Serial.print('2');
  } else if (currtouched & (1<<3))
  {
    Serial.print('3');  
  } else if (currtouched & (1<<4))
  {
    Serial.print('4');  
  } else 
  {
    Serial.print('0');  
  }

}

ofApp.cpp

#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
    serial.listDevices();
    vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
    
    sound.load("Pacman Eating Cherry.mp3");
    sound.setVolume(0.75f);
    
    int baud = 57600;
    serial.setup(1,baud);
}

//--------------------------------------------------------------
void ofApp::update(){
    
    while (serial.available())
    {
        myByte = serial.readByte();
        printf("myByte is %d \n", myByte);
        
        if (myByte == 49)
        {
            sound.play(); //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.
        }
        
    }
}

ofApp.h

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

	public:
		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.

Thanks

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929