Need help for Arduino project

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.

Your transmitter is sending data at a very high rate. Try a delay in its loop().

Sadly, it won’t work as long as soft serial is above about 9600/19200

The cpu just doesn’t have enough time to reliably pretend it’s a UART

hello... i need help for how to changed this code from using 1 ultrasonic sensor to 4 ultrasonic sensor

int tonePin = 8;    // buzzer pin
int trigPin = 9;    // sensor trigger pin
int echoPin = 10;   // sensor echo pin
int clockPin = 11;  // shift register clock pin
int latchPin = 12;  // shift register latch pin
int dataPin = 13;   // shift register data pin

unsigned long previousMillisLEDS = 0; // initialization of the previosMillis for LEDS
unsigned long previousMillisLOW = 0;  // initialization of the previosMillis for LOW distance buzzer
unsigned long previousMillisMID = 0;  // initialization of the previosMillis for MED distance buzzer
unsigned long previousMillisHIGH = 0; // initialization of the previosMillis for HIGH distance buzer

const int intervalLEDS = 100; // interval of refreshing the LEDS state
const int intervalLOW = 800;  // interval of the LOW distance buzzer
const int intervalMID = 400;  // interval of the MED distance buzzer
const int intervalHIGH = 100; // interval of the HIGH distance buzzer

const int freqLOW = 1500;  // frequency of the LOW distabce buzzer
const int freqMID = 1800;  // frequency of the MED distabce buzzer
const int freqHIGH = 2000; // frequency of the HIGH distabce buzzer

const int durLOW = 100;   // on time of the LOW distabce buzzer
const int durMED = 100;   // on time of the MED distabce buzzer
const int durHIGH = 80;   // on time of the HIGH distabce buzzer

const byte patterns[9] = {   // initialization of the patterns the LEDS are going to display
  B00000000,  // all LEDS OFF
  B00000001,  // 1 LED ON
  B00000011,  // 2 LEDS ON
  B00000111,  // 3 LEDS ON
  B00001111,  // 4 LEDS ON
  B00011111,  // 5 LEDS ON
  B00111111,  // 6 LEDS ON
  B01111111,  // 7 LEDS ON
  B11111111,  // 8 LEDS ON
};

int prox = 0; // initialization of the proximity value (0-8)
int dur;      // initialization of the duration between the Trigger and Echo signal of the sensor
int dist;     // initialization of the distance between the sensor and the object in front of it (in centimeters)

void setup() {
  pinMode(tonePin, OUTPUT);   // set tone pin to OUTPUT
  pinMode(trigPin, OUTPUT);   // set trigger pin to OUTPUT
  pinMode(echoPin, INPUT);    // set echo pin to INPUT
  pinMode(clockPin, OUTPUT);  // set clock pin to OUTPUT
  pinMode(latchPin, OUTPUT);  // set latch pin to OUTPUT
  pinMode(dataPin, OUTPUT);   // set data pin to OUTPUT
}

void loop() {

  unsigned long currentMillis = millis(); // set the currentMillis variable to the current number of milliseconds from the start of the loop

  if ((unsigned long)(currentMillis - previousMillisLEDS) >= intervalLEDS) {    // check if the time between the current time and previous time for LEDS is larger or equal to the interval the LEDS should stay on
    digitalWrite(latchPin, LOW);  // set the latch pin to LOW
    digitalWrite(trigPin, LOW);   // set the trigger pin to LOW
    delayMicroseconds(2);         // delay 2 microseconds
    digitalWrite(trigPin, HIGH);  // set the trigger pin to HIGH and send out a sound signal
    delayMicroseconds(100);       // delay 100 microseconds
    digitalWrite(trigPin, LOW);   // set the trigger pin to LOW
    dur = pulseIn(echoPin, HIGH); // caluclate the duration between the trigger and echo
    dist = dur / 2 / 29;          // calculate distance in centemeters based on the speed of sound

    prox = map(dist, 0, 48, 8, 0);    // map the distannce between 0 and 48 cm to a value between 0 and 8
    shiftOut(dataPin, clockPin, MSBFIRST, patterns[prox]);  // send the pattern to the shift register based on the prox value (0-8)
    digitalWrite(latchPin, HIGH);   // latch the shift register
    previousMillisLEDS = currentMillis;   // set the previousMillis for LEDS to the current time in milliseconds from the start of the loop
  }
  if (prox < 0) {   // if we get a proximity value less than 0 set it to 0
    prox = 0;
  }
  else if (prox == 6) {   // if we get a proximity value of 6
    if ((unsigned long)(currentMillis - previousMillisLOW) >= intervalLOW) {   // check if the time between the current time and previous time for LOW buzzer interal is larger or equal to the interval the buzzer should stay on
      tone(tonePin, freqLOW, 100);    // set the tone pin to the LOW frequency and an on time of 100 milliseconds
      previousMillisLOW = currentMillis; // set the previousMillis for LOW buzzer interal to the current time in milliseconds from the start of the loop
    }

  }
  else if (prox == 7) { // if we get a proximity value of 7
    if ((unsigned long)(currentMillis - previousMillisMID) >= intervalMID) {  // check if the time between the current time and previous time for MID buzzer interal is larger or equal to the interval the buzzer should stay on
      tone(tonePin, freqMID, 100);    // set the tone pin to the MID frequency and an on time of 100 milliseconds
      previousMillisMID = currentMillis;  // set the previousMillis for MID buzzer interal to the current time in milliseconds from the start of the loop
    }

  }
  else if (prox == 8) { // if we get a proximity value of 8
    if ((unsigned long)(currentMillis - previousMillisHIGH) >= intervalHIGH) {  // check if the time between the current time and previous time for HIGH buzzer interal is larger or equal to the interval the buzzer should stay on
      tone(tonePin, freqHIGH, 80);    // set the tone pin to the HIGH frequency and an on time of 80 milliseconds
      previousMillisHIGH = currentMillis;   // set the previousMillis for HIGH buzzer interal to the current time in milliseconds from the start of the loop
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.