我想使用c
和pic +光电二极管制作一个人
我的代码使用c
:
假设我们在门前有两个二极管来计算来电人数和一个
在门后面减少左边人数,我的问题由代码:
while(1)
{
//suppose we declare a count variable to hold the count
if(d1 == 1) //suppose it's the first diode and when it's cut it's value will be one
{
count++;
}
if(d2 == 1)// represent d2
{
count--;
} // now my problem is when the count is increase by one it will decrease also by one
}
你能帮忙吗?
答案 0 :(得分:0)
我并不完全确定你是如何进行此设置的,但我认为您需要在阅读后清除变量。因此,您的代码需要看起来更像这样:
int inOut = 0; // Positive for in negative for out
while(1)
{
if(d1 == 1)
{
if(d2 == 1)
{
count += inOut;
d1 = 0;
d2 = 0;
}
else
{
inOut = 1;
}
}
else if(d2 == 1)
{
inOut = -1;
}
}
答案 1 :(得分:0)
这应该有用,它不优雅,但它是:
int Entered = 0;
int Exited = 0;
while(1)
{
if(Exited == 0 && d1 == 1 && d2 == 0)
{
Entered = 1;
}
if(Entered == 1 && d1 == 0 && d2 == 1)
{
count++;
Entered = 0;
}
if(Entered == 0 && d1 == 0 && d2 == 1)
{
Exited = 1;
}
if(Exited == 1 && d1 == 1 && d2 == 0)
{
count--;
Exited = 0;
}
}
如果你不明白,请问我。
答案 2 :(得分:0)
亲爱的,你的回答是行不通的,因为有人进入它会削减第二个二极管,它会减少计数器,但最后我想我得到了答案: 我们将改变每个二极管的位置,使其彼此相邻并延迟 //假设我们有3个变量,in,out和finally count
while(1)
{
if(D1 == 1) //somone come
{
count++;
D1 = D2 = 0;
}
delay(time)
if(D2 == 1)
{
count--;
D1 = D2 = 0;
}
}