Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 Sensor Issues #11

Open
lockarde opened this issue Apr 21, 2019 · 0 comments
Open

2 Sensor Issues #11

lockarde opened this issue Apr 21, 2019 · 0 comments

Comments

@lockarde
Copy link

I'm trying to write code for 2 sensors, it takes a distance average, then, depending on that value, either flashes a light or keeps it solid. I can get it to work flawlessly with one sensor, but when I add the second sensor code, the output stops after going through the loop once. I think I'm setting up the serial ports incorrectly? I noticed another person had a similar issue, but it didn't prove helpful for solving my problem. the issue starts where comment is ** Any help is greatly appreciated; my code is below and an image of Serial Monitor:
`
#include <Arduino.h>
#include <stdint.h>
#include <Wire.h>
#include "stdio.h"
#include <DFRobot_TFmini.h>
#include <SoftwareSerial.h>
#include "TFMini.h"

// Setup software serial port for both sensors
SoftwareSerial mySerial_L(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
SoftwareSerial mySerial_R(6, 7); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini_L;
TFMini tfmini_R;
int i, j;
int LED_pin_L = 12; //sets LED for LEFT signal as pin 13
int LED_pin_R = 13; //sets LED for RIGHT signal as pin 12
uint16_t dist_L[5], dist_R[5], avg_R, avg_L;

void setup() {
Serial.begin(115200);
pinMode(LED_pin_R, OUTPUT);
pinMode(LED_pin_L, OUTPUT);
}

void loop() {
mySerial_L.begin(115200);
tfmini_L.begin(&mySerial_L);

uint16_t sumR = 0, sumL = 0, veh_apr_L = 0; // sets all variables to 0, resets every loop iteration
uint16_t avg_R = 0, avg_L = 0; //Measure Distance and get signal strength
for (i = 0; i < 5; i++) //gets L sensor data
{
dist_L[i] = tfmini_L.getDistance();
sumL += dist_L[i];
Serial.println(dist_L[i]); //for testing so I can see each data point
delay(50);
}
avg_L = (sumL / 5);
Serial.print("Distance = ");
Serial.print(avg_L);
Serial.println("cm");
delay(50);

mySerial_L.end();
//**This is where it seems to have issues
// **If I remove everything after this relating to second
// **sensor, it works perfectly
delay(50);
mySerial_R.begin(115200);
tfmini_R.begin(&mySerial_R);

for (i = 0; i < 5; i++) //gets R sensor data
{
dist_R[i] = tfmini_R.getDistance();
sumL += dist_R[i];
Serial.println(dist_R[i]);
delay(50);
}
avg_R = (sumR / 5);
Serial.print("Distance = ");
Serial.print(avg_R);
Serial.println("cm");
delay(50);

if (avg_L >= 50 && avg_L < 90)
{
digitalWrite(LED_pin_L, HIGH);
}

else if (avg_L < 50)
{
for (i = 0; i < 8; i++)
{
digitalWrite(LED_pin_L, LOW);
delay(62);
digitalWrite(LED_pin_L, HIGH);
delay(62);
}
}
else
{
digitalWrite(LED_pin_L, LOW);
}

if (avg_R >= 50 && avg_R < 90)
{
digitalWrite(LED_pin_R, HIGH);
}

else if (avg_R < 50)
{
for (i = 0; i < 8; i++)
{
digitalWrite(LED_pin_R, LOW);
delay(62);
digitalWrite(LED_pin_R, HIGH);
delay(62);
}
}
else
{
digitalWrite(LED_pin_R, LOW);
}
delay(50);
}
`
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant