I have a sensor that oscillates between 2 values.
I would like to use a function that gives a true or false result
depending on whether the result is between these 2 values.
For example if result is between 875 and 894 then result = true.
Boolean result = false
If (read sensorPin >= whateverNumber) && (less than whateverNumber2)
Result =true
Pseudocode with entirely incorrect syntax.
bool isInRange(int value)
{
if(value >= 875 && value <= 894) {
return true;
}
return false;
}
@LightuC Please explain your code to the OP, while experienced programmers can easily follow it, the OP may not (evident in the need to ask this question).
bool result = (value >= 875) && (value <= 894);
It basically translates exactly to the sentence the op was asking:
That can be simplified to:
bool isInRange(int value)
{
return (value >= 875 && value <= 894);
}
Many thanks to all.
Here is my code it doesn't work.
The relay doesn't trip in the meantime
what's wrong
t/*
reading a voltage
*/
int relais = 9;
int captPin = A0;
int Vbatterie = 0;
void setup() {
pinMode (relais, OUTPUT);
pinMode (captPin, INPUT);
}
void loop() {
int Vbatterie = analogRead(captPin);// We read a voltage
bool result = (Vbatterie >= 875) && (Vbatterie <= 894);
if (result=true) {digitalWrite (relais,HIGH);}
delay(2000);
}ype or paste code here
Should be
if(result==true)
Also 't'at beginning and 'ype code here' at end are wrong...
Thanks build-1971 ...a real rookie mistake
It's me again. I fixed the error but when running I now get the message :
' expected initializer before '==' token'.
what's wrong again
/*
reading a voltage
*/
int relais = 9;
int captPin = A0;
int Vbatterie = 0;
bool result = false;
void setup() {
pinMode (relais, OUTPUT);
pinMode (captPin, INPUT);
}
void loop() {
int Vbatterie = analogRead(captPin);// We read a voltage
bool result == (Vbatterie >= 875) && (Vbatterie <= 894);
if (result=true) {digitalWrite (relais,HIGH);}
delay(2000);
}
Remove bool
Compiler thinks you are trying to declare it twice
I managed to correct it's ok.
/*
reading a voltage
*/
int relais = 9;
int captPin = A0;
int Vbatterie = 0;
bool result = false;
void setup() {
pinMode (relais, OUTPUT);
pinMode (captPin, INPUT);
}
void loop() {
int Vbatterie = analogRead(captPin);// We read a voltage
if ( result == (Vbatterie >= 875) && (Vbatterie <= 894));
{digitalWrite (relais,HIGH);}
delay(2000);
}
This may not do what you want. First, the variable 'result' is now completely unnecessary. Next, by not using parentheses properly, you have changed the meaning of the expression. Operator precedence is important: that's why we use parentheses to make it clear which part of a logic expression should be evaluated first.
You should at least try to understand the code changes people tell you to make and not just blindly implement them. Not trying to be a jerk, but it's frustrating to watch.
The LoC I think you're trying to implement is
if ( (Vbatterie >= 875) && (Vbatterie <= 894) )
{
digitalWrite (relais,HIGH);
}
I can't tell if the delay is supposed to be a part of this logic or not. Good luck!
Thank you cedarlakeinstruments,
I modified the program but the result of the operation is still wrong despite Vbatterie being well within the interval.
I don't understand.
(delay is just to have time to observe the change)
/*
reading a voltage
*/
int relais = 9;
int captPin = A0;
int Vbatterie = 0;
void setup()
{
pinMode (relais, OUTPUT);
pinMode (captPin, INPUT);
}
void loop() {
int Vbatterie = analogRead(captPin);// We read a voltage
if ( (Vbatterie >= 875) && (Vbatterie <= 963) );
{digitalWrite (relais,HIGH);}
delay(2000);
}
semicolon at the end of the line is superfluous.
You definitely need to improve your knowledge of syntax, otherwise you will hang on every line
@grandiosos Try:
void loop() {
int Vbatterie = analogRead(captPin);// We read a voltage
bool result = ((Vbatterie >= 875) && (Vbatterie <= 894));
if (result == true) {
digitalWrite (relais,HIGH);
}
delay(2000);
}
Please observe syntax for the compare in the if statement.
Sigh. Let's keep this simple. Try:
/*
reading a voltage
*/
int relais = 9;
int captPin = A0;
int Vbatterie = 0;
void setup() {
pinMode (relais, OUTPUT);
// pinMode (captPin, INPUT); mode set not required for analog input
}
void loop() {
int Vbatterie = analogRead(captPin); // We read a voltage
if ( Vbatterie >= 875 and Vbatterie <= 963 ) {
digitalWrite (relais, HIGH);
}
else {
digitalWrite (relais, LOW);
}
delay(2000);
}
Thanks aarg,
Yes it works !