String - Int confusion

Hi there peeps,

I just developer an Android app using MIT App inventor 2. It connects my Arduino Due via HC 06 bluetooth module. There's a slider on the app and its values are from 0 to 255. Program feeds the slider value to Arduino and then I want Arduino to change RGB led's colors accordingly.

So I find this example code which is used to read "on" and "off" commands that has been sent via bluetooth to turn a simple led on or off. So I did try to change it to suit my needs but I get errors. I can't use numbers with strings.

What do I need to change in this code to make it work?

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
}

void loop() {
  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }
  if (readString.length() >0) {
    Serial.println(readString);
    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }
    readString="";
  } 
}

First of all, ditch the String class. It has reliability problems on the Arduino.

Second, get the bluetooth device off the hardware serial pins. Read from the bluetooth device using SoftwareSerial on two other pins. Use Serial (using the hardware serial pins) to debug your sketch.

readString doesn't need global scope and should be defined at the start of the loop function. Also, for this code where ledPin is constant, ledPin should be declared as such (const int rather than just int). These are just some good C++ programming practices.

May I suggest you look at the StaticString library. It's an easy string class that doesn't fragment the heap like String.

There's some StaticString examples here.

There's a slider on the app and its values are from 0 to 255. Program feeds the slider value to Arduino and then I want Arduino to change RGB led's colors accordingly.

A simple example of code for servos that demonstrates capturing a string of characters into a String variable, then converting that into an integer for servo position control.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer
    myservo.write(n);
    readString="";
  } 
}

Guys, turns out my problem is more severe than I thought. My phone can't communicate with Arduino at all. The commands I send is not received. When I type on off on the serial monitor, I can get things work. So there aren't any issues on the code part. But it seems the commands I send are not receiver properly.

I follow this tutorial. App Inventor 2 tutorial - Android Control Arduino with HC-06 Bluetooth module - YouTube

cagancelik:
So there aren't any issues on the code part.

Okay, just don't ever say you weren't warned.