#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const float BETA = 3950;
const int pinR = 3;
const int pinG = 5;
const int pinB = 6;
const int potR = A0;
const int potG = A1;
const int potB = A2;
#define BUTTON_PIN1 8
#define BUTTON_PIN2 9
#define BUTTON_PIN3 10
int i = 0;
int i1 = 0;
int qp = 0;
int R;
int G;
int B;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(potR, INPUT);
pinMode(potG, INPUT);
pinMode(potB, INPUT);
pinMode(2, OUTPUT);
pinMode(BUTTON_PIN1, INPUT_PULLUP);
pinMode(BUTTON_PIN2, INPUT_PULLUP);
pinMode(BUTTON_PIN3, INPUT_PULLUP);
}
int readPot(int pin) {
return map(analogRead(pin), 0, 1023, 0, 255);
}
void loop() {
int BT1 = digitalRead((BUTTON_PIN1));
int BT2 = digitalRead((BUTTON_PIN2));
int BT3 = digitalRead((BUTTON_PIN3));
R = readPot(potR);
G = readPot(potG);
B = readPot(potB);
int temper = analogRead(A3);
float T = 1 / (log(1 / (1023. / temper - 1)) / BETA + 1.0 / 298.15) - 273.15;
if (BT2 == 0) {
i = !i;
lcd.clear();
delay(30);
}
Serial.println(i);
if (BT1 == 0) {
i1 = !i1;
lcd.clear();
delay(30);
}
if (i == 1) {
R = 168;
G = 209;
B = 111;
}
if (BT1 == 0 && i==0) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.println("R:");
lcd.setCursor(2, 1);
lcd.println(R);
lcd.setCursor(5, 1);
lcd.println("G:");
lcd.setCursor(7, 1);
lcd.println(G);
lcd.setCursor(11, 1);
lcd.println("B:");
lcd.setCursor(13, 1);
lcd.println(B);
lcd.setCursor(0, 0);
lcd.println("Temper:");
lcd.setCursor(7, 0);
lcd.println(T);
delay(10);
}
else if(i==0) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("chenziying");
lcd.setCursor(0, 0);
lcd.print("21060401121");
delay(10);
}
else if (i==1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("RGB:");
lcd.setCursor(4, 0);
lcd.println(R);
lcd.setCursor(8, 0);
lcd.println(G);
lcd.setCursor(12, 0);
lcd.println(B);
lcd.setCursor(0, 1);
delay(10);
}
if (T > 30) {
digitalWrite(2, HIGH);
delay(30);
}
else
{
analogWrite(2, LOW);
delay(30);
}
analogWrite(pinR, R);
analogWrite(pinG, G);
analogWrite(pinB, B);
if (BT3 == 0 && i == 1) {
analogWrite(pinR, 0);
analogWrite(pinG, 0);
analogWrite(pinB, 0);
i = 0;
lcd.clear();
lcd.backlight();
delay(8000);
}
}