Hello, everyone
I'm working on my first project and I've encountered an issue called "else" without a previous "if".
Here's my code:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int beeperPin = 2;
int beeperIntervallBackRight;
int beeperIntervallBackLeft;
const int maxDistanceForBeeper = 60; //distance needs to be reviewed
const int minDistanceForBeeper = 2; //distance needs to be reviewed
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; //needs to be ckecked(could be( = 0))
const int brakePin = 9;
const int maxDistanceForBrake = 150;//distance needs to be reviwed
unsigned long previousTime; //needs to be ckecked(could be( = 0))
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(ledPin, OUTPUT);
pinMode(trigPinBackRight, OUTPUT);
pinMode(echoPinBackRight, INPUT);
pinMode(trigPinBackLeft, OUTPUT);
pinMode(echoPinBackLeft, INPUT);
pinMode(casBackSwitchPin, INPUT);
pinMode(brakePin, OUTPUT);
}
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);
}
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);
}
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");
}
else {
digitalWrite(beeperPin, LOW);
digitalWrite(brakePin, LOW);
lcd.setCursor(14,0);
lcd.print("!OFF!");
lcd.setCursor(14,1);
lcd.print("!OFF!");
}
motorTempSensorVal = analogRead(A0);
motorTemp = (motorTempSensorVal) * 0.48848125;
batteryTempSensorValRaw = analogRead(A1);
batteryTempSensorVal = (batteryTempSensorValRaw / 1024.0) * 5.0;
batteryTemp = (batteryTempSensorVal - 0.5) * 100; //Might be .5 instead of 0.5(kit)
lcd.setCursor(16,2);
lcd.print(motorTemp);
if(motorTemp >= motorTempTooHigh) {
digitalWrite(ledPin, HIGH);
lcd.setCursor(18,2);
lcd.print("!");
}
else if(motorTemp <= motorTempTooLow) {
digitalWrite(ledPin, HIGH);
lcd.setCursor(18,2);
lcd.print("!");
}
else {
digitalWrite(ledPin, LOW);
lcd.setCursor(18,2);
lcd.print("C");
}
lcd.setCursor(16,3);
lcd.print(batteryTemp);
if(batteryTemp >= batteryTempTooHigh) {
digitalWrite(ledPin, HIGH);
lcd.setCursor(18,3);
lcd.print("!");
}
else if(batteryTemp <= batteryTempTooLow) {
digitalWrite(ledPin, HIGH);
lcd.setCursor(18,3);
lcd.print("!");
}
else {
digitalWrite(ledPin, LOW);
lcd.setCursor(18,3);
lcd.print("C");
}
}