Code android e arduino

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

:thinking:

care to document context and what is the real question?

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

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

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)

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.

1 Like

Hello,

Please 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
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
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.

:)

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

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.

:)

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]

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.

1 Like

Hello,

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

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/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/toint/

:)

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.

:)

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.

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?

@noel great job

couple typos:

The pins were chosen for an Android mini pro

I supposed you meant an 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

(on my screen they could be confused with red/red/violet R which would be 220MΩ (20%) so way too much)


1 Like

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

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)

Hum seems some posts disappeared

@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.