Multiple values to Arduino

I'm trying to create an app to change the color and brightness of a neopixel strip, via Bluetooth. I used a canvas and a ball to create a color wheel, and a slider for brightness, every time I drag the ball I change the RGB values.
I write the values in some Labels and then send the text written in them as a string to Arduino.

The problem is that when dragging the ball on the canvas, or dragging the slider, this happens in Arduino:

I can put a trailing separator for the data and only read the first or last block of values, but I want to change the state of the neopixel strip in parallel (or a sort of) with the change of the canvas or slider.

For now, I'm just trying to see that the data is received correctly, I haven't added anything about the neopixels yet.

#include <SoftwareSerial.h>

String mainString;
SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  Serial.begin(9600);
  mySerial.begin(38400);
}

void loop() {
  if(mySerial.available()) {
    mainString = mySerial.readString();
    Serial.println(mainString);
    byte firstIndex = mainString.indexOf('+');
    byte secondIndex = mainString.indexOf('+',firstIndex + 1);
    byte thirdIndex = mainString.indexOf('+',secondIndex + 1);
    byte fourthdIndex = mainString.indexOf('+',thirdIndex + 1);

    String RED = mainString.substring(0,firstIndex);
    String GREEN = mainString.substring(firstIndex + 1, secondIndex);
    String BLUE = mainString.substring(secondIndex + 1, thirdIndex);
    String LIGHT = mainString.substring(thirdIndex + 1, fourthdIndex);
    String MODE = mainString.substring(fourthdIndex + 1);

    int r = RED.toInt();
    int g = GREEN.toInt();
    int b = BLUE.toInt();
    int l = LIGHT.toInt();
    int m = MODE.toInt();
  }
}

When you move the Slider you are continuously sending data to Bluetooth, it may not be able to process that data rate.
It uses the Slider extension to send the data when you stop touching the Slider.

1 Like

Send a \n as a terminator, and readStringUntil \n

1 Like

I made the change and my mindset of sending the data constantly, now the data is sent at Canvas touch-up, Slider touch-up, and a Spinner's selection.

The issue now is the data I receive is trash.

Canvas data (I receive trash):
image

Slider data (trash and the Slider's value without 'L'):
image

Spinner data (Selected Index without 'M'):
image

My entire blocks and BT properties:


image

Arduino code:

#include <SoftwareSerial.h>

SoftwareSerial bt(10, 11); // RX, TX

String response;

void setup() {
  Serial.begin(9600);
  bt.begin(38400);
}

void loop() {
  
  while(bt.available()) {
    char cr = bt.read();
    response += cr; 
  }
  
  if (response.length() > 0) {
    Serial.println(response);
    response = "";
  }
}

I'm using an HC-05.

For the recent issue, I found that I required a delay at the end of my loop, which works great, Thanks for your help @ABG and @Juan_Antonio.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.