#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
pinMode(13, INPUT);
pinMode(2, OUTPUT);
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("Robotronix"); // set text
oled.display(); // display on OLED
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
}
void loop()
{
int R1=analogRead(26);
int R2=analogRead(27);
int R3=analogRead(14);
int R4=analogRead(12);
int R5=analogRead(13);
int S1 = map(R1,0,4098,0,255);
int S2 = map(R2,0,4098,0,255);
int S3 = map(R3,0,4098,0,255);
int S4 = map(R4,0,4098,0,255);
int S5 = map(R5,0,4098,0,255);
char C1 = 'X';
char C2 = 'X';
char C3 = 'X';
char C4 = 'X';
char C5 = 'X';
//==========================================
//Sensor 1
if (S1 < 50)
{
C1 = 'n';
}
else if(S1 < 100)
{
C1 = 'G';
}
else if(S1 < 150)
{
C1 = 'B';
}
else if(S1 < 200)
{
C1 = 'R';
}
else
{
C1 = 'W';
}
//==========================================
//Sensor 2
if (S2 < 50)
{
C2 = 'n';
}
else if(S2 < 100)
{
C2 = 'G';
}
else if(S2 < 150)
{
C2 = 'B';
}
else if(S2 < 200)
{
C2 = 'R';
}
else
{
C2 = 'W';
}
//==========================================
//Sensor 3
if (S3 < 50)
{
C3 = 'n';
}
else if(S3 < 100)
{
C3 = 'G';
}
else if(S3 < 150)
{
C3 = 'B';
}
else if(S3 < 200)
{
C3 = 'R';
}
else
{
C3 = 'W';
}
//==========================================
//Sensor 4
if (S4 < 50)
{
C4 = 'n';
}
else if(S4 < 100)
{
C4 = 'G';
}
else if(S4 < 150)
{
C4 = 'B';
}
else if(S4 < 200)
{
C4 = 'R';
}
else
{
C4 = 'W';
}
//==========================================
//Sensor 5
if (S5 < 50)
{
C5 = 'n';
}
else if(S5 < 100)
{
C5 = 'G';
}
else if(S5 < 150)
{
C5 = 'B';
}
else if(S5 < 200)
{
C5 = 'R';
}
else
{
C5 = 'W';
}
//==========================================
//Serial print
Serial.println("Sensor :");
Serial.println("S1 S2 S3 S4 S5");
Serial.println("Value :");
Serial.print(S1);
Serial.print(" ");
Serial.print(S2);
Serial.print(" ");
Serial.print(S3);
Serial.print(" ");
Serial.print(S4);
Serial.print(" ");
Serial.println(S5);
Serial.println("Colour :");
Serial.print(C1);
Serial.print(" ");
Serial.print(C2);
Serial.print(" ");
Serial.print(C3);
Serial.print(" ");
Serial.print(C4);
Serial.print(" ");
Serial.println(C5);
delay(200); // this speeds up the simulation
//==========================================
//OLED print
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(20, 10); // set position to display (x,y)
oled.println("COLOUR DETECTED"); // set text
oled.setCursor(10, 25); // set position to display (x,y)
oled.println("S1 S2 S3 S4 S5"); // set text
oled.setCursor(10, 35); // set position to display (x,y)
oled.println(S1);
oled.setCursor(35, 35);
oled.println(S2);
oled.setCursor(60, 35);
oled.println(S3);
oled.setCursor(85, 35);
oled.println(S4);
oled.setCursor(110, 35);
oled.println(S5);
oled.setCursor(10, 45); // set position to display (x,y)
oled.println(C1);
oled.setCursor(35, 45);
oled.println(C2);
oled.setCursor(60, 45);
oled.println(C3);
oled.setCursor(85, 45);
oled.println(C4);
oled.setCursor(110, 45);
oled.println(C5);
oled.display(); // display on OLED
delay(200); // wait two seconds for initializing
oled.clearDisplay(); // clear display
}