fairy new to this.
trying to make a UVI sensor project. i can get the display to read the analog in, but i must have a massive error in my code.
As i am now trying to display a uvi of 0 - 11. and when it should be 0 i have 878 displayed.
here is my code
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int sensor_pin = A0;
int UV;
int output;
void setup()
{
pinMode(sensor_pin,INPUT);
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(32,4);
display.print(" UV ");
display.setCursor(10,19);
display.print(" SENSOR ");
display.display();
delay(4000);
}
void loop()
{
output = analogRead(sensor_pin);
int voltage = (output * (5.0 / 1023.0))*1000;
if(voltage<50)
{
UV = "0";
}else if (voltage>50 && voltage<=227)
{
UV = "0";
}else if (voltage>227 && voltage<=318)
{
UV = "1";
}
else if (voltage>318 && voltage<=408)
{
UV = "2";
}else if (voltage>408 && voltage<=503)
{
UV = "3";
}
else if (voltage>503 && voltage<=606)
{
UV = "4";
}else if (voltage>606 && voltage<=696)
{
UV = "5";
}else if (voltage>696 && voltage<=795)
{
UV = "6";
}else if (voltage>795 && voltage<=881)
{
UV = "7";
}
else if (voltage>881 && voltage<=976)
{
UV = "8";
}
else if (voltage>976 && voltage<=1079)
{
UV = "9";
}
else if (voltage>1079 && voltage<=1170)
{
UV = "10";
}
else if (voltage>1170)
{
UV = "11";
}
display.clearDisplay();
display.setTextSize(2);
display.setCursor(20,10);
display.setTextSize(2);
display.setCursor(20,10);
display.print("UV: ");
display.print(UV);
display.display();
delay(1000);
}