Problem with reading a byte from the serial port
in
Integration and Hardware
•
4 months ago
Hi guys.
I've tried several solutions for this problem but I can't reach my target.
I'm creating an electronic drum with Arduino.
The code in Arduino works well so I haven't problems in it.
Then I transmit the byte I've assembled in a program written in Processing (from the serial port) but there is something wrong.
Sometimes Processing gives me some errors or it works but in the wrong way.
I've got some problems with reading the byte from Arduino and discompose it in a string of 8 bit.
This is my code in Processing with some extra information because I tried other solutions in the same code:
- import processing.serial.*;
- import ddf.minim.*;
- PImage img;
- Minim minim;
- AudioSnippet snare,hiHat,tomH,tomM,floorT,crash;
- String SPORT = Serial.list()[1];
- Serial port;
- boolean limit=false;
- int PADres;
- int hwe=10;
- int[] v = new int[7];
- /*int v[2]={1,2};
- int i=1;*/
- void setup(){
- for(int i=0;i<7;i++) {
- v[i]=0;
- }
- port = new Serial(this, SPORT, 9600);
- port.bufferUntil('\n');
- size(600, 600);
- img = loadImage("drum.jpg");
- background(img);
- minim = new Minim(this);
- /*if(port.available()>0){
- snare = minim.loadSnippet("kit"+v[i]+"/snare.wav");*/
- snare = minim.loadSnippet("kit1/1snare.wav");
- tomH = minim.loadSnippet("kit1/2tomhigh.wav");
- tomM = minim.loadSnippet("kit1/3tommed.wav");
- floorT = minim.loadSnippet("kit1/4floortom.wav");
- hiHat = minim.loadSnippet("kit1/5hihat.wav");
- crash = minim.loadSnippet("kit1/6crash.wav");
- }
- /*void draw(){
- serialEvent(port);
- }
- void serialEvent (Serial miaporta) {
- String PADres = port.readStringUntil('\n');
- if (PADres != null) {
- PADres = trim(PADres);
- float inByte = float(PADres);
- byte dati = byte(inByte);
- for(int i=0;i<7;i++) {
- converti(dati);
- }
- if (port.available() > 0){
- //byte(PADres);
- //dati=PADres;
- //PADres=PADres>>2;
- if(v[0]==1){//if((dati&00000001)==1){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(250,280,hwe,hwe);
- snare.play();
- snare.rewind();
- }else if(v[0]==0){
- stroke(1);
- ellipseMode(CENTER);
- ellipse(250,280,hwe,hwe);
- }
- if(v[1]==1){//if((dati&00000010)==2){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(284,213,hwe,hwe);
- tomH.play();
- tomH.rewind();
- }else{
- stroke(1);
- ellipseMode(CENTER);
- ellipse(284,213,hwe,hwe);
- }
- if(v[2]==1){//if((dati&00000100)==4){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(370,210,5,5);
- tomM.play();
- tomM.rewind();
- }else{
- stroke(1);
- ellipseMode(CENTER);
- ellipse(370,210,hwe,hwe);
- }
- if(v[3]==1){//if((dati&00001000)==8){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(410,285,hwe,hwe);
- floorT.play();
- floorT.rewind();
- }else{
- stroke(1);
- ellipseMode(CENTER);
- ellipse(410,285,hwe,hwe);
- }
- if(v[4]==1){//if((dati&00010000)==16){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(150,200,hwe,hwe);
- hiHat.play();
- hiHat.rewind();
- }else{
- stroke(1);
- ellipseMode(CENTER);
- ellipse(150,200,hwe,hwe);
- }
- if(v[5]==1){//if((dati&00100000)==32){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(215,105,hwe,hwe);
- crash.play();
- crash.rewind();
- }else{
- stroke(1);
- ellipseMode(CENTER);
- ellipse(215,105,hwe,hwe);
- }
- }
- }
- }*/
- void draw(){
- //int dati=PADres;
- //PADres[0]=0;
- //println(Serial.list());
- if (port.available() > 0){
- for(int i=0;i<7;i++) {
- v[i]=0;
- }
- PADres=port.read();
- for(int i=0;i<7;i++) {
- converti(PADres);
- }
- //byte(PADres);
- //dati=PADres;
- //PADres=PADres>>2;
- if(v[0]==1){//if((PADres&00000001)==1){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(250,280,hwe,hwe);
- snare.play();
- snare.rewind();
- }else if(v[0]==0){
- stroke(1);
- ellipseMode(CENTER);
- ellipse(250,280,hwe,hwe);
- }
- if(v[1]==1){//if((PADres&00000010)==2){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(284,213,hwe,hwe);
- tomH.play();
- tomH.rewind();
- }else if(v[1]==0){
- stroke(1);
- ellipseMode(CENTER);
- ellipse(284,213,hwe,hwe);
- }
- if(v[2]==1){//if((PADres&00000100)==4){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(370,210,5,5);
- tomM.play();
- tomM.rewind();
- }else if(v[2]==0){
- stroke(1);
- ellipseMode(CENTER);
- ellipse(370,210,hwe,hwe);
- }
- if(v[3]==1){//if((PADres&00001000)==8){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(410,285,hwe,hwe);
- floorT.play();
- floorT.rewind();
- }else if(v[3]==0){
- stroke(1);
- ellipseMode(CENTER);
- ellipse(410,285,hwe,hwe);
- }
- if(v[4]==1){//if((PADres&00010000)==16){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(150,200,hwe,hwe);
- hiHat.play();
- hiHat.rewind();
- }else if(v[4]==0){
- stroke(1);
- ellipseMode(CENTER);
- ellipse(150,200,hwe,hwe);
- }
- if(v[5]==1){//if((PADres&00100000)==32){
- stroke(0);
- ellipseMode(CENTER);
- ellipse(215,105,hwe,hwe);
- crash.play();
- crash.rewind();
- }else if(v[5]==0){
- stroke(1);
- ellipseMode(CENTER);
- ellipse(215,105,hwe,hwe);
- }
- }
- }
- void stop(){
- snare.close();
- hiHat.close();
- tomH.close();
- tomM.close();
- floorT.close();
- crash.close();
- minim.stop();
- super.stop();
- }
- void converti(int n){
- int z=n;
- for(int i=0;i<7 && z!=0;i++){
- v[i]=z%2;
- z=z/2;
- }
- }
1