can i have ins and out being used at the same time

hello im trying to get a potentiometer to control the level of an LED through max if that makes sense. could someone tell me if this is actually possible. so far the potentiometer value comes in but the LED just glitches.

Code below cheers

int ledPina = 11;
int ledPinb = 10;
int ledPinc = 9;

int vala;
int valb;
int valc;

int nextval = 1;

int x = 0;
int ledpin = 13;

void setup()
{
Serial.begin(9600);
pinMode(ledPina, OUTPUT);
pinMode(ledPinb, OUTPUT);
pinMode(ledPinc, OUTPUT);
}

void loop()
{

if (Serial.available()) {
if(nextval == 1) {

vala = Serial.read();
analogWrite(ledPina, vala);
nextval = 2;
}
}

if (Serial.available()) {
if(nextval == 2) {

valb = Serial.read();
analogWrite(ledPinb, valb);
nextval = 3;
}
}

if (Serial.available()) {
if(nextval == 3) {

valc = Serial.read();
analogWrite(ledPinc, valc);
nextval = 4;
}
}

if (Serial.available()) {
if(nextval == 4) {
nextval = 1;

//send sensors ///////////////////////////////////

if (Serial.read() == ‘r’) { // If an ‘r’ is received then read the pins

for (int pin= 0; pin<=5; pin++){ // Read and send analog pins 0-5
x = analogRead(pin);
sendValue (x);
}

for (int pin= 2; pin<=13; pin++){ // Read and send digital pins 2-13
x = digitalRead(pin);
sendValue (x);
}

Serial.println(); // Send a carriage returnt to mark end of pin data.
delay (5); // add a delay to prevent crashing/overloading of the serial port

}

//send sensors ///////////////////////////////////
}
}

}

void sendValue (int x){ // function to send the pin value followed by a “space”.
Serial.print(x);
Serial.print(32, BYTE);
}

heres the max code

max v2;

N vpatcher 406 85 1042 874;

P origin -67 511;

P user swatch 171 -131 128 32;

P window setfont "Sans Serif" 9.;

P window linecount 1;

P newex 80 191 43 196617 delay 4;

P number 403 642 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 338 642 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 273 642 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 208 642 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 143 642 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 78 642 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P button 80 223 15 0;

N vpatcher 289 44 1127 677;

P outlet 99 316 15 0;

P window setfont "Sans Serif" 9.;

P window linecount 1;

P newex 107 210 31 196617 spell;

P window linecount 0;

P message 90 241 20 196617 13;

P newex 90 127 27 196617 t b l;

P inlet 90 70 15 0;

P connect 0 0 1 0;

P fasten 1 0 2 0 95 235 95 235;

P fasten 2 0 4 0 95 286 104 286;

P fasten 3 0 4 0 112 286 104 286;

P connect 1 1 3 0;

P pop;

P newobj 1 214 53 196617 p convert;

P newex 338 757 29 196617 s a4;

B color 14;

P newex 403 757 29 196617 s a5;

B color 14;

P newex 273 757 29 196617 s a3;

B color 14;

P newex 208 757 29 196617 s a2;

B color 14;

P newex 143 757 29 196617 s a1;

B color 14;

P newex 78 757 29 196617 s a0;

B color 14;

N vpatcher 582 197 863 594;

P window setfont "Sans Serif" 9.;

P newex 137 226 64 196617 fromsymbol;

P newex 137 182 40 196617 itoa;

P newex 137 148 61 196617 zl group 78;

P newex 137 84 53 196617 sel 10 13;

N comlet out;

P outlet 137 298 26 0;

N comlet in;

P inlet 137 37 15 0;

P connect 0 0 2 0;

P fasten 2 2 3 0 184 124 142 124;

P fasten 2 1 3 0 163 124 142 124;

P connect 3 0 4 0;

P connect 4 0 5 0;

P connect 5 0 1 0;

P pop;

P newobj 78 472 73 196617 p convertback;

P newex 78 530 52 196617 route list;

P newex 403 671 49 196617 change 0;

B color 14;

P newex 338 671 49 196617 change 0;

B color 14;

P newex 273 671 49 196617 change 0;

B color 14;

P newex 208 671 49 196617 change 0;

B color 14;

P newex 143 671 49 196617 change 0;

B color 14;

P newex 78 671 49 196617 change 0;

B color 14;

P newex 78 588 663 196617 unpack i i i i i i i i i i i i i i i i i i;

P message 1 164 14 196617 r;

P toggle 1 81 15 0;

P newex 1 128 52 196617 metro 15;

P number 203 184 32 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 170 184 31 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 138 184 31 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P newex 138 224 44 196617 bondo 3;

P user uslider 138 32 18 128 254 1 0 0;

P user uslider 170 32 18 128 254 1 0 0;

P comment 128 -5 32 196617 Red;

P comment 166 -6 32 196617 Green;

P number 203 11 32 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 170 11 31 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P number 138 11 31 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

P newex 138 -26 75 196617 unpack 0 0 0;

P user uslider 203 32 18 128 254 1 0 0;

P newex 138 261 71 196617 serial a 9600;

P comment 201 -7 32 196617 Blue;

P connect 16 0 15 0;

P connect 15 0 17 0;

P connect 17 0 33 0;

P connect 1 0 26 0;

P connect 26 0 25 0;

P connect 25 0 18 0;

P connect 18 0 35 0;

P connect 35 0 19 0;

P fasten 19 0 27 0 83 738 83 738;

P fasten 15 0 41 0 6 156 85 156;

P connect 41 0 34 0;

P connect 42 0 3 0;

P connect 3 0 4 0;

P connect 4 0 10 0;

P connect 10 0 12 0;

P connect 12 0 11 0;

P connect 34 0 1 0;

P connect 33 0 1 0;

P connect 11 0 1 0;

P fasten 11 1 1 0 160 256 143 256;

P fasten 11 2 1 0 177 256 143 256;

P connect 18 1 36 0;

P connect 36 0 20 0;

P fasten 20 0 28 0 148 740 148 740;

P fasten 13 0 11 1 175 211 160 211;

P fasten 3 1 5 0 175 -15 175 -15;

P connect 5 0 9 0;

P connect 9 0 13 0;

P fasten 14 0 11 2 208 211 177 211;

P fasten 3 2 6 0 207 -15 208 -15;

P connect 6 0 2 0;

P connect 2 0 14 0;

P connect 18 2 37 0;

P connect 37 0 21 0;

P fasten 21 0 29 0 213 740 213 740;

P connect 18 3 38 0;

P connect 38 0 22 0;

P fasten 22 0 30 0 278 740 278 740;

P connect 18 4 39 0;

P connect 39 0 23 0;

P fasten 23 0 32 0 343 740 343 740;

P connect 18 5 40 0;

P connect 40 0 24 0;

P fasten 24 0 31 0 408 740 408 740;

P pop;

Its not clear what you want to happen and what your sketch is doing wrong. It would help if you could post a simplified sketch that just has the minimum code to demonstrate the problem. A clear description of what you are observing and what you want to happen will also make it easier to help you.

---------- EDIT ----------

Apologies,

What I am asking is: is it possible to have bi-directional communication over the serial link?

I want to be able to have maxMSP controlling three led's on the arduino and also have maxMSP receiving input from the analogue pins.

The code I have above can achieve each of these things separately but I cannot work out how to make the two work simultaneously.

Many thanks in advance,

Jimmeh

Yes it is possible, but it can be a little tricky to make sure everything stays in sync. Why not start off by sending debug text back to max indicating what the arduino logic is doing.

For example where you have arduino code that receives info:

if (Serial.available()) { 
    if(nextval == 1) { 
     
    vala = Serial.read(); 
    analogWrite(ledPina, vala);

You could do something like:

if (Serial.available()) { 
    if(nextval == 1) { 
     
    vala = Serial.read(); 
  // analogWrite(ledPina, vala);
  Serial.print(vala);
  Serial.println (“ received for val 1”);

Then you should be able to see if the arduino code is doing what you expect it to