1 单词
word | meaning |
---|---|
water level sensor | 水位传感器 |
analog | 模拟 |
rainfall | 降雨量 |
refraction | 折射 |
2 传感器
代码和线路图
代码:将水位值转换成厘米
int water;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(8,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
water=analogRead(A0);
float x=water/175.0;
Serial.println(x);
}
代码:加个呼吸灯,0到1亮绿色,2到3亮红色,4以上亮蓝色
int water,r=11,g=10,b=9;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(r,OUTPUT);
pinMode(g,OUTPUT);
pinMode(b,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
water=analogRead(A0);
int x=map(water,0,700,0,4);
digitalWrite(g,!(x>=0&&x<2));
digitalWrite(r,!(x>=2&&x<4));
digitalWrite(b,!(x>=4));
delay(100);
}
代码:当x>=2时,抽水
int water,r=11,g=10,b=9;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(r,OUTPUT);
pinMode(g,OUTPUT);
pinMode(b,OUTPUT);
pinMode(13,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
water=analogRead(A0);
int x=map(water,0,700,0,4);
digitalWrite(g,!(x>=0&&x<2));
digitalWrite(r,!(x>=2&&x<4));
digitalWrite(13,x>=2);
digitalWrite(b,!(x>=4));
delay(100);
}
加一个蜂鸣器:x>=3抽水,x>=2报警
int water,r=11,g=10,b=9;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(r,OUTPUT);
pinMode(g,OUTPUT);
pinMode(b,OUTPUT);
pinMode(13,OUTPUT);
pinMode(6,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
water=analogRead(A0);
int x=map(water,0,700,0,4);
digitalWrite(g,!(x>=0&&x<2));
digitalWrite(r,!(x>=2&&x<4));
digitalWrite(13,x>2);
digitalWrite(b,!(x>=4));
if(x>=2) analogWrite(6,233);
else analogWrite(6,0);
delay(100);
}