#include SoftwareSerial.h
int val;
String inputString = "";
boolean stringComplete = false; // whether the string is complete
SoftwareSerial BTSerial(8, 9); // RX | TX
void setup()
{
Serial.begin(9600);
BTSerial.begin(4800);
inputString.reserve(200);
}
void loop()
{
// print the string when a newline arrives:
// clear the string:
if (stringComplete)
{
Serial.println(inputString.toInt());
inputString = "";
stringComplete = false;
}
serialEvent();
}
void serialEvent()
{
//Serial.println(1);
int i = 0;
while (BTSerial.available())
{
//Serial.println(2);
char inChar = (char)BTSerial.read();
i++;
if (i < 2)
{
//Serial.println(3);
inputString += inChar;
}
if (inChar == '\n')
{
stringComplete = true;
}
}
}
#include SoftwareSerial.h
int Xmax = 0, Xmin = 255, Ymax = 0, Ymin = 255;
int posX, posY;
int MposX = 0, MposY = 0;
SoftwareSerial BTSerial(1, 2); // RX | TX
void setup()
{
pinMode(4, INPUT_PULLUP);
BTSerial.begin(4800);
delay(500);
for (int i = 0; i < 10; i++)
{
MposX += analogRead(A2);
delay(10);
}
delay(100);
MposX = MposX / 10;
delay(100);
}
void loop()
{
posX = analogRead(A2);
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (posX > Xmax)
Xmax = posX;
if (posX < Xmin)
Xmin = posX;
if (posX < MposX)
posX = map(posX, MposX, Xmin, 50, 1);
else
posX = map(posX, MposX, Xmax, 51, 99);
BTSerial.println(posX);
delay(50);
}