Difficult communication with processing

Hi everyone, i have a hard time making Processing read ascii character from the serial port (being a Arduino UNO)

My code in arduino is, i think, pretty on point: basically, i have 13 buttons, and when one is pressed, Arduino send an ASCII character in a serial way (with the Serial.write function). Each button as a character dedicated to it so Processing can eventually do a single task for each button.

My question is, how do i read the Serial.write communication from arduino with processing, for then to assign a task to processing?

My actual code in arduino (first lines of the void loop)

void loop() {

buttonState = digitalRead(buttonPin1);

if (buttonState == HIGH) {

Serial.write('A'); } else {

Serial.write('L'); }

buttonState = digitalRead(buttonPin2); if (buttonState == HIGH) { Serial.write('B');

} else {

Serial.write('L'); }

Always post ALL the code, using code tags. See the "How to use this forum" post.

My question is, how do i read the Serial.write communication from arduino with processing, for then to assign a task to processing?

First, that is a Processing question, not an Arduino question.

Second, you are spamming Processing unmercifully. You should send data only when the state of a pin changes. See the state change detection example.

@jremington , here it is:

const int buttonPin1 = 1;
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;
const int buttonPin8 = 8;
const int buttonPin9 = 9;
const int buttonPin10 = 10;
const int buttonPin11 = 11;
const int buttonPin12 = 12;
const int buttonPin13 = 13;


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int incomingByte;


void setup() {
  // initialize the LED pin as an output:
  Serial.begin(9600);
  
  pinMode(ledPin, OUTPUT); // l'output doit être une data envoyée a processing  Serial.begin(9600);
  // initialize the pushbutton pin as an input:
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  pinMode(buttonPin9, INPUT);
  pinMode(buttonPin10, INPUT);
  pinMode(buttonPin11, INPUT);
  pinMode(buttonPin12, INPUT);
  pinMode(buttonPin13, INPUT);
  
  
 }


void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    Serial.write('A'); //quelque chose a la place de "ledpin"
  } else {
    // turn LED off:
    Serial.write('L');
  }


  buttonState = digitalRead(buttonPin2);
 if (buttonState == HIGH) {
    Serial.write('B');
    
  } else {
    
    Serial.write('L');
  }
  
  
  
   buttonState = digitalRead(buttonPin3);
   
   if (buttonState == HIGH) {
   
    Serial.write('C'); 
  } else {
   
    Serial.write('L');
  }

   buttonState = digitalRead(buttonPin4);
   
   if (buttonState == HIGH) {

    Serial.write('D');
  } else {

    Serial.write('L');
  }

   buttonState = digitalRead(buttonPin5);
   
   if (buttonState == HIGH) {
  
    Serial.write('E');
  } else {
   
    Serial.write('L');
  }

   buttonState = digitalRead(buttonPin6);
   
   if (buttonState == HIGH) {

    Serial.write('F'); 
  } else {
   
    Serial.write('L');
  }

  
   
   buttonState = digitalRead(buttonPin7);
   
   if (buttonState == HIGH) {
   
    Serial.write('G'); 
  } else {

    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin8);
   
   if (buttonState == HIGH) {
    
    Serial.write('H');
  } else {

    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin9);
   
   if (buttonState == HIGH) {
   
    Serial.write('I'); 
  } else {
   
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin10);
   
   if (buttonState == HIGH) {
   
    Serial.write('J'); 
  } else {
    // turn LED off:
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin11);
   
   if (buttonState == HIGH) {
    
    Serial.write('K'); 
  } else {
    
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin12);
   
   if (buttonState == HIGH) {
    
    Serial.write('M'); 
  } else {
   
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin13);
   
   if (buttonState == HIGH) {
    
    Serial.write('N'); 
  } else {
    
    Serial.write('L');
  }
}

@PaulS, thanks for your answer. What you say is that the second part (else serial.write (L)) isn't essential?

What should i write instead?

Also, my bad, the output isn't the ledpin i forgot to remove that line.

Do some reading on arrays. Using them will make your code very much shorter.

Thank you UKHeliBob!

Only self-proclaimed Arduino forum “experts” write INITIAL cods using arrays, loops etc.
Most mortals, me included, do the initial version the way you did it.
That helps to visualize the patterns and LATER helps to condense the code.
It is called “software development”.

Not sure how posting full code with code tags is of such help.

I believe MorganS recently wrote very nice essay on how to organize “pushing multiple buttons” code.
Sorry , but :confused: OF does not recall the thread. Perhaps Mrs Google could help.

const int buttonPin1 = 1;
Pin 0 and 1 are already used by the USB<>Serial chip, so can’t be used for buttons.

You can use the analogue inputs as digital inputs.
const int buttonPin1 = A0;

Example of a pin array.

const byte buttonPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; // 14 is A0 on an Uno

A button can also be used between pin and ground, with internal pull up enabled (no external resistor).
Used in an array

for (byte i = 2; i < 15; i++) pinMode(buttonPin[i], INPUT_PULLUP);

Leo…

232:
Only self-proclaimed Arduino forum “experts” write INITIAL cods using arrays, loops etc.
Most mortals, me included, do the initial version the way you did it.
That helps to visualize the patterns and LATER helps to condense the code.
It is called “software development”.

IMHO that is called learning a language.

Most mortals, me included, do the initial version the way you did it.

But once you know how useful arrays can be you will use them in your initial code as a matter of course. The code is easier to write and maintain rather than doing masses of typing or repetitive copy/paste/revise cycles to basically repeat the same code over and over.

That helps to visualize the patterns and LATER helps to condense the code.

Rubbish. The patterns are easily visualised with data in arrays and you don't need to go back and change code later and introduce new bugs in the process.

Only self-proclaimed Arduino forum “experts” write INITIAL cods using arrays, loops etc.
Most mortals, me included, do the initial version the way you did it.

What data did you use to come to that assertion? are you trying to feel good because you need to write crappy code first before trying to rewrite everything?

Really it’s not an expert thing, that is one of the fundamental building block of programming and everyone uses that when possible, directly - not as a second thought…

of course that means “thinking a bit before writing a bit of code”… and that is a best practice for programming as much as it is in life (think a bit before acting)

@PaulS, thanks for your answer. What you say is that the second part (else serial.write (L)) isn't essential?

No. What I mean is that, because you send data on every iteration of loop(), if the switch IS pressed or if the switch is NOT pressed, you will be sending a huge volume of data.

What you need to do is send one value when the switch BECOMES pressed, and another value when the switch BECOMES released. Many orders of magnitude less data that way.

Is it a good use of arrays? Specially for the setup?

const byte buttonPin[] = {15, 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //array: remplace ce qui est au dessus


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int incomingByte;


void setup() {

  Serial.begin(9600);

  pinMode (buttonPin[15, 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], INPUT);
  
 }

I don't understand how arrays can help me to reduce the size of the void loop code though.

PaulS: What you need to do is send one value when the switch BECOMES pressed, and another value when the switch BECOMES released. Many orders of magnitude less data that way.

Yeah you're right, processing would be unnecessarily flooded by data in the console. So is there a function that send a understandable signal to processing just once and not 9600 times per second?

I don't understand how arrays can help me to reduce the size of the void loop code though.

Well, pinMode() doesn't take that many arguments...

So is there a function that send a understandable signal to processing just once and not 9600 times per second?

Yes. It is Serial.print(). The key is to call the function just once, not 9600 times per second (or more). I suggested that you look at the state change detection example. Apparently, you haven't, so I'll make the same suggestion again.

Hillcres-Hellio: pinMode (buttonPin[15, 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], INPUT);

read again post #8 to see how to use a for loop (but that code is not OK for the array as @Wawa got confused with the array index and pins... :) )

I don't understand how arrays can help me to reduce the size of the void loop code though.

Put the pin numbers in an array Put the character corresponding to each pin in a second array Use a for loop to read each input in turn. When you find that an input has gone HIGH use the pin number array index to get the corresponding character from the character array and output it

NOTE : you could use an array of structs instead of 2 arrays but I don't think that you are ready for that yet.

Hi everyone, I have a hard time making Arduino communicating with processing:

My circuit is pretty simple: i have 12 buttons related to my arduino uno and i want that each of them make processing do a single task. For example, when buttonpin4 is pressed, processing would launch "beat it.mp3".

Starting with my arduino code below, what should i write in the processing code to make it understand the arduino strings?

Thanks in advance

// constants won't change. They're used here to set pin numbers:
const int buttonPin0 = 15;
const int buttonPin1 = 14;
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;
const int buttonPin8 = 8;
const int buttonPin9 = 9;
const int buttonPin10 = 10;
const int buttonPin11 = 11;
const int buttonPin12 = 12;
const int buttonPin13 = 13;

const byte buttonPin[] = {15, 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //array: remplace ce qui est au dessus


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int incomingByte;


void setup() {

  Serial.begin(9600);

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  pinMode(buttonPin9, INPUT);
  pinMode(buttonPin10, INPUT);
  pinMode(buttonPin11, INPUT);
  pinMode(buttonPin12, INPUT);
  pinMode(buttonPin13, INPUT);

  pinMode (buttonPin[15, 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], INPUT);
  
 }


void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin1);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    Serial.write('A'); //quelque chose a la place de "ledpin"
  } else {
    // turn LED off:
    Serial.write('L');
  }


  buttonState = digitalRead(buttonPin2);
 if (buttonState == HIGH) {
    Serial.write('B');
    
  } else {
    
    Serial.write('L');
  }
  
  
  
   buttonState = digitalRead(buttonPin3);
   
   if (buttonState == HIGH) {
   
    Serial.write('C'); 
  } else {
   
    Serial.write('L');
  }

   buttonState = digitalRead(buttonPin4);
   
   if (buttonState == HIGH) {

    Serial.write('D');
  } else {

    Serial.write('L');
  }

   buttonState = digitalRead(buttonPin5);
   
   if (buttonState == HIGH) {
  
    Serial.write('E');
  } else {
   
    Serial.write('L');
  }

   buttonState = digitalRead(buttonPin6);
   
   if (buttonState == HIGH) {

    Serial.write('F'); 
  } else {
   
    Serial.write('L');
  }

  
   
   buttonState = digitalRead(buttonPin7);
   
   if (buttonState == HIGH) {
   
    Serial.write('G'); 
  } else {

    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin8);
   
   if (buttonState == HIGH) {
    
    Serial.write('H');
  } else {

    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin9);
   
   if (buttonState == HIGH) {
   
    Serial.write('I'); 
  } else {
   
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin10);
   
   if (buttonState == HIGH) {
   
    Serial.write('J'); 
  } else {
    // turn LED off:
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin11);
   
   if (buttonState == HIGH) {
    
    Serial.write('K'); 
  } else {
    
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin12);
   
   if (buttonState == HIGH) {
    
    Serial.write('M'); 
  } else {
   
    Serial.write('L');
  }

  buttonState = digitalRead(buttonPin13);
   
   if (buttonState == HIGH) {
    
    Serial.write('N'); 
  } else {
    
    Serial.write('L');
  }
}

I have executed your program, and I see that the Serial Monitor is showing continuously characters A, B, C, ... without having any key/button pressed down. Figure-1: Example of PUSH and ON/OFF heys

You need to look at the following things in order to define the initial states of your buttons/keys.

1. K1 in Fig-1 is the schematic of a PUSH key or sometimes called Spring Return Key. It is normally an open condition key. You press it, it will get closed; you release it, it will get opened. To define the initial state (button is not closed), you need to make an arrangement with necessary external pull-up/pull-down resistor so that the button state remains at LL-state.

2. K2 in Fig-1 is the schematic of an ON/OFF key or sometimes called Latched Key. It is normally an open condition key. You press it, it gets closed and remains in closed condition until you open it.

  pinMode (buttonPin[15, 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], INPUT);

This statement does not what you think it does.