My Bluetooth Code

Good day,

I am not good at programming, i just copying the samples and modified, i have here is the code to control arduino with android using bluetooth. My problem is that i cant capture the arduino analog pin back to my android. I would like the user knows of the analog pin is high or low by changing the image in my app.

my plan is to control the lights of my house using bluetooth, and the 6 lightbulb returns 5 volts voltage to its corresponding analog pins. But it seems i cant capture the input pin. im using MIT app inventor 2

Here is my code:

char buffer[67];

byte Pin02 = 2;
byte Pin03 = 3;
byte Pin04 = 4;
byte Pin05 = 5;
byte Pin06 = 6;
byte Pin07 = 7;
byte Pin08 = 8;
byte Pin09 = 9;
byte Pin10 = 10;
int Pin11 = 11;
int Pin12 = 12;
byte Pin13 = 13;

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

int sensorprint0 = 0;
int sensorprint1 = 0;
int sensorprint2 = 0;
int sensorprint3 = 0;
int sensorprint4 = 0;
int sensorprint5 = 0;

int sensorValue0 = 0;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int counter=0;

void setup(){
Serial.begin(9600);
Serial.flush( );
pinMode(Pin02, OUTPUT);
pinMode(Pin03, OUTPUT);
pinMode(Pin04, OUTPUT);
pinMode(Pin05, OUTPUT);
pinMode(Pin06, OUTPUT);
pinMode(Pin07, OUTPUT);
pinMode(Pin08, OUTPUT);
pinMode(Pin09, OUTPUT);
pinMode(Pin10, OUTPUT);
pinMode(Pin11, OUTPUT);
pinMode(Pin12, OUTPUT);
pinMode(Pin13, OUTPUT);

}
void loop(){

sensorValue0 = analogRead(analogInPin0);

sensorValue1 = analogRead(analogInPin1);

sensorValue2 = analogRead(analogInPin2);

sensorValue3 = analogRead(analogInPin3);

sensorValue4 = analogRead(analogInPin4);

sensorValue5 = analogRead(analogInPin5);

if (sensorValue1 > 900){
Serial.println("PortA1");
}else{
Serial.println("PortA1off");
delay(1000);
}
if (sensorValue2 > 900){
Serial.println("PortA2");
}else{
Serial.println("PortA2off");
delay(1000);
}
if (sensorValue3 > 900){
Serial.println("PortA3");
}else{
Serial.println("PortA3off");
delay(1000);
}
if (sensorValue4 > 900){
Serial.println("PortA4");
}else{
Serial.println("PortA4off");
delay(1000);
}
if (sensorValue5 > 900){
Serial.println("PortA5");
}else{
Serial.println("PortA5off");
delay(1000);
}

if (Serial.available()>0){
int index=0;
delay(100);//delay buffer encher
int numChar = Serial.available();
if(numChar>65){
numChar=65;
}
while(numChar--){
buffer[index++] = Serial.read();
}
splitString(buffer);
}
}

void splitString(char* data){
Serial.print("Data entered: ");
Serial.println(data);
char* parameter;
parameter= strtok (data, " ,");
while(parameter != NULL){
setLED(parameter);
parameter = strtok (NULL, " ,");
}
//Limpa o texto e os buffers seriais
for (int x=0; x<66; x++){
buffer[x]='\0';
}
Serial.flush();
}
void setLED(char* data){

if ((data[0] =='a') || (data[0] == 'A')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,1);
digitalWrite(Pin02, Ans);
Serial.print("Pin02 is: ");
Serial.println(Ans);
}
if ((data[0] =='b') || (data[0] == 'B')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(Pin03, Ans);
Serial.print("Pin03 is: ");
Serial.println(Ans);
}
if ((data[0] =='c') || (data[0] == 'C')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,1);
digitalWrite(Pin04, Ans);
Serial.print("Pin04 is: ");
Serial.println(Ans);
}
if ((data[0] =='d') || (data[0] == 'D')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(Pin05, Ans);
Serial.print("Pin05 is: ");
Serial.println(Ans);
}
if ((data[0] =='e') || (data[0] == 'E')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(Pin06, Ans);
Serial.print("Pin06 is: ");
Serial.println(Ans);
}
if ((data[0] =='f') || (data[0] == 'F')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,1);
digitalWrite(Pin07, Ans);
Serial.print("Pin07 is: ");
Serial.println(Ans);
}
if ((data[0] =='g') || (data[0] == 'G')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,1);
digitalWrite(Pin08, Ans);
Serial.print("Pin08 is: ");
Serial.println(Ans);
}
if ((data[0] =='h') || (data[0] == 'H')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(Pin09, Ans);
Serial.print("Pin09 is: ");
Serial.println(Ans);
}
if ((data[0] =='i') || (data[0] == 'I')){
int Ans = strtol(data+1, NULL, 10);
Ans = constrain(Ans,0,255);
analogWrite(Pin10, Ans);
Serial.print("Pin10 is: ");
Serial.println(Ans);
}

}

I would appreciate if someone can help me furnish the command with comments so i can compare and study.

if (Serial.available()>0){
    int index=0;
    delay(100);//delay buffer encher
    int numChar = Serial.available();
    if(numChar>65){
      numChar=65;
    }
    while(numChar--){
      buffer[index++] = Serial.read();
    }

Given that the serial buffer is 64 bytes, what are the odds that it will ever hold 65 or more characters?

I would like the user knows of the analog pin is high or low by changing the image in my app.

What does HIGH or LOW mean for an analog pin?

What is connected to the analog pins?

What happens to the serial data that is sent?

What happened to the code tags that you were supposed to use?

What does HIGH or LOW mean for an analog pin?

= if my analog pins goes up means the light that indicate the light is already on. if it is low it is off

What is connected to the analog pins?

=it is connected to external 5 volts supply sent by ac lightbulb as indication of its status.

What happens to the serial data that is sent?

= if the data character in my phone is like this.

Portxx
Portxx
Portxx
Portxx
Portxx
Portxx
Portxx

What happened to the code tags that you were supposed to use?
=actually i cant understand some, What I did is trial and error.

Please send me feedback if i can use this.
if (counter > 9000){
if (sensorValue0 > 900){
Serial.println("PortA1");
counter = 0;
}
counter = 0;
}
counter++;

Please correct your posts above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

if not you have code like this that gets displayed:
buffer

  • ='\0';

This is my code:
Attached is my visual of the app

char buffer[67];

byte Pin02 = 2;
byte Pin03 = 3;
byte Pin04 = 4;
byte Pin05 = 5;
byte Pin06 = 6;
byte Pin07 = 7;
byte Pin08 = 8;
byte Pin09 = 9;
byte Pin10 = 10;
int Pin11 = 11;
int Pin12 = 12;
byte Pin13 = 13;

const int analogInPin0 = A0;  
const int analogInPin1 = A1;  
const int analogInPin2 = A2;  
const int analogInPin3 = A3;  
const int analogInPin4 = A4;  
const int analogInPin5 = A5;
  
int sensorprint0 = 0;
int sensorprint1 = 0;
int sensorprint2 = 0;
int sensorprint3 = 0;
int sensorprint4 = 0;
int sensorprint5 = 0;

int sensorValue0 = 0;        
int sensorValue1 = 0;        
int sensorValue2 = 0;        
int sensorValue3 = 0;        
int sensorValue4 = 0;        
int sensorValue5 = 0;
int counter=0;

void setup(){
  Serial.begin(9600);
  Serial.flush( );
  pinMode(Pin02, OUTPUT);
  pinMode(Pin03, OUTPUT); 
  pinMode(Pin04, OUTPUT); 
  pinMode(Pin05, OUTPUT); 
  pinMode(Pin06, OUTPUT); 
  pinMode(Pin07, OUTPUT); 
  pinMode(Pin08, OUTPUT); 
  pinMode(Pin09, OUTPUT); 
  pinMode(Pin10, OUTPUT); 
  pinMode(Pin11, OUTPUT); 
  pinMode(Pin12, OUTPUT); 
  pinMode(Pin13, OUTPUT); 
  
 }
void loop(){
 
 sensorValue0 = analogRead(analogInPin0);  
 
 sensorValue1 = analogRead(analogInPin1);
 
 sensorValue2 = analogRead(analogInPin2);
         
 sensorValue3 = analogRead(analogInPin3);
 
 sensorValue4 = analogRead(analogInPin4); 
      
 sensorValue5 = analogRead(analogInPin5);
 
// i would like to capture and store the data here and put to android variable so i can use it.
  if  (sensorValue1 > 900){
       Serial.println("PortA1");
}else{
    Serial.println("PortA1off");    
    delay(1000);
   }
   if  (sensorValue2 > 900){
       Serial.println("PortA2");
    }else{
    Serial.println("PortA2off");
    delay(1000);
   }
   if  (sensorValue3 > 900){
       Serial.println("PortA3");
    }else{
    Serial.println("PortA3off");
    delay(1000);
   }
   if  (sensorValue4 > 900){
       Serial.println("PortA4");
    }else{
    Serial.println("PortA4off");
    delay(1000);
   }
   if  (sensorValue5 > 900){
       Serial.println("PortA5");
    }else{
    Serial.println("PortA5off");
    delay(1000);
   }


//this area is copied and it works----------

   if (Serial.available()>0){
    int index=0;
    delay(100);//delay buffer encher
    int numChar = Serial.available();
    if(numChar>65){
      numChar=65;
    }
    while(numChar--){
      buffer[index++] = Serial.read();
    }
    splitString(buffer);
  }
}

void splitString(char* data){
  Serial.print("Data entered: ");
  Serial.println(data);
  char* parameter;
  parameter= strtok (data, " ,");
  while(parameter != NULL){
    setLED(parameter);
    parameter = strtok (NULL, " ,");
  }
  //Limpa o texto e os buffers seriais
  for (int x=0; x<66; x++){
    buffer[x]='\0';
  }
  Serial.flush();
}
void setLED(char* data){
  
  if ((data[0] =='a') || (data[0] == 'A')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,1);
    digitalWrite(Pin02, Ans);
    Serial.print("Pin02 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='b') || (data[0] == 'B')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,255);
    analogWrite(Pin03, Ans);
    Serial.print("Pin03 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='c') || (data[0] == 'C')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,1);
    digitalWrite(Pin04, Ans);
    Serial.print("Pin04 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='d') || (data[0] == 'D')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,255);
    analogWrite(Pin05, Ans);
    Serial.print("Pin05 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='e') || (data[0] == 'E')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,255);
    analogWrite(Pin06, Ans);
    Serial.print("Pin06 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='f') || (data[0] == 'F')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,1);
    digitalWrite(Pin07, Ans);
    Serial.print("Pin07 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='g') || (data[0] == 'G')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,1);
    digitalWrite(Pin08, Ans);
    Serial.print("Pin08 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='h') || (data[0] == 'H')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,255);
    analogWrite(Pin09, Ans);
    Serial.print("Pin09 is: ");
    Serial.println(Ans);
  }
  if ((data[0] =='i') || (data[0] == 'I')){
    int Ans = strtol(data+1, NULL, 10);
    Ans = constrain(Ans,0,255);
    analogWrite(Pin10, Ans);
    Serial.print("Pin10 is: ");
    Serial.println(Ans);
  }

}

.

what's the deal with

byte Pin02 = 2;
byte Pin03 = 3;
byte Pin04 = 4;
byte Pin05 = 5;
byte Pin06 = 6;
byte Pin07 = 7;
byte Pin08 = 8;
byte Pin09 = 9;
byte Pin10 = 10;
int Pin11 = 11;
int Pin12 = 12;
byte Pin13 = 13;

or

  pinMode(Pin02, OUTPUT);
  pinMode(Pin03, OUTPUT); 
  pinMode(Pin04, OUTPUT); 
  pinMode(Pin05, OUTPUT); 
  pinMode(Pin06, OUTPUT); 
  pinMode(Pin07, OUTPUT); 
  pinMode(Pin08, OUTPUT); 
  pinMode(Pin09, OUTPUT); 
  pinMode(Pin10, OUTPUT); 
  pinMode(Pin11, OUTPUT); 
  pinMode(Pin12, OUTPUT); 
  pinMode(Pin13, OUTPUT);

where are you using any of those pins?

you should throw away all that code and start by doing a project with 1 pin , 1 display and get that to work. there are plenty of code for this on the internet. once you get how that works, then go to the next stage and add more lights

That output pins go to relay to the ac bulbs and the bulbs have an 5volts out goes back to A0-A5 and the bluetooth send back the data from the analog pin to the android app. so the image can change its state from on or off for ac bulbs status.

ManangTang:
That output pins go to relay to the ac bulbs and the bulbs have an 5volts out goes back to A0-A5 and the bluetooth send back the data from the analog pin to the android app. so the image can change its state from on or off for ac bulbs status.

please describe the wiring and components (and fix the code in the first post using the tags)