Calling a Function through Bluetooth Mobile

I have make an apps to call a function from arduino to run, which the function is selected through the apps and when the start button is press all the function will be running. The problem is it can only read 7 value from the bluetooth and after the 7 value, it does shows an mix number of the example is 255.

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>

#define rxPin 8
#define txPin 7

Adafruit_NeoPixel strip = Adafruit_NeoPixel (24,2,NEO_RGB + NEO_KHZ800);


SoftwareSerial mySerial(rxPin, txPin); // RX, TX
byte numChar=0;
byte myChar[40];
byte i=0;

// function prototypes
void ani1();
void ani2();
void ani3();
void ani4();
void ani5();
void ani6();
void ani7();
void ani8();
void ani9();
void ani10();
void ani11();
void ani12();
void ani13();
void ani14();
void ani15();
void ani16();
void ani17();
void ani18();
void ani19();
void ani20();
void ani21();
void ani22();
void ani23();
void ani24();
void ani25();
void ani26();
void ani27();
void ani28();
void ani29();
void ani30();
void ani31();
void ani32();
void ani33();
void ani34();
void ani35();
void ani36();
void ani37();
void ani38();
void ani39();
void ani40();
void ani41();

// array of function pointers
void (*animations[])() =
{
  
  ani1,
  ani2,
  ani3,
  ani4,
  ani5,
  ani6,
  ani7,
  ani8,
  ani9,
  ani10,
  ani11,
  ani12,
  ani13,
  ani14,
  ani15,
  ani16,
  ani17,
  ani18,
  ani19,
  ani20,
  ani21,
  ani22,
  ani23,
  ani24,
  ani25,
  ani26,
  ani27,
  ani28,
  ani29,
  ani30,
  ani31,
  ani32,
  ani33,
  ani34,
  ani35,
  ani36,
  ani37,
  ani38,
  ani39,
  ani40,
  ani41
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Test Wave");


  mySerial.begin(9600);
  mySerial.println("Hello, world?");
  strip.begin();
  strip.show();
}

void loop(){
  if(mySerial.available()){
    i=0;
    myChar[numChar] = mySerial.read();
    myChar[numChar] -= '1';
    Serial.print(myChar[numChar]);
    Serial.print(" ");
    Serial.print(numChar);
    numChar++;
  if(numChar>40){
    numChar=0;
  }
  }

  if(Serial.available()){
   myChar[numChar] = Serial.read();
   myChar[numChar] -= '1';
   Serial.print(myChar[numChar]);
   
  }
  
  Serial.print(i);
  Serial.print(" ");
  i++;
  if(i>40){
    i=0;
  }
  
    animations[myChar[i]]();
  Serial.println();
}

void ani1(){
}

void ani2(){
  for(int c=0;c<24;c++){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
 Serial.print("animtion2");
}

void ani3(){
  for(int c=0;c<24;c++){
    strip.setPixelColor(23-c,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
 Serial.print("animtion3");
}
void ani4(){
  for(int c=0;c<12;c++){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animtion4");
}

void ani5(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation5");
}

void ani6(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation6");
}

void ani7(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation7");
}
void ani8(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation8");
}
void ani9(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation9");
}
void ani10(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation10");
}
void ani11(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation11");
}
void ani12(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation12");
}
void ani13(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation13");
}
void ani14(){
for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation14");
}
void ani15(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation15");
}
void ani16(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation16");
}
void ani17(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation17");
}
void ani18(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation18");
}
void ani19(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation19");
}
void ani20(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation20");
}
void ani21(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation21");
}
void ani22(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation22");
}
void ani23(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation23");
}
void ani24(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation24");
}
void ani25(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation25");
}
void ani26(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation26");
}
void ani27(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation27");
}
void ani28(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation28");
}
void ani29(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation29");
}
void ani30(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation30");
}
void ani31(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation31");
}
void ani32(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation32");
}
void ani33(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation33");
}
void ani34(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation34");
}
void ani35(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation35");
}
void ani36(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation36");
}
void ani37(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation37");
}
void ani38(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation38");
}
void ani39(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation39");
}
void ani40(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation40");
}
void ani41(){
  for(int c=12;c>=0;c--){
    strip.setPixelColor(c,strip.Color(255,0,0));
    strip.setPixelColor(c+12,strip.Color(255,0,0));
    strip.show();
    strip.clear();
    delay(100);
  }
  Serial.print("animation41");
}

Here is the value of serial.print

 00 
2 10 animtion3
3 20 animtion3
4 30 animtion3
5 40 animtion3
8 50 animtion3
6 60 animtion3
0 70 animtion3
255 80 animtion3
0 90 animtion3
0 100 animtion3
0 110 animtion3
1 120 animtion3
0 130 animtion3
2 140 animtion3
0 150 animtion3
3 160 animtion3

Start from the value of 255 it shows random number, is it because bluetooth can only send 8 data?

In this part of the loop

    myChar[numChar] = mySerial.read();
    myChar[numChar] -= '1';
    Serial.print(myChar[numChar]);
    Serial.print(" ");
    Serial.print(numChar);
    numChar++;
    if (numChar > 40) {
      numChar = 0;
    }

myChar is an array with 40 slots, index ranging from 0 to 39

You test if (numChar > 40) before reseting it to 0 so if numChar == 40 you'll keep going and try to write beyond the allocated memory in myChar[numChar] = mySerial.read();

why do you have two consecutive if (Serial.available())? (and not increasing numChar in the second one?

  if (Serial.available()) {
    myChar[numChar] = Serial.read();
    myChar[numChar] -= '1';
    Serial.print(myChar[numChar]);
  }

I would suggest to study Serial Input Basics to handle your input in the right way

well i look at the example of arduino forum which they use 2 serial available. If i use at the second one it will have a double value. Usually that was my plan to add the numChar when there is an input come from bluetooth

Do you understand the value from the serial monitor means?

you are listening both on Serial and mySerial

when data comes in from mySerial, you store the value received minus the ASCII code of '1' in the buffer and increment the position (and are at risk to overflow as I said)

when data comes in from Serial, you store the value received minus the ASCII code of '1' in the buffer and don't increment the position

you never check if the data received is a digit (do you risk receiving '\r' or '\n' for example? then subtracting '1' will lead to disappointment)

so the whole data handling seems fragile to me, hence the recommendation to learn more about Serial ports

i get it, thank you. But do you know why there is value coming 255 from the bluetooth like the serial port shown above does the bluetooth maximum output value in 1 times only 8 value?
look like 255 is an error or restart?

all data come through a number from 1 to 41 lemme show you the build

If you send '0' and do the subtraction in ASCII of '0'-'1', you get -1, which is coded 255 on a byte

in the serial port shown above the very left is the input value from the bluetooth and the next is numChar and the 0 is i which later will be modified it's to see the value right now , which i wonder start from after 6 the value should be 7 and 8 depend on the input on the apps but it shows 0 and 255

which i do questioned is that the maximum capabillity of bluetooth to send value

BT will send any byte you want - the whole 256 possible values. What you do with the value is up to your code

sorry for missinterpretation, what i mean capabillity to send only around 6 data in one click, because the rest of it look like an error?

or is it my code?

it's likely your code.

can you install this code in the arduino

#include <SoftwareSerial.h>
#define rxPin 8
#define txPin 7
SoftwareSerial mySerial(rxPin, txPin);

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

void loop() {
  if (mySerial.available()) {
    byte r = mySerial.read();
    Serial.print(F("got 0x"));
    if (r < 0x10) Serial.write('0');
    Serial.print(r, HEX);
    if (isalnum(r)) { // if it's an alphanumeric ASCII code
      Serial.print(F("\t-->\t"));
      Serial.write(r);
    }
    Serial.prinln();
  }
}

and open the Serial monitor at 115200 bauds and play with your app to see what's received on the other side

--> post here (as text) what's displayed in the Serial Monitor.

got 0x35	-->	5
got 0x36	-->	6
got 0x37	-->	7
got 0x38	-->	8
got 0x39	-->	9
got 0x31	-->	1
got 0x30	-->	0
got 0x31	-->	1
got 0x31	-->	1
got 0x31	->	1
got 0x32	-->	2
got 0x31	-->	1
got 0x33	-->	3

After 9 should be 10 and 11 from the application input

Yes, this is what you get:

got 0x31	-->	1
got 0x30	-->	0

or for 11

got 0x31	-->	1
got 0x31	-->	1

it's sent as 2 characters because you are sending text
image

, not numbers... so the '0' becomes -1 when you subtract '1'

if your list was containing just numbers (not text) and you would send the binary value and not its ASCII representation then you can have 256 choices (from 0 to 255)

ahh so it does read 1 and 0. is there a way to read it becomes 10?

No if you don't send it as a number.

if you were to send a "end marker", ie a way to tell the message is complete, like a new line character after the number then you could do something about it. but here when you get '1', follow by '2' there is no way to tell if the user pressed on button '1' and then on button '2' or if she pressed on '12'

easiest way is to modify the MIT App to send numbers, not text

okay ill try it now