I have programmed a client-server between an Arduino and my mobile app in Android. I´m using a buetooth HC-06. For the moment is a very basic code just to turn ON/OFF a led, regulate its voltage, and receive the charge of the battery.
This is my Arduino code for receiving the message:
#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial hc06(2,3);
char command;
String string;
boolean ledon = false;
const byte MAX_STRING_LEN = 40;
const int BLINK_INTERVAL = 1000;
char inputString[MAX_STRING_LEN]; // a string to hold incoming data
int BatteryPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
float BatteryCar = 0;
int PercentageBatt = 0;
int intensidad = 0;
byte strLen = 0; // current length of rec'd string
#define led 11
void setup()
{
Serial.begin(9600);
hc06.begin(9600);
pinMode(led, OUTPUT);
Timer1.initialize(10000); // set a timer of length 10000 microseconds (or 0.01 sec - or 1Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
Timer1.attachInterrupt( SendVoltage );
}
void loop()
{
sensorValue = analogRead(BatteryPin);
BatteryCar = sensorValue * (5.0 / 1023.0);
if (hc06.available() == 0)
{
analogWrite(led, 0);
delay (10);
}
while(hc06.available() > 0)
{
command = ((byte)hc06.read());
if((command == '\n') || (command == '\r'))
{
command =='\0';
break;
}
else if (command == 'O')
analogWrite(led, 255);
else
analogWrite(led, 0);
delay(10);
Serial.println(command);
}
}
void SendVoltage ()
{
PercentageBatt = (BatteryCar * 100)/4;
hc06.println(PercentageBatt);
}
This is the data that I´m receiving:
It supposed that i should only receive "O" or "F". Any suggestion??
thanks in advanced,
