//lcd
#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;
//RGB彩灯
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初始化
lcd.init();
//lcd背光
lcd.backlight();
//波特率9600
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));
//按钮状态打印输出
//Serial.println(BT1);
//Serial.println(BT2);
//Serial.println(BT3);
//将滑动变阻器模拟量转化对应的RGB
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;
//Serial.println(T);
//按钮2按下=0
if (BT2 == 0) {
//置位开关 现在=1
i = !i;
lcd.clear();
delay(30);
}
//输出当前按钮2状态
Serial.println(i);
//按钮1按下 = 0
if (BT1 == 0) {
//现在=1
i1 = !i1;
lcd.clear();
delay(30);
}
//指定颜色
if (i == 1) {
R = 168;
G = 209;
B = 111;
}
//按钮1和按钮2松开的情况下
if (i1 == 0 && i==0) {
lcd.setCursor(0, 1);
lcd.println("R:");
lcd.setCursor(2, 1);
lcd.println(R);
lcd.setCursor(6, 1);
lcd.println("G:");
lcd.setCursor(8, 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);
}
//按钮1功能
else if(i==0) {
lcd.setCursor(0, 1);
lcd.print("zhangjiajun");
lcd.setCursor(0, 0);
lcd.print("21060401028");
delay(10);
}
//
else if (i==1) {
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);
}
}