0

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.

#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);
}

Thanks for anything you might have to say, drop a comment or something if you want to know more. Craig

  • Does it really build as is ? 'and' is not valid in C ... try with : if ( (opened != true) && (temperature >= 26)) – Mali Apr 10 '17 at 09:58
  • also, in the two differents if, you test if the vent is not open. if(opened != true) and if (!opened) are two different notations of the same logical test. It is also the same as if(opened == false) – Mali Apr 10 '17 at 10:01
  • Well I got it fixed, I just changed from the `#define`s to int consts and the `digitalWrite`/`analogWrite`s into `digitalWrite(M1A, HIGH)` Everything works perfect now. Thanks guys! – Craig Stratford Apr 10 '17 at 16:41

0 Answers0