我怎样才能让这个继电器被我拥有的 3 个触摸传感器中的任何一个打开?
How can i make this relay be turned on by any of the 3 touch sensors i have?
我有一个继电器,我想用它打开灯,我有 2 个触摸传感器,但使用此代码我只能用 1 个打开它,我怎样才能让它工作?第三个是一个开关,但它应该仍然可以正常工作。我试过了,它使用不同的代码。但该代码用于伺服而不是继电器。如果你知道如何让它工作,你能发送完整的代码吗?
int touchPin = 2;
int relayPin = 3;
int val = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
if(val == HIGH && lightON == LOW){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);
}
delay(100);
}
我建议阅读有关 Arduino 编程的文章,这样您就可以充分了解其他人的程序中发生了什么,然后对其进行修改以适应。网上有海量资源等着你:)
我会这样写程序:
`/*
* As I understand the relay is to turn on a light,
* should either of the touch sensors be triggered
*/
const int touchPin_1 = 2;
const int touchPin_2 = 3;
const int relayPin = 4;
bool isLightOn = false;
void setup() {
pinMode(touchPin_1, INPUT);
pinMode(touchPin_2, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int touchPin_1_val = digitalRead(touchPin_1);
int touchPin_2_val = digitalRead(touchPin_2);
if (touchPin_1_val == HIGH || touchPin_2_val == HIGH) { //if either of the sensors are triggered...
isLightOn = !isLightOn; //toggle light state
}
if (isLightOn) { //if the light state is true, or set to 'on'...
Serial.println("light on");
digitalWrite(relayPin, HIGH); //on
} else {
Serial.println("light off");
digitalWrite(relayPin, LOW); //off
}
delay(100);
}`
Tnx 但我就是这样写的,效果很好
int touchPin = 6;
int touchPin2 = 7;
int touchPin3 = 5;
int relayPin = 9;
int val = 0;
int val2 = 0;
int val3 = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(touchPin2, INPUT);
pinMode(touchPin3, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
val2 = digitalRead(touchPin2);
val3 = digitalRead(touchPin3);
if((val == HIGH && lightON == LOW) || (val2 == HIGH && lightON == LOW) || (val3 == HIGH && lightON == LOW)){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);
}
delay(100);
}
我有一个继电器,我想用它打开灯,我有 2 个触摸传感器,但使用此代码我只能用 1 个打开它,我怎样才能让它工作?第三个是一个开关,但它应该仍然可以正常工作。我试过了,它使用不同的代码。但该代码用于伺服而不是继电器。如果你知道如何让它工作,你能发送完整的代码吗?
int touchPin = 2;
int relayPin = 3;
int val = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
if(val == HIGH && lightON == LOW){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);
}
delay(100);
}
我建议阅读有关 Arduino 编程的文章,这样您就可以充分了解其他人的程序中发生了什么,然后对其进行修改以适应。网上有海量资源等着你:)
我会这样写程序:
`/*
* As I understand the relay is to turn on a light,
* should either of the touch sensors be triggered
*/
const int touchPin_1 = 2;
const int touchPin_2 = 3;
const int relayPin = 4;
bool isLightOn = false;
void setup() {
pinMode(touchPin_1, INPUT);
pinMode(touchPin_2, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int touchPin_1_val = digitalRead(touchPin_1);
int touchPin_2_val = digitalRead(touchPin_2);
if (touchPin_1_val == HIGH || touchPin_2_val == HIGH) { //if either of the sensors are triggered...
isLightOn = !isLightOn; //toggle light state
}
if (isLightOn) { //if the light state is true, or set to 'on'...
Serial.println("light on");
digitalWrite(relayPin, HIGH); //on
} else {
Serial.println("light off");
digitalWrite(relayPin, LOW); //off
}
delay(100);
}`
Tnx 但我就是这样写的,效果很好
int touchPin = 6;
int touchPin2 = 7;
int touchPin3 = 5;
int relayPin = 9;
int val = 0;
int val2 = 0;
int val3 = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(touchPin2, INPUT);
pinMode(touchPin3, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
val2 = digitalRead(touchPin2);
val3 = digitalRead(touchPin3);
if((val == HIGH && lightON == LOW) || (val2 == HIGH && lightON == LOW) || (val3 == HIGH && lightON == LOW)){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);
}
delay(100);
}