Vent

http://stackoverflow.com/questions/43311823/arduino-uno-variables-not-being-tested-against-in-if-statement

I am doing a school project, if you could help at all please click on the link. The code isn't working perfectly. Thanks

if you could help at all please click on the link.

Why? If you want help HERE, post your code HERE AND tell us HERE what the code does, and how that differs from what you expect.

  if (opened != true and temperature >= 26) {

Add parentthesis; I've got 20+ years of C programming experience, and I wouldn't be sure of the relative precedence of "!=" vs "and"

Speaking of which: "and" ?? Are you sure that does what you think it does? Don't you want "&&"

westfw: Speaking of which: "and" ?? Are you sure that does what you think it does? Don't you want "&&"

http://en.cppreference.com/w/cpp/language/operator_alternative

barry2803:
The code isn’t working perfectly.

Could you tell us what’s wrong? “It isn’t working” is the worst way to ask a question! :slight_smile:

I agree that you should post your code here. And if you redirect us, please make it a clickable link so we don’t have to copy and paste.

But OK

if ([b]opened != true[/b] and temperature >= 26) {
if ([b]!opened[/b] and temperature < 26) {

The first one has the condition that opened must be false. The second one has that same condition worded in a different way. So why bother?

I have written some code for a project that sets a motor running in one direction for 6 seconds when the temperature is >= 26 degC to open a vent. when its below 26 degC the motor should run the other direction for 6 seconds to close the vent. After the vent has been opened, the motor should not be able to run that direction again to open it, it should only be able to close after it is opened, not open further. Same goes for the closing. The problem I’m having with my code is the opened function is being set in the code to essentially tell the code that the vent is open, do not try open again. However, when I set up some debug lines, I see the motor still spinning and opened is set to either true/false, depending on the temperature, but its always the wrong value, its set to be opened when the motor has closed the vent, and set to be closed when in face the vent is opened.

Thanks for all the help so far, here is the code as requested. Thanks

#include <Thermistor.h> //Taken from http://garagelab.com/profiles/blogs/tutorial-using-ntc-thermistors-with-arduino

#define M1A 4
#define M1B 5

bool opened = false;

Thermistor temp(0);
void setup() {
Serial.begin(9600);
}
void loop() {
int temperature = temp.getTemp();
Serial.print(temperature);
Serial.println("*C");

// OPENING///////////////
if (opened != true and temperature >= 26) {
digitalWrite(M1A, 1);
analogWrite(M1B, 0);
delay(6000);
digitalWrite(M1A, 0);
analogWrite(M1B, 0);
opened = true;
}

// CLOSING///////////////
if (!opened and temperature < 26) {
digitalWrite(M1A, 0);
analogWrite(M1B, 255);
delay(6000);
digitalWrite(M1A, 0);
analogWrite(M1B, 0);
opened = false;
}

delay(1000);
Serial.println(opened);
}

Use code tags; you managed to display the code properly on stackoverflow, why not here?

Hi,

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)

Hi,

if (opened != true and temperature >= 26)
could be
if (opened = false && temperature >= 26)
//======
 if (!opened and temperature < 26)
could be 
 if (opened == true && temperature < 26)

Just looks more logical and easier to comprehend.

Tom… :slight_smile:

You initialize "opened" to false, so before you start the program you should physically set the vent according to the current temperature, you need limit switches. Also, what happens if the temperature blinks 26, 25, 26, while the six second delay is blocking? You should have some hysteresis (dead band).