# Code android e arduino

**URL:** https://discourse.processing.org/t/code-android-e-arduino/26364
**Category:** Electronics (Arduino, etc.)
**Tags:** homework
**Created:** [December 18, 2020, 12:02pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364 "2020-12-18T12:02:28Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)
#### Post date: [December 18, 2020, 12:02pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/1 "2020-12-18T12:02:28Z")

</div>

But on processig for Android it is possible that no one has ever used a slider code and an LED to drive Arduino via bluetooth it seems very strange to me

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 18, 2020, 2:03pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/2 "2020-12-18T14:03:01Z")

</div>

🤔

care to document context and what is the real question?

---

<div class="post-metadata">

### Author: ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)
#### Post date: [December 18, 2020, 4:17pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/3 "2020-12-18T16:17:51Z")

</div>

When i move slider led on arduino go to on  
I have 3 led when slider reached Char led set it on

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 18, 2020, 4:23pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/4 "2020-12-18T16:23:08Z")

</div>

It would help us hep you if you were to:

- Describe your equipment
- Describe the setup (how are things wired, powered)
- Post your code that is not doing what you expect

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 18, 2020, 10:57pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/6 "2020-12-18T22:57:33Z")

</div>

What’s that? `connectedThread`

How did you pair the android device with the HC-06?  
How did you connect the HC-06 with your arduino (some only support 3.3v on Rx)?

The way you read Serial is weird (you have two serial reads in the loop)

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [December 19, 2020, 12:43pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/7 "2020-12-19T12:43:50Z")

</div>

Hi @r0x15, I think you are trying to fix too many things at the same time. First develop and test your Ard program connected directly to the PC. The 2nd serial read, as @jay_m says, looks wrong, suggest remove. You are using Serial.print to see what’s happening, but in the final arrangement they will arrive in the Processing sketch. Is that what you want? As you are using a Mega you could use one of the other Serial ports for your progress messages. You need an adapter, and putty. If you’ve never done that, ask.

When the Ard program is reliable, then connect the Processing program. When that is working well, change the connection to via Bluetooth.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 19, 2020, 1:55pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/9 "2020-12-19T13:55:03Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

You formatted it so I can work with it now… `:)`

This was a very quick exploration using Processing on PC and Arduino MEGA 2560 R3 connected with a USB cable.

> **Arduino**
>
> ```auto
> int incomingData;
> char data = 0;
> const byte LED9 = 9;
> const byte LED11 = 11; //BACK
> const byte LED12 = 12; //STOP
> const byte LED13 = 13; //FWD
> void setup()
> {
> 
> pinMode(LED9, OUTPUT); //set pin4 as output 
> pinMode(11, OUTPUT);
> pinMode(12, OUTPUT);
> pinMode(13, OUTPUT);
> Serial.begin(9600);
> 
> }
> 
> void loop()
> {
> //if(Serial.available())
> if (Serial.available() > 0) 
> {
> analogWrite(9, incomingData);
>     
> data = Serial.read();
> //Serial.print("\n");
> //Serial.print(data); //Print Value of data in Serial monitor 
> //Serial.print(" : ");  
> incomingData = Serial.read();
> Serial.println(data);
> 
> if (data == '!')
> //stop_();
> digitalWrite(LED13, HIGH);
>     
> else if (data == '~')
> digitalWrite(LED13, LOW);
>   
> //forward_();
> 
> // else if (data == 'b')
> // backward_();
> }
> }
> 
> void stop_() 
> {
> Serial.println("STOP");
> digitalWrite(LED12, HIGH);
> digitalWrite(LED11, LOW);
> digitalWrite(LED13, LOW);
> }
> 
> void forward_() 
> {
> Serial.println("FORWARD");
> digitalWrite(LED13, HIGH);
> digitalWrite(LED12, LOW);  
> digitalWrite(LED11, LOW);
> }
> 
> void backward_() 
> {
> Serial.println("BACKWARD");
> digitalWrite(LED11, HIGH);
> digitalWrite(LED12, LOW);
> digitalWrite(LED13, LOW);
> }
> 
> ```

> **Processing**
>
> ```auto
> import processing.serial.*;
> 
> Serial myPort; // Create object from Serial class
> int val; // Data received from the serial port
> 
> int count;
> 
> void setup() 
> {
> size(200, 200);
> // List all the available serial ports
> printArray(Serial.list());
>   
> String portName = Serial.list()[2];
> myPort = new Serial(this, portName, 9600);
> delay(1000);
> }
> 
> void draw() 
> {
> count++;
> byte data = byte(count);
> myPort.write(data); // send an H to indicate mouse is over square
> println(char(data));
>   
> if(data == '!')
> background(255, 255, 0);
> else if (data == '~')
> background(0);
> }
> 
> ```

This works!

I did not have a bank of LEDs so just using LED13 on Arduino and turned it ON and OFF and also changed Processing background.  
I used an integer counter converted to a byte to send data from Processing and looked for the equivalent ASCII value (character) received on Arduino.

The _ **important thing** _ is I added a delay() in setup():  
` delay(1000)`

The Arduino will reboot when you make an initial serial connection and you should wait until it is ready to receive serial data first otherwise you will flood the buffers.

I have a topic about this in the forum.  
You can look for it.

`:)`

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 19, 2020, 2:44pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/10 "2020-12-19T14:44:04Z")

</div>

> [@r0x15](#):
>
> it is possible that no one has ever used a slider code and an LED to drive Arduino via bluetooth it seems very strange to me

This is one of the first projects I did with Processing:

[![](https://img.youtube.com/vi/fj_UebW246k/maxresdefault.jpg "Exploring 3D Space") ](https://www.youtube.com/watch?v=fj_UebW246k)

I later adapted the same code to Processing on Android with the Ketai library and used the sliders as a general controller for LEDs, my robots etc.

Once you grasp serial communications and serial over Bluetooth this gets easier to work with.

Start with simple examples and build on that.

I used the Processing slider examples and not a library for this.

`:)`

---

<div class="post-metadata">

### Author: ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)
#### Post date: [December 19, 2020, 3:58pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/11 "2020-12-19T15:58:38Z")

</div>

maybe you didn’t understand I am making a dimmer on one led and the possibility through buttons to turn on three other leds the problem is that when I move the slider to dim the led the other 3 also turn on randomly according to the code that arrives

[image]

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 19, 2020, 6:37pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/12 "2020-12-19T18:37:01Z")

</div>

We understood. We are saying that your arduino code is not good. You will likely have something failing when you try to second guess when the data will arrive since you have two different reads in your arduino code.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 19, 2020, 9:21pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/13 "2020-12-19T21:21:12Z")

</div>

Hello,

Consider sending comma delimited data terminated with a ‘\n’:

```auto
import processing.serial.*;

Serial myPort; // Create object from Serial class

int data;

void setup() 
  {
  size(200, 200);
  
  // List all the available serial ports
  printArray(Serial.list());
  
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  delay(1000);
  }

void draw() 
  {
  if(frameCount%10 == 0) //Sends every 1/6 second
    {
    String sData = nf(byte(data), 3); // Only the LSB (least significant byte)
    //myPort.write(sData); //This will send 0 to 255 and repeat; it is only sending the LSB (least significatn byte of the int)
    //myPort.write(',');
    
    String R = nf(int(random(256)), 3);
    //myPort.write(R); //R
    //myPort.write(',');
    
    String G = nf(int(random(256)), 3);
    //myPort.write(G); //R
    //myPort.write(','); 
    
    String B = nf(int(random(256)), 3);
    //myPort.write(B); //R
    //myPort.write('\n'); 
    
    //This replaces commented data above:
    myPort.write(sData + ',' + R + ',' + G + ',' + B + '\n'); 
    
    data++;
    }
  }

```

This is an example of the data strings it was sending:  
000,183,109,014  
001,170,200,221  
002,243,029,109  
003,200,061,115  
004,180,070,156  
005,080,132,113  
…

The above is one way to do it; I found it easy to get substrings if data had leading 0’s and went with that.

On the Arduino side you will need to:

[https://www.arduino.cc/reference/en/language/functions/communication/serial/readstringuntil/](https://www.arduino.cc/reference/en/language/functions/communication/serial/readstringuntil/)

[https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/trim/](https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/trim/)

[https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/](https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/)

[https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/](https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/)

`:)`

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 19, 2020, 9:39pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/14 "2020-12-19T21:39:13Z")

</div>

> [@RichardDL](#):
>
> As you are using a Mega you could use one of the other Serial ports for your progress messages. You need an adapter, and putty. If you’ve never done that, ask.

That is my approach as well to monitor data.

I once used the network library as well for this.

You can also send the data (received from Arduino) back to the same sketch on same COM port and view it in the console; I did this in my exploration of this topic since my other hardware was not available and removed that in my example to keep it simple.

`:)`

---

<div class="post-metadata">

### Author: ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)
#### Post date: [December 19, 2020, 10:55pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/15 "2020-12-19T22:55:05Z")

</div>

Thank you so much this is a great solution but there is a problem  
what you posted is in java mode and i am in android mode  
how can I do?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 20, 2020, 12:23am UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/16 "2020-12-20T00:23:43Z")

</div>

> [@r0x15](#):
>
> Thank you so much this is a great solution but there is a problem  
> what you posted is in java mode and i am in android mode  
> how can I do?

This is not a problem… it is an opportunity for you.

> [@glv](#):
>
> Consider sending comma delimited data terminated with a ‘\n’

You will have to do this in Android mode.

`:)`

---

<div class="post-metadata">

### Author: ![J\_Silva](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@J\_Silva](https://discourse.processing.org/u/J_Silva)
#### Post date: [December 22, 2020, 8:49pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/17 "2020-12-22T20:49:55Z")

</div>

> [@r0x15](#):
>
> But on processig for Android it is possible that no one has ever used a slider code and an LED to drive Arduino via bluetooth it seems very strange to me

> [@glv](#):
>
> You will have to do this in Android mode.

Hi, @r0x15 Did you succeed in making an Android app with a slider to dim LEDs on an Arduino?

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 23, 2020, 11:00am UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/19 "2020-12-23T11:00:21Z")

</div>

@noel great job

couple typos:

> The pins were chosen for an Android mini pro

I supposed you meant an [Arduino Pro Mini](https://store.arduino.cc/arduino-pro-mini)

in the fritzing drawing:

- it’s probably the **Common Cathode** that is connected to GND.

- just for clarity, the 3 resistors are red, red, brown so 220Ω ![220](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/082c44f1c99cddc870514c4d68d96de39bec23d4.gif)

(on my screen they could be confused with red/red/violet ![R](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9f05656e47f43facd37b3f7aca44c844518722aa.gif) which would be 220MΩ (20%) so way too much)…

---

<div class="post-metadata">

### Author: ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)
#### Post date: [December 23, 2020, 11:29am UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/20 "2020-12-23T11:29:54Z")

</div>

Noel  
you are always available thank you very much I apologized if not I answered your questions but the commitments are many Hello and thanks

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 23, 2020, 1:04pm UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/23 "2020-12-23T13:04:46Z")

</div>

> [@glv](#):
>
> We must be careful when providing generic advice on circuit hookups.
> 
> The Rx pin on the HC05 is 3.3V and may not be 5V tolerant.

This was clearly stated

> This is a 3.3 V Arduino, thus no voltage divider was used on the Rx input. (Necessary with the 5V model.)

———————

> Each LED in an RGB LED has a different forward voltage drop; I would use correct resistor values for each one depending on the RGB LED datasheet to balance the intensity and limit current as required.

What’s the real point of « balancing the intensity »?  
(I get the concept but it has no practical use since human eye does not perceive RGB intensities in the same way. That’s what should be taken into account more than forward current and protection alone if you want to match the RGB choice)

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 28, 2020, 9:16am UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/25 "2020-12-28T09:16:44Z")

</div>

Hum seems some posts disappeared

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [December 28, 2020, 10:57am UTC](https://discourse.processing.org/t/code-android-e-arduino/26364/26 "2020-12-28T10:57:21Z")

</div>

@J_Silva@jay_m The post was flagged by the community. It seems that the rule is, not to paste full codes at least on topics tagged as homework. I respect that, but in the case of Android-related topics, I disagree because there is no Processing for Android documentation teaching to code programmatically. Something necessary, because it would be difficult to implement an XML file structure. The problem is, that for building apps, instead of using P4A in conjunction with the Arduino IDE (which were born to live together); P4A has totally lost the battle against software like “MIT App Inventor” because documentation and handling seem much more simple. And that’s really a pity. Further, I don’t believe that a teacher will use A4P for actually teaching Android. So I believe that this rule in this case has a negative effect. This is why I decided to make a Github repo with some ready code, and you can find the code in question there.

[Next page](https://discourse.processing.org/t/code-android-e-arduino/26364.md?page=2)
