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.