Hello and a good afternoon,
I need some help regarding this problem I have when trying to build a code that uses serial communication between two Arduino Unos. The sensors I'm using are TF-Luna and JSN-SR04T ultrasonic sensor. The code below is my whole code and the receiving Arduino. The problem lies in void equation() where I can't receive my Serial.write value from the transmitting arduino reliably from the incomingByte.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SimpleTimer.h>
#include <SoftwareSerial.h>
SoftwareSerial Serial1(2,3);
LiquidCrystal_I2C lcd(0x27,20,4);
#define TRIG 8
#define ECHO 9
const int error = 5;
int dist;
int check;
int i;
int uart[9];
int incomingByte;
float x = 0;
float p = 0;
float TTC = 0;
float spd = 0;
float range = 0;
const int HEADER=0x59;
SimpleTimer timer;
void lidar() {
if (Serial1.available()) {
if(Serial1.read() == HEADER) {
uart[0]=HEADER;
if (Serial1.read() == HEADER) {
uart[1] = HEADER;
for (i = 2; i < 9; i++) {
uart[i] = Serial1.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)){
dist = uart[2] + uart[3] * 256;
lcd.setCursor(8,0);
lcd.print("D2:");
lcd.print(dist);
lcd.print(" ");
if(dist>400){
range= (dist*7.5)/100;
}
}
}
}
}
}
int get_ultrasonic() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG, LOW);
int distance = pulseIn(ECHO, HIGH,26000);
distance= distance/58;
distance = distance - error;
lcd.setCursor(0,0);
lcd.print("D1:");
lcd.print(distance);
lcd.print(" ");
if(distance<=400){
range= (distance*7.5)/100;}
return distance;
}
void equation(){
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("I received: ");
Serial.println(incomingByte);
spd=incomingByte;
}
TTC = (spd*1000)/(3600*range);
x = -9.073+24.225*(TTC)+0.0534*(spd/1.609);
p = 1/(1+pow(2.7183,-x));
Serial.println(spd);
lcd.setCursor(0,1);
lcd.print("S:");
lcd.print(spd);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print("P:");
lcd.print(p);
lcd.print(" ");
if(p>=0.75){
tone(13, 1000); //Buzzer on
delay (30);
noTone(13); // Buzzer off
delay (30);
analogWrite(10, 255); //LED HIGH
}else{
noTone(13); // Buzzer off
analogWrite(10, 0); //LED OFF
}
}
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
Serial1.begin(115200);
timer.setInterval(5, lidar);
timer.setInterval(100, get_ultrasonic);
timer.setInterval(500, equation);
pinMode (10, OUTPUT);
pinMode (13, OUTPUT);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT_PULLUP);
}
void loop() {
timer.run();
}
This is the code from my transmitting arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write(45); // send a byte with the value 45
}
What I wanted to get was only 45, then its value would be sent to the speed variable. However, the values would always change every time whenever I open my serial monitor or change the code. It would change from 45 to 10/45/75/106/169. I would assume the sensors were bothering with the values but I'm not quite sure. Afterwards, I need to include another sensor on the transmitting arduino, so this was only a test on serial communication.
Is there another way to fix it? Thanks.