mySerial.read(); Reads ÿ

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include “DHT.h”

#define DHTPIN A3
#define DHTTYPE DHT22

DHT dht(DHTPIN,DHTTYPE);
SoftwareSerial mySerial(8, 8);
LiquidCrystal lcd(12, 6, 5, 4, 3, 2);

byte tet[8] = {
0b10001,
0b10011,
0b10101,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111
};

byte mem[8] = {
0b10001,
0b10111,
0b01001,
0b10001,
0b10001,
0b10001,
0b10001,
0b10011
};

byte pe[8] = {
0b11111,
0b10001,
0b10001,
0b11101,
0b00001,
0b00001,
0b00001,
0b11111
};

byte resh[8] = {
0b11111,
0b00001,
0b00001,
0b00001,
0b00001,
0b00001,
0b00001,
0b00001,
};

byte vav[8] = {
0b01100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
};

byte lamed[8] = {
0b10000,
0b10000,
0b11111,
0b00001,
0b00001,
0b00001,
0b00001,
0b11111
};

byte het[8] = {
0b11111,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001
};

byte taf[8] = {
0b01111,
0b01001,
0b01001,
0b01001,
0b01001,
0b01001,
0b01001,
0b11001
};

byte psik[8] = {
0b00010,
0b00001,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
};

void setup()
{
pinMode(A2,OUTPUT);
pinMode(A5,OUTPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);
pinMode(7,OUTPUT);
pinMode(8,INPUT);

analogWrite(A3,HIGH);

lcd.begin(16, 2);

lcd.createChar(0,tet);
lcd.createChar(1,mem);
lcd.createChar(2,pe);
lcd.createChar(3,taf);
lcd.createChar(4,vav);
lcd.createChar(5,psik);
lcd.createChar(6,lamed);
lcd.createChar(7,het);

analogWrite(A5,0);
analogWrite(A2,1023);

Serial.begin(9600);
dht.begin();
mySerial.begin(9600);

}
void loop()
{
float TM,Tm,HM,Hm,z;

if (mySerial.available())
{
unsigned int blue= mySerial.read();
Serial.write(blue);
switch(blue)
{
case ‘T’:
lcd.clear();
lcd.print("Max temp is: ");
lcd.setCursor(0,1);
lcd.print™;
TM=reading();
lcd.clear();

break;

case ‘t’:
lcd.clear();
lcd.print(“Min temp is:”);
lcd.setCursor(0,1);
lcd.print™;
Tm=reading();
lcd.clear();

break;

case ‘H’:
lcd.clear();
lcd.print(“Max hum is:”);
lcd.setCursor(0,1);
lcd.print(HM);
HM=reading();
lcd.clear();

break;

case ‘h’:
lcd.clear();
lcd.print(“Min hum is:”);
lcd.setCursor(0,1);
lcd.print(Hm);
Hm=reading();
lcd.clear();

break;
}
}

delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();

lcd.setCursor(15,0);
lcd.write(byte(0));
lcd.setCursor(14,0);
lcd.write(byte(1));
lcd.setCursor(13,0);
lcd.write(byte(2));
lcd.setCursor(12,0);
lcd.write(byte(5));
lcd.setCursor(15,1);
lcd.write(byte(6));
lcd.setCursor(14,1);
lcd.write(byte(7));
lcd.setCursor(13,1);
lcd.write(byte(4));
lcd.setCursor(12,1);
lcd.write(byte(3));

lcd.setCursor(0,0);
lcd.print("Temp ");
lcd.print(t);
lcd.setCursor(0,1);
lcd.print(“Hum “);
lcd.print(h);
lcd.print(”%”);

if(t>HM || t<Hm || h>HM || h<Hm)
{
tone(7,1000,2500);
}

}

char wait()
{
while(1)
{
if (mySerial.read()!=-1)
{
char k;
k = mySerial.read();
Serial.print(k);
return k;
break;
}
}

}

float reading()
{
char a,b,c,d;
int a2,b2,c2,d2;
float z;
a=wait(a);
if(a==‘b’)
{return 0;}
lcd.setCursor(0,1);
lcd.print(a);
b= wait(b);
if(b==‘b’)
{return 0;}
lcd.setCursor(1,1);
lcd.print(b);
c=wait(c);
if(c==‘b’)
{return 0;}
lcd.setCursor(2,1);
lcd.print(".");
d=wait(d);
if(d==‘b’)
{return 0;}
lcd.setCursor(3,1);
lcd.print(d);

z= (a2-48)*10+(b2-48)+(d2-48)*0.1;
return z;
}

I have a project which I have to input numbers into an lcd and save them as maximum and minimum Temperature and humidity.I send the numbers using a smartphone but when I send them the program gets ÿ. What to do?

I send the numbers using a smartphone but when I send them the program gets ÿ. What to do?

Wait for them to arrive before reading them.

Please use code tags when posting code.

  analogWrite(A3,HIGH);I think it'll probably do what you intended, but it's risky!

what do you mean by "Wait for them to arrive before reading them"? what is the code for that?

SoftwareSerial mySerial(8, 8);

Which pin would that be?

I know that that is what the forum software does when you can not be bothered to post code correctly. But, that brings up the question of why you are trying to use ONE pin to send and receive data? That is NOT how serial works.

pinMode(8,INPUT);

So, you lied when you told the SoftwareSerial class that that was it's pin to use as it needed. Why?

I think it'll probably do what you intended, but it's risky!

I don't. It appears that OP is using a UNO or other 328 based Arduino. In which case, pin 17 is NOT a PWM pin. In that case, using analogWrite() with a value less than 127 will result in the pin being off.

I knew it wasn’t a PWM pin, but since the pin was set as an input in setup() I assumed the intent was to turn on the pullup.
But now that I look at analogWrite, I find that it can’t possibly do that either.

Well i didnt used RX (I need to send information to the Arduino but i dont need to get information back through the bluetooth) so i think it doesn’t matter. But to be sure I wrote 9 insteed of 8 as the RX leg and it still not working.

Yes it supose to be a pullup. What is the problem with that?

Yes it supose to be a pullup. What is the problem with that?

Pullups are turned on or off using digitalWrite(), not analogWrite().

Pullups are turned on or off using digitalWrite(), not analogWrite().

And analogWrite turns the pin into an output anyway, which was the bit I'd forgotten. (also, it would be a 255 write, not a 1023)

Alright but this doesn't solve my problem. How do i make the BlueTooth get the chars i send correctly?

I suggest you write a short sketch that does nothing except receive data from the bluetooth device and display the data on the Serial Monitor.

When that works you will know what to put into your project.

...R

HI,

char wait()
{
  while(1)
  {
    if (mySerial.read()!=-1) 
    {
      char k;
      k = mySerial.read();
      Serial.print(k);

      delay(1); //try some delay

      return k;
      break;
    }
  }
  
}

solved the problem for me...

Why put a break after a return?

Very simple serial echo code that will capture what is sent to the arduino rx pin and send the captured material out the arduino tx pin. You might try connecting the bluetooth tx to the arduino rx, and see what is sent to the serial monitor via the arduino tx. Note the bluetooth, arduino, and serial monitor need to be set to the same baud rate.

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}

delay(1); //try some delay

delay(2); //slow looping to allow buffer to fill with next character

Delays aren’t needed if it is written properly.

See: Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

@SerialQ The code you provided ill only return every other char. Look at the documentation for Serial.read().

Mark

Delays aren't needed if it is written properly.

Is "it" the the data packet being sent, or the code receiving the data packet? My simple code with a delay will receive a data packet with or without a byte delimiter included. Do all your solutions require at least one byte being used as the packet delimiter?

Async serial is (usually) by its nature not delimited by time, but by some delimiter (eg. space, newline, comma).

In this example the user says he sends "by smartphone" which I presume would send a newline or some such delimiter. Therefore simply waiting for a timeout is not correct.

But certainly, there are cases (eg. typing letters into the older-style mobile phones) where the time between characters is important.

But you still don't need a delay there. You can have a loop (looking for Serial.available) and also a condition, that if nothing becomes available after (say) one second, you consider that to be the delimiter.

If you have a delay (as in your loop) either it is too long (in which case you are processing data more slowly than you need to) or too short (in which case you miss data). And that length varies depending on the baud rate. And also on the rate at which the sending side sends stuff.