int IR = A2;
#define model1 20150
float IRvalue;
int pRightButton = 2;
int pLeftButton = 3;
int RightbuttonStatus = 0;
int LeftbuttonStatus = 0;
void setup() {
pinMode (IR,OUTPUT);
pinMode(pRightButton,INPUT);
pinMode(pLeftButton,INPUT);
Serial.begin(9600);
}
void loop()
{
int IRvalue= analogRead(IR);
Serial.println(IRvalue);
Serial.print("IR value: ");
delay(200);
int RightbuttonStatus = digitalRead(pRightButton);
int LeftbuttonStatus = digitalRead(pLeftButton);
int distance = map(IRvalue,0,1000,20,150);
if((RightbuttonStatus == HIGH || LeftbuttonStatus == HIGH) || (RightbuttonStatus == HIGH && LeftbuttonStatus == HIGH));
{
???????
}
}
system
July 23, 2019, 2:00pm
3
arduino how to control IRsensor , gp2y0a02yk0f,sense 20 to 150 ,with button,
What? Don't try to ask your entire question in the thread title.
if((RightbuttonStatus == HIGH || LeftbuttonStatus == HIGH) || (RightbuttonStatus == HIGH && LeftbuttonStatus == HIGH));
Write out an English description of what that statement is supposed to do. As written, it does not make sense.
Compound if statements are much harder to debug, and to ensure that you have complete coverage, than nested if/else if statements.
if(RightButtonStatus == HIGH)
{
doSomething();
}
else if(LeftbuttonStatus == HIGH)
{
doSomething(); // or doSomethingElse();
}
else if( // more simple conditions)
{
// do the right thing(s)
}
Notice that my does does NOT have semicolons at the end of the if and else if statements. It is extremely unlikely that yours should have them.
It is also extremely unlikely that your code should have any calls to delay().
Now, what was your question?
system
July 23, 2019, 2:03pm
4
PaulS:
Notice that my does does NOT have semicolons at the end of the if and else if statements. It is extremely unlikely that yours should have them.
Is there ever a likely (and this is a real question) time to have them?
system
July 23, 2019, 2:07pm
5
Is there ever a likely (and this is a real question) time to have them?
Theoretically, ending an if statement with a semicolon is meaningful. In practice, I can't think of a single realistic situation where I would end an if statement with a semicolon.
While statements, yes. If statements, no.
But, even in the case of a while statement, I'd put the semicolon in curly braces, to show that I knew that there was nothing to do:
while(Serial.available() == 0)
{
; // just waiting for some serial data to arrive
}
what should I write about the analogue signal of the sensor so tthat it can be operate when one button is high or both buttons high.
system
July 23, 2019, 2:40pm
7
You want the sensor to read when button a is high, or button b is high, or both buttons are high?
There's no point checking for both, since either one being high as part of both being high, will already have passed the test for either being high.
An OR is actually an "inclusive OR" which covers the case of both. (As distinct from an eXclusiveOR which specifically excludes the both case.)
yes,what I confused about is how to write the analogue signal (IRvalue ) after
if...…..
system
July 23, 2019, 3:02pm
9
Don't you just need to move the analogRead() that you already have, into the if() that you already have? (Although you can leave the 3rd part off the if())
(Or maybe I'm not sure what you're asking?)