No errors, but the project doesn't work

Hello everyone. I am trying to make an alarming system to protect my eyes from laptop's screen radiation. I wrote this code and made the circuit. The code doesn't give any error messages and I also double checked my circuit according to the diagram. But when I plug the Arduino Nano in and upload the code, nothing really happens. No readings from the sonar sensor, the OLED display stays black, the LEDs don't light up and the buzzer also stays silent. I tried same circuit with the same code with another Arduino Uno. But same case. But when I test the HC-SR04 Ultrasonic Sonar Sensor or the OLED display individually, it works completely fine whether I'm using the nano or uno. Can someone help me find out where is the problem?

The code:

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET     4
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HCSR04 hc(2, 3);

const int buzzer = 8;
const int red = 5;
const int green = 6;
const int yellow = 7;

int safeDis = 40;

void setup() {
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  
  display.display();
  delay(2000);
  display.clearDisplay();
  display.drawPixel(10, 10, SSD1306_WHITE);
  display.display();
  delay(2000);
}

void loop() {
  int dis = hc.dist();
  Serial.print(dis);
  unsigned long sTime = millis ();
  unsigned long interval = 3.6e+6;
  
  if (dis <= safeDis) {
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 3);
    display.print(dis);
    display.setCursor(0, 0);
    display.print("Too Close! Get Back");
    display.display();
    digitalWrite(green, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);
    tone(buzzer, 500);
    delay(500);
    digitalWrite(red, LOW);
    noTone(buzzer);
    delay(500);
  }
  
  else if (millis () - sTime >= interval) {
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);
    display.print("Let's have a walk");
    display.display();
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(red, LOW);
    noTone(buzzer);
  }
  
  else {
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 3);
    display.print(dis);
    display.setCursor(0, 0);
    display.print("We are safe");
    display.display();
    digitalWrite(green, HIGH);
    digitalWrite(yellow, LOW);
    digitalWrite(red, LOW);
  }
}

The Circuit Diagram:

You mean the X-rays, or the Covid-19 5G radiation? :frowning:

The X-rays. :slight_smile:

You didn't use display.begin() function in the setup section. This would fix the problem

2 Likes

Hello
Check the handling of the variables for the timer function.

1 Like

You must also detect if the user is not present (out of range) for a sufficiently long interval so you can reset sTime. sTime must be either a global variable or , if you keep it in the loop(), a static variable.

1 Like

From a laptop LCD screen?

That's news to me.

1 Like

Have a look at debugging methods ( google Arduino debugging)
Add code to just print to the display or print to the serial monitor the value of some of your variables to help you spot what is going wrong , flash the leds in setup , write your code in small sections and check each as you go etc etc..

Aaaarrgh too late .. my iPhone has damaged my eyes ..:wink:

1 Like

Hi @israfil_shaheen_arannya .

I don't use this display, but I think your sketch is missing its initialization line.

display.begin(SSD1306_SWITCHCAPVCC);

RV mineirin

PS:
Be careful with the RX of your 1306 display. !!!!!!!
RV mineirin

You have red and yellow swapped. The wiring diagram shows yellow on Pin 5 and red on Pin 7.

Your wiring diagram doesn't show a "Reset" line from the OLED to Pin 4.

WARNING: Some breadboards have a break in the power rails half-way down each side. If yours does, that could explain why some parts of the circuit are not getting power and/or ground.

Steampunk laptop?
I've never seen a laptop with a crt monitor, there were a few very early portable computers with small built-in displays.

Not portable but pull-able...... Joking!

Maybe adding a ghost sensor would add quality?

And what's protecting your ears from the extremely loud 40kHz ultrasonic sound.
Leo..

Luggable was the term.

CRT in a box.
Keyboard over to protect when lugging.

Let's not forget how radar was discovered.

Haha, luggable... That's the word. Launched a kitchen made translation...

Same problem i have also face. Alter the HCSR04 module with another one and also change the Arduino UNO board. But problem is same. In serial monitor always get 0.00 (ZERO distance).

#include <HCSR04.h>

HCSR04 hc(13,11);//initialisation class HCSR04 (trig pin , echo pin)

void setup()
{ Serial.begin(9600); }

void loop()
{ Serial.println(hc.dist()); }  //return curent distance in serial

// End of the Code

This is the simple code get from the official website. But distance is not be measured.

Can anyone have any idea what is the prob?

You have then also altered the wiring to the sensor, shown in post #1, to match this new code ?

Edit

Ahhhh. I assumed (incorrectly) that you were the OP. You have simply hijacked an existing thread. But you have solved your problem in the meantime according to this ? HC-SR04 always return ZERO with Arduino UNO - #7 by TomGeorge

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