# Code for TouchOSC and Arduino doesn't work

**URL:** <https://discourse.processing.org/t/code-for-touchosc-and-arduino-doesnt-work/16627>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [December 25, 2019, 11:23am UTC](https://discourse.processing.org/t/code-for-touchosc-and-arduino-doesnt-work/16627 "2019-12-25T11:23:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikeAelbrecht](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MikeAelbrecht](https://discourse.processing.org/u/MikeAelbrecht)\
**Post date:** [December 25, 2019, 11:23am UTC](https://discourse.processing.org/t/code-for-touchosc-and-arduino-doesnt-work/16627/1 "2019-12-25T11:23:15Z")

</div>

This is my Processing code:

import oscP5._; // Load OSC P5 library  
import netP5._; // Load net P5 library  
import processing.serial.\*; // Load serial library

Serial arduinoPort; // Set arduinoPort as serial connection  
OscP5 oscP5; // Set oscP5 as OSC connection

int redLED = 0; // redLED lets us know if the LED is on or off  
int [] led = new int [2]; // Array allows us to add more toggle buttons in TouchOSC  
int blueLED = 0;

void setup() {  
size(100,100); // Processing screen size  
noStroke(); // We don’t want an outline or Stroke on our graphics  
oscP5 = new OscP5(this,8000); // Start oscP5, listening for incoming messages at port 8000  
arduinoPort = new Serial(this, Serial.list()[0], 9600); // Set arduino to 9600 baud  
}

void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message

```
String addr = theOscMessage.addrPattern(); // Creates a string out of the OSC message
if(addr.indexOf("/1/toggle") !=-1){ // Filters out any toggle buttons
  int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
  led[i] = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
// Button values can be read by using led[0], led[1], led[2], etc.

}

if(addr.indexOf("/2/toggle") !=-1){ // Filters out any toggle buttons
  int o = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
  led[o] = int(theOscMessage.get(0).floatValue()); // Puts button value into led[o]
// Button values can be read by using led[0], led[1], led[2], etc.
 
}

```

}

void draw1() {  
background(50); // Sets the background to a dark grey, can be 0-255

if(led[1] == 0){ // If led button 1 if off do…  
arduinoPort.write(“r”); // Sends the character “r” to Arduino  
redLED = 0; // Sets redLED color to 0, can be 0-255  
}  
if(led[1] == 1){ // If led button 1 is ON do…  
arduinoPort.write(“R”); // Send the character “R” to Arduino  
redLED = 255; // Sets redLED color to 255, can be 0-255  
}  
fill(redLED,0,0); // Fill rectangle with redLED amount  
ellipse(50, 50, 50, 50); // Created an ellipse at 50 pixels from the left…  
// 50 pixels from the top and a width of 50 and height of 50 pixels  
}

void draw2(){  
background(50);  
if(led[2] == 0){ // If led button 1 if off do…  
arduinoPort.write(“b”); // Sends the character “b” to Arduino  
blueLED = 0; // Sets redLED color to 0, can be 0-255  
}  
if(led[2] == 1){ // If led button 1 is ON do…  
arduinoPort.write(“B”); // Send the character “B” to Arduino  
blueLED = 255; // Sets redLED color to 255, can be 0-255  
}  
fill(blueLED,0,0); // Fill rectangle with redLED amount  
ellipse(50, 50, 50, 50); // Created an ellipse at 50 pixels from the left…  
// 50 pixels from the top and a width of 50 and height of 50 pixels

}

and this is my Arduino code:

int message = 0; // This will hold one byte of the serial message  
int redLEDPin = 11; // What pin is the red LED connected to?  
int redLED = 0;// The value/brightness of the LED, can be 0-255  
int blueLEDPin = 7;  
int blueLED = 0;

void setup() {  
Serial.begin(9600); //set serial to 9600 baud rate  
}

void loop(){  
if (Serial.available() \> 0) { // Check if there is a new message  
message = Serial.read(); // Put the serial input into the message

if (message == ‘R’){ // If a capitol R is received…  
redLED = 255; // Set redLED to 255 (on)  
}  
if (message == ‘r’){ // If a lowercase r is received…  
redLED = 0; // Set redLED to 0 (off)  
}

}

if (Serial.available() \> 0) { // Check if there is a new message  
message = Serial.read(); // Put the serial input into the message

if (message == ‘B’){ // If a capitol R is received…  
blueLED = 255; // Set redLED to 255 (on)  
}  
if (message == ‘b’){ // If a lowercase r is received…  
blueLED = 0; // Set redLED to 0 (off)  
}

}  
analogWrite(redLEDPin, redLED); // Write an analog value between 0-255  
analogWrite(blueLEDPin, blueLED); // Write an analog value between 0-255

}

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 25, 2019, 11:41am UTC](https://discourse.processing.org/t/code-for-touchosc-and-arduino-doesnt-work/16627/2 "2019-12-25T11:41:10Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

please post a short code what is reduced to the problem

- not mix 2 protocols…
- make a minimal test code for arduino read write  
( yes, make the arduino send back what it gets ( echo )  
and show it on the processing console )

* * *

the arduino code you show has 2  
`if (Serial.available() > 0) {`  
so there might be no chance it ever sees a B/b

- please ignore processing, write a new arduino code  
and test it from the arduino IDE monitor console.

After that works start again with processing.
