Hi All,
I am working on setup in which I have sensor's array which is acting a like calculator input i.e. each sensor corresponds to an integer number or arithmetic operation. I am using HC-05 module to send the data to Android app (Calculator Prototype). Following command is being used to send the data to the app.
BTserial.print("value");
Here "value" is 0-9 and symbols of arithmetic operations.
The problem which I am facing is that App perfectly works from keys 0-7 but when I try to code 8 and so on, the app shows strange values like blocks etc.
Can someone tell me that whether the problem is in Arduino code (for sending the data).
Note:
The code is bit messy because it is still in testing phase. Moreover, the code added which is causing error.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(11,10);
LiquidCrystal_I2C lcd(0x3F,20,4);
// The following function is used to make a number i.e. 123
int numbermaker(int sizer ,int num[]){
int number=0;
int exponent=sizer;
for (int i=0;i<=sizer-1;i++)
{
exponent--;
if (exponent == 0)
number = number + (num[i] * pow(10 , 0));
else
number = number + (num[i] * pow(10 , exponent));
}
if (sizer >= 3)
return number + 1;
else
return number;
}
// LCD Display Function
int dis (int col, int row){
lcd.setCursor(col,row);
}
// Arithmetic Functions//
int add (int a, int b){
int res;
return res=a+b;
}
int sub (int a, int b){
int res;
return res=a-b;
}
int mult (int a, int b){
int res;
return res=a*b;
}
//Integer based divsion
int division (int a, int b){
float res;
return res=a/b;
}
// Pins Assignment
const int OUT_PIN1 = A1;
const int OUT_PIN2 = A2;
const int OUT_PIN3 = A3;
const int OUT_PIN4 = A4;
const int OUT_PIN5 = A5;
const int OUT_PIN6 = A6;
const int OUT_PIN7 = A7;
const int OUT_PIN8 = A8;
const int OUT_PIN9 = A9;
const int OUT_PIN10 = A10;
const int OUT_PIN11 = A11;
const int OUT_PIN12 = A12;
const int OUT_PIN13 = A13;
const int OUT_PIN14 = A14;
const int OUT_PIN15 = A15;
// Variable Declaration
int row=0; int col=0;
int minus=0;
int divis=0;
int multy=0;
int final_out=0;
int sen1,sen2,sen3,sen4,sen5,sen6,sen7,sen8,sen9,sen10;
int opr1=0;
int opr2=0;
int plus=0;int equal=0;
int arr_cnt = 0 ;
int cmdSeries1 = 0;
int cmdSeries2 = 0;
const int IN_PIN = A0;
// Follwoing part relates to Capacitance measurement
const float IN_STRAY_CAP_TO_GND = 24.48; const float IN_CAP_TO_GND = IN_STRAY_CAP_TO_GND;
const float R_PULLUP = 34.8; const int MAX_ADC_VALUE = 1023;
// Array to store operands
int op1[6] = {};
int op2[6]= {};
//
// Arithmetic operations///
//
int do_operation (int digit){
if(plus==1){
op2[cmdSeries2] = digit;
int x = op2[cmdSeries2];
cmdSeries2 = cmdSeries2+1;
}
else if(minus==1){
op2[cmdSeries2] = digit;
int x = op2[cmdSeries2];
cmdSeries2 = cmdSeries2+1;
}
else if(divis==1){
op2[cmdSeries2] = digit;
int x = op2[cmdSeries2];
cmdSeries2 = cmdSeries2+1;
}
else if(multy==1){
op2[cmdSeries2] = digit;
int x = op2[cmdSeries2];
cmdSeries2 = cmdSeries2+1;
}
else{
op1[cmdSeries1] = digit;
int x = op1[cmdSeries1];
cmdSeries1 = cmdSeries1+1;
}
}
// Main loop starts
void setup()
{
lcd.init();
lcd.backlight();
pinMode(OUT_PIN1, OUTPUT);
pinMode(OUT_PIN2, OUTPUT);
pinMode(OUT_PIN3, OUTPUT);
pinMode(OUT_PIN4, OUTPUT);
pinMode(OUT_PIN5, OUTPUT);
pinMode(OUT_PIN6, OUTPUT);
pinMode(OUT_PIN7, OUTPUT);
pinMode(OUT_PIN8, OUTPUT);
pinMode(OUT_PIN9, OUTPUT);
pinMode(OUT_PIN10, OUTPUT);
pinMode(OUT_PIN11, OUTPUT);
pinMode(OUT_PIN12, OUTPUT);
pinMode(OUT_PIN13, OUTPUT);
pinMode(OUT_PIN14, OUTPUT);
pinMode(OUT_PIN15, OUTPUT);
pinMode(IN_PIN, OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(14,OUTPUT);
pinMode(15,OUTPUT);
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
pinMode(IN_PIN, INPUT);
///////////////////////////////////////////////////////Key 0//////////////////////////
digitalWrite(OUT_PIN1, HIGH);
int val1 = analogRead(IN_PIN);
digitalWrite(OUT_PIN1, LOW);
///////////////////////////////////////////////////////Key 1//////////////////////////
digitalWrite(OUT_PIN2, HIGH);
int val2 = analogRead(IN_PIN);
digitalWrite(OUT_PIN2, LOW);
///////////////////////////////////////////////////////Key 2//////////////////////////
digitalWrite(OUT_PIN3, HIGH);
int val3 = analogRead(IN_PIN);
digitalWrite(OUT_PIN3, LOW);
///////////////////////////////////////////////////////Key 3//////////////////////////
digitalWrite(OUT_PIN4, HIGH);
int val4 = analogRead(IN_PIN);
digitalWrite(OUT_PIN4, LOW);
///////////////////////////////////////////////////////Key 4//////////////////////////
digitalWrite(OUT_PIN5, HIGH);
int val5 = analogRead(IN_PIN);
digitalWrite(OUT_PIN5, LOW);
///////////////////////////////////////////////////////Key 5//////////////////////////
digitalWrite(OUT_PIN6, HIGH);
int val6 = analogRead(IN_PIN);
digitalWrite(OUT_PIN6, LOW);
///////////////////////////////////////////////////////Key 6//////////////////////////
digitalWrite(OUT_PIN7, HIGH);
int val7 = analogRead(IN_PIN);
digitalWrite(OUT_PIN7, LOW);
///////////////////////////////////////////////////////Key 7//////////////////////////
digitalWrite(OUT_PIN8, HIGH);
int val8 = analogRead(IN_PIN);
digitalWrite(OUT_PIN8, LOW);
///////////////////////////////////////////////////////Key 8//////////////////////////
digitalWrite(OUT_PIN9, HIGH);
int val9 = analogRead(IN_PIN);
digitalWrite(OUT_PIN9, LOW);
///////////////////////////////////////////////////////Key 9//////////////////////////
digitalWrite(OUT_PIN10, HIGH);
int val10 = analogRead(IN_PIN);
digitalWrite(OUT_PIN10, LOW);
///////////////////////////////////////////////////////Key +//////////////////////////
digitalWrite(OUT_PIN11, HIGH);
int val11 = analogRead(IN_PIN);
digitalWrite(OUT_PIN11, LOW);
///////////////////////////////////////////////////////Key - //////////////////////////
digitalWrite(OUT_PIN12, HIGH);
int val12 = analogRead(IN_PIN);
digitalWrite(OUT_PIN12, LOW);
///////////////////////////////////////////////////////Key x //////////////////////////
digitalWrite(OUT_PIN13, HIGH);
int val13 = analogRead(IN_PIN);
digitalWrite(OUT_PIN13, LOW);
///////////////////////////////////////////////////////Key Div (/)//////////////////////////
digitalWrite(OUT_PIN14, HIGH);
int val14 = analogRead(IN_PIN);
digitalWrite(OUT_PIN14, LOW);
///////////////////////////////////////////////////////Key = //////////////////////////
digitalWrite(OUT_PIN15, HIGH);
int val15 = analogRead(IN_PIN);
digitalWrite(OUT_PIN15, LOW);
/////////////////////////////////////////////////////////////////////////////////////
if ((val1 < 1000)|(val2 < 1000)|(val3 < 1000)|(val4 < 1000)|(val5 < 1000)|(val6 < 1000)|(val7 < 1000)|(val8 < 1000)|(val9 < 1000)|(val10 < 1000){
/////////////////////////////// Integer 0 / /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance1 = (float)val1 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val1);
if(capacitance1 > 10.0){
digitalWrite(2,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("0");
BTserial.print("0");
do_operation(sen1=0);
}
else{
digitalWrite(2,LOW);
}
/////////////////////////////// Integer 1 /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance2 = (float)val2 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val2);
if(capacitance2 > 10.0){
digitalWrite(3,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("1");
BTserial.print("1");
do_operation(sen2=1);
}
else{
digitalWrite(3,LOW);
}
/////////////////////////////// Integer 2 /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance3 = (float)val3 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val3);
if(capacitance3 > 10.0){
digitalWrite(4,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("2");
BTserial.print("2");
do_operation(sen3=2);
}else{
digitalWrite(4,LOW);
}
/////////////////////////////// Integer 3 /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance4 = (float)val4 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val4);
if(capacitance4 > 10.0){
digitalWrite(5,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("3");
BTserial.print("3");
do_operation(sen4=3);
}
else{
digitalWrite(5,LOW);
}
/////////////////////////////// Integer 4 /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance5 = (float)val5 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val5);
if(capacitance5 > 10.0){
digitalWrite(6,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("4");
BTserial.print("4");
do_operation(sen5=4);
}else{
digitalWrite(6,LOW);
}
/////////////////////////////// Integer 5 /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance6 = (float)val6 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val6);
if(capacitance6 > 10.0){
digitalWrite(7,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("5");
BTserial.print("5");
do_operation(sen6=5);
}else{
digitalWrite(7,LOW);
}
/////////////////////////////// Integer 6 /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance7 = (float)val7 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val7);
if(capacitance7 > 10.0){
digitalWrite(8,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("6");
BTserial.print("6");
do_operation(sen7=6);
}else{
digitalWrite(8,LOW);
}
/////////////////////////////// Integer 7 /////////////////////
pinMode(IN_PIN, OUTPUT);
float capacitance8 = (float)val8 * IN_CAP_TO_GND /
(float)(MAX_ADC_VALUE - val8);
if(capacitance8 > 10.0){
digitalWrite(9,HIGH);
dis (col,row);
if (col==19){row=row+1; col=0;} else {col=col+1;}
lcd.print("7");
BTserial.print("7");
do_operation(sen8=7);
}else{
digitalWrite(9,LOW);
}
///////////////////////////////// Integer 8 /////////////////////
// pinMode(IN_PIN, OUTPUT);
// float capacitance9 = (float)val9 * IN_CAP_TO_GND /
// (float)(MAX_ADC_VALUE - val9);
// if(capacitance9 > 10.0){
// // sen8=1;
// digitalWrite(10,HIGH);
// dis (col,row);
// if (col==19){row=row+1; col=0;} else {col=col+1;}
// lcd.print("8");
// BTserial.print("8");
// do_operation(sen8=8);
//
// }else{
// digitalWrite(10,LOW);
// }
//
}
if(equal==1){
opr1 = numbermaker(cmdSeries1 , op1);
opr2 = numbermaker(cmdSeries2 , op2);
Serial.println(opr1);
Serial.println(opr2);
if (plus==1)
final_out= add(opr1,opr2);
else if (minus==1)
final_out= sub(opr1,opr2);
else if (multy==1)
final_out= mult(opr1,opr2);
else if (divis==1)
final_out= division(opr1,opr2);
Serial.print("Final result is : ");
Serial.println(final_out);
BTserial.print(final_out);
lcd.print(final_out);
equal=0;
plus=0;
minus=0;
divis=0;
multy=0;
}
while (millis() % 1000 != 0);
}