Hello. I have a problem with my code. I can't understand where i'm making mistake, but i'm sure i have. I'm new to programing and don't kwot most of the things. Now i want to display information on oled display with IR sensor (to show when there is detected an object, and when there is not). When i upload the code the display shows either detected or not detected(depending if the data in the bracets is LOW or HIGH).i try to change small parts of the code that i think may cause this issue but nothing changes.
here is the code:
#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 -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int sensor = 18;
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop()
{
int data = digitalRead(sensor);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
if (data == LOW) display.println("Detected");
else display.println("Not Detected");
display.display();
delay(1000);
}
I'm open to any tips
I think that the problem may be in the IF and ELSE function or in the int, but i may be wrong
The purpose of this project is when i place my hand or object to display "detected", and when removed to display "not detected"