Primary Expression Error, Can't find where is the problem!

Hello! I am trying to make an alarming system to protect my eyes from laptop screen radiation. Here is the code I wrote (not completed yet. I will add some more features later). But it gives the "expected primary-expression before ')' token" error. I am looking at this code for a long time but couldn't find the mistake I made. Can someone help to find out where I messed up? Thanks in advance.

The Code:

#include<HCSR04.h>
#include<Wire.h>
#include<Adafruit_GFX.h>
#include<Adafruit_SSD1306.h>

#define reset 13
const int buzzer = 4;
const int red = 5;
const int green = 6;
const int yellow = 7;

int safeDis = 50;
int state = 0;

HCSR04 hc(2, 3);
Adafruit_SSD1306 oled(reset);

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);

}

void loop() {
  int dis = hc.dist();

  if (dis < safeDis) {
    state = 1;
  }
  else (){
    state = 0;
  }
  
  if (state = 1) {
    oled.clearDisplay();
    oled.setCursor(0,0);
    oled.setTextColor(WHITE);
    oled.setTextSize(1);
    oled.print("!! Too Close !! Get Back !!");
    oled.display();
    digitalWrite(green, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);
    tone(buzzer, 5000);
    delay(500);
    digitalWrite(red, LOW);
    noTone(buzzer);
    delay(500);
  }
  else (){
    oled.clearDisplay();
    oled.setCursor(0,0);
    oled.setTextColor(WHITE);
    oled.setTextSize(1);
    oled.print("Yayy, We are Safe");
    oled.display();
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);
    noTone(buzzer);
  }
}

Here is the error message:

Perhaps the OP may want to review how an If..else thingy works. clicky clicky for a link to if else syntax.

Oops

Even if you looked at examples of proper if/else syntax for many minutes, you might never notice see the extraneous and erroneous open and close parentheses ‘()’ that you have placed after your else. In all cases it seems.

What you never saw in any working program.

a7

As a general piece of advice , when you write a sketch do small bits at a time and save, compile regularly .
That will help you find where you’ve added an error .
In your circumstance , comment out bits of code until it will compile , that will help find the error .
When it’s running use serial print or outputs to leds to help you know where you program is at various stages .

Opsss.......!! My bad.....!! Am I an idiot! Thanks everyone for your replies.

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