Hello, everyone
I have a problem with my code. When I insert code 1 in the if statement in the custom void function, everything works fine, but when I insert code 2, the sensors don't work properly anymore.
Code 1:
digitalWrite(beeperPin, LOW);
digitalWrite(brakePin, LOW);
lcd.setCursor(14,0);
lcd.print("!OFF!");
lcd.setCursor(14,1);
lcd.print("!OFF!");
code 2:
lcd.setCursor(14,0);
lcd.print(distanceBackRightM);
lcd.setCursor(18,0);
lcd.print("m");
lcd.setCursor(14,1);
lcd.print(distanceBackLeftM);
lcd.setCursor(18,1);
lcd.print("m");
main code:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int beeperPin = 2;
int beeperIntervallBackRight;
int beeperIntervallBackLeft;
const int maxDistanceForBeeper = 60;
const int minDistanceForBeeper = 2;
const int maxValForBeeperIntervall = 110;
const int minValForBeeperIntervall = 10;
const int ledPin = 3;
const int motorTempPin = A0;
int motorTemp;
float motorTempSensorVal;
const int motorTempTooHigh = 50; //Temp needs to be checked
const int motorTempTooLow = -5; //Temp needs to be checked
const int batteryTempPin = A1;
int batteryTemp;
float batteryTempSensorVal;
float batteryTempSensorValRaw; //(datatype float)needs to be checked
const int batteryTempTooHigh = 50;
const int batteryTempTooLow = -5;
const int trigPinBackRight = 4;
const int echoPinBackRight = 5;
long durationBackRight;
float distanceBackRightCm;
float distanceBackRightM;
const int trigPinBackLeft = 6;
const int echoPinBackLeft = 7;
long durationBackLeft;
float distanceBackLeftCm;
float distanceBackLeftM;
const int casBackSwitchPin = 8;
int casBackSwitchState;
const int brakePin = 9;
const int maxDistanceForBrake = 150;//distance needs to be reviwed
unsigned long previousTime;
unsigned long currentTime;
void setup() {
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("casBackRight: ");
lcd.setCursor(0,1);
lcd.print("casBackLeft: ");
lcd.setCursor(0,2);
lcd.print("motorTemp: ");
lcd.setCursor(0,3);
lcd.print("batteryTemp: ");
pinMode(beeperPin, OUTPUT);
pinMode(trigPinBackRight, OUTPUT);
pinMode(echoPinBackRight, INPUT);
pinMode(trigPinBackLeft, OUTPUT);
pinMode(echoPinBackLeft, INPUT);
pinMode(casBackSwitchPin, INPUT);
Serial.begin(9600);
}
void lcdPrinting() {
if(casBackSwitchState == HIGH) {
}
}
void loop() {
casBackSwitchState = digitalRead(casBackSwitchPin);
currentTime = millis();
if(casBackSwitchState == HIGH) {
digitalWrite(trigPinBackRight, LOW);
delayMicroseconds(2);
digitalWrite(trigPinBackRight, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinBackRight, LOW);
durationBackRight = pulseIn(echoPinBackRight, HIGH);
distanceBackRightCm = durationBackRight * 0.034 / 2;
if(distanceBackRightCm <= 400) {
distanceBackRightM = (distanceBackRightCm / 100.00);
}
digitalWrite(trigPinBackLeft, LOW);
delayMicroseconds(2);
digitalWrite(trigPinBackLeft, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinBackLeft, LOW);
durationBackLeft = pulseIn(echoPinBackLeft, HIGH);
distanceBackLeftCm = durationBackLeft * 0.034 / 2;
if(distanceBackLeftCm <= 400) {
distanceBackLeftM = (distanceBackLeftCm / 100.00);
}
Serial.print(distanceBackRightM);
Serial.print(" ");
Serial.println(distanceBackLeftM);
if(distanceBackRightCm <= maxDistanceForBeeper || distanceBackLeftCm <= maxDistanceForBeeper) {
constrain(distanceBackRightCm, minDistanceForBeeper, maxDistanceForBeeper);
int beeperIntervallBackRight = map(distanceBackRightCm, minDistanceForBeeper, maxDistanceForBeeper, minValForBeeperIntervall, maxValForBeeperIntervall);
constrain(distanceBackLeftCm, minDistanceForBeeper, maxDistanceForBeeper);
int beeperIntervallBackLeft = map(distanceBackLeftCm, minDistanceForBeeper, maxDistanceForBeeper, minValForBeeperIntervall, maxValForBeeperIntervall);
if(distanceBackRightCm <= distanceBackLeftCm) {
if(currentTime - previousTime >= beeperIntervallBackRight) {
digitalWrite(beeperPin, HIGH);
if(currentTime - previousTime >= (beeperIntervallBackRight * 2)) {
digitalWrite(2, LOW);
previousTime = currentTime;
}
}
}
else if(distanceBackRightCm > distanceBackLeftCm) {
if(currentTime - previousTime >= beeperIntervallBackLeft) {
digitalWrite(beeperPin, HIGH);
if(currentTime - previousTime >= (beeperIntervallBackLeft * 2)) {
digitalWrite(2, LOW);
previousTime = currentTime;
}
}
}
}
else {
digitalWrite(beeperPin, LOW);
}
if(distanceBackRightCm <= maxDistanceForBrake || distanceBackLeftCm <= maxDistanceForBrake) {
digitalWrite(brakePin, HIGH);
}
else {
digitalWrite(brakePin, LOW);
}
}
else {
digitalWrite(beeperPin, LOW);
digitalWrite(brakePin, LOW);
}
lcdPrinting();
}