我有一个带有2个接近传感器,两个LED和一个用于启动/结束程序的拨动开关的电路。
激活时,传感器下降(sD)打开(ledB),然后关闭(ledG)。 激活后,传感器上升(sU)会打开(ledG),然后关闭(ledB)。
上下文: 一旦活塞泵达到冲程末端,接近传感器就会发出信号,这将告诉arduino切换一个 电磁阀朝另一个方向,使泵反向。 (指示灯代表螺线管,以便于 测试)
问题: 当拨动开关切换到关闭位置时,如果泵处于中冲程,则它将不会移动,直到电磁阀 开启后,泵达到冲程终点,这将激活传感器以启动过程。 我需要一个led /电磁阀来点亮,然后在其中一个代理传感器被激活后立即关闭。
编辑: 有关我拥有的以及我要实现的目标的视频: https://drive.google.com/file/d/15bgbLU_OcVZIzw9IDD_5R_cjSMGxHwGZ/view?usp=sharing
感谢您的任何输入...
这让我感到难过
-山姆
int sD = 4;
int ledB = 2;
int ledG = 3;
int sU = 5;
int mainSwitch = 7;
int ledBin = 8;
int ledGin = 9;
void setup()
{
// put your setup code here, to run once:
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
void loop()
{
mainSwitch = digitalRead(7);
if (mainSwitch == true) //Ends Program
{
sD = digitalRead(4);
sU = digitalRead(5);
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
if (mainSwitch == false)//Starts program
{
sD = digitalRead(4);
sU = digitalRead(5);
if (sD == false) digitalWrite(ledB, HIGH); //sD(proximity sensor) Turns on ledB turns off ledG when activated
if (sD == false) digitalWrite(ledG, LOW);
if (sU == false) digitalWrite(ledG, HIGH); //sU(poximity sensor) Turns on ledG turns off ledB when activated
if (sU == false) digitalWrite(ledB, LOW);
}
}
答案 0 :(得分:0)
据我所知
当您在Arduino上使用digitalRead()时,它可以返回“ HIGH”或“ LOW”
if (sD == false)
if (sU == false)
尝试将“ false”更改为“ LOW”,看看是否适合您。
请不要忘记查看here以获取更多详细信息。
答案 1 :(得分:0)
我知道您需要在主开关打开时激活一个或另一个LED(活塞)。您可以在终点处更改它们,并在未到达终点时保持它们不变。
启动时,您必须以任意(或预定义)方向启动,但不能像现在一样关闭两个LED。因此,没有太多的遗漏。
不要混淆恒定的引脚号及其可变状态:)
const byte ledB = 2;
const byte ledG = 3;
// other pin numbers, hardcoded usage in code
// sU = 5; sD = 4;
// mainSwitch = 7;
bool dir = true; // Direction: default = up
void setup()
{
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
}
void loop()
{
bool mainSwitch = digitalRead(7);
if (mainSwitch == true) // stops Program
{
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
else
{ // normal mode, one of both LED on
// Read end switches
bool sD = digitalRead(4);
bool sU = digitalRead(5);
delay(5); // debounce, if these are mechanical switches
if (sD) dir = true; // switch to Up
if (sU) dir = false; // switch to Down
// else don't change direction
if (dir) {
digitalWrite(ledG, LOW);
digitalWrite(ledB, HIGH);
}
else {
digitalWrite(ledB, LOW);
digitalWrite(ledG, HIGH);
}
}
}
答案 2 :(得分:0)
我已经解决了自己的问题,感谢您的帮助。 -山姆
int sD = 4;
int ledB = 2;
int ledG = 3;
int sU = 5;
int mainSwitch = 7;
int ledBin = 8;
int ledGin = 9;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
void loop()
{
mainSwitch = digitalRead(7);
if (mainSwitch == true) //Ends Program
{
sD = digitalRead(4);
sU = digitalRead(5);
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
if (mainSwitch == false)//Starts program
{
sU = digitalRead(5);
sD = digitalRead(4);
if (sU == HIGH) //Turns on ledG(down solenoid) at first start. sU(upper
sensor) in deactivated
{
ledGin = digitalRead(9);
ledBin = digitalRead(8);
if (ledBin == LOW)digitalWrite(ledG, HIGH);
}
else if (sU == LOW) //sU(upper sensor) is activated turning on ledB(down
solenoid) turning off ledG(up solenoid)
{
ledBin = digitalRead(8);
ledGin = digitalRead(9);
if (ledBin == LOW) digitalWrite(ledB, HIGH);//ledB(down solenoid) turn on
if (ledGin == HIGH) digitalWrite(ledG, LOW);//ledG(up solenoid) turn off
}
if (sD == LOW)//sD(Bottom sensor) is activated turning on ledB(down
solenoid) turning off ledG(down solenoid)
{
ledBin = digitalRead(8);
ledGin = digitalRead(9);
if (ledBin == HIGH) digitalWrite(ledB, LOW);//ledB(down solenoid) turn off
if (ledGin == LOW) digitalWrite(ledG, HIGH);//ledB(up solenoid) turn on
}
}
}.