[英]Could anyone help me with an Arduino sketch to control motor operation?
I am making Arduino sketch to control motor operation according to water level in tank. 我正在制作Arduino草图以根据水箱中的水位控制电动机的运行。 The model is Arduino atMega 2560.
该模型是Arduino at Mega 2560。
When lower button(water in tank is low) is LOW the led should become HIGH (motor will pump water) and when upper Button (water tank is filled) is HIGH led(motor) should get LOW. 当下部按钮(水箱中的水低)为LOW时,指示灯应变高(电动机将抽水),而当上部按钮(水箱注满)为高时,LED(电动机)应变低。
This code not working: 此代码不起作用:
const int lowerButtonPin = 2;
const int upperButtonPin = 3;
const int ledPin = 6;
int lowerButtonState = 0;
int upperButtonState = 0;
/***************************setup****************************/
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(lowerButtonPin, INPUT);
pinMode(upperButtonPin, INPUT);
digitalWrite(ledPin,LOW);
}
/**********loop****************/
void loop() {
lowerButtonState = digitalRead(lowerButtonPin);
if(lowerButtonState==HIGH){
digitalWrite(ledPin,HIGH);
}
upperButtonState = digitalRead(upperButtonPin);
if(upperButtonState==HIGH){
digitalWrite(ledPin,LOW);
}
}
Could anyone help me out? 有人可以帮我吗?
What I can tell is that when you're lower button is low, the led should become high. 我可以告诉您的是,当您按下按钮较低时,LED应该变高。 But here you've put lower button high, led high:
但是在这里,您将下按钮设为高,将led设为高:
lowerButtonState = digitalRead(lowerButtonPin);
if(lowerButtonState==HIGH){
digitalWrite(ledPin,HIGH);
}
Try this: 尝试这个:
lowerButtonState = digitalRead(lowerButtonPin);
if(lowerButtonState==LOW){ // If lower button is low
digitalWrite(ledPin,HIGH); // Set led high
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.