My motor keeps running although they should run at temp > tempLimitmax

// Motor Pinout
int ena = 10;
int in1 = 6;
int in2 = 7;
int in3 = 8;
int in4 = 9;
int enb = 5;

int moisture;
int sensorinput;


float moistLimit = 463.5;
int tempLimitmax = 29;

void setup() {
  Serial.begin(9600);
  pinMode(ena, OUTPUT);
  pinMode(enb, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
}

void loop() {
  moisture = analogRead(A0);
  if (moisture >= moistLimit) {
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enb, 255);
  } else {
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    analogWrite(enb, 0);
  }

  sensorinput = analogRead(A1);
  float voltage = sensorinput * (5.0 / 1024.0);
  float temp;
  temp = (voltage - 0.5) * 100;

  if (temp > tempLimitmax) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    analogWrite(ena, 80);
  } else {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    analogWrite(ena, 0);
  }
}

Using 4 AA batteries to power Arduino and motor.

If you want a better diagram, I can make one more :+1:

Have you tried printing the value that you are testing to see whether it is what you expect ?

1 Like

Yes, I have printed temp.

So, what are the ACTUAL values you are getting from the sensors? And are they reasonable or not?

They are voltages converted to Celsius temperature, if youre asking the tmp 36 sensor.

I am asking about ALL your sensors that your are using in your program. Are the values what you expect? If not, then that is the FIRST problem to work on.

It is what I am expecting.

Mixing int and float will never end well. Change your tempLimitnax to a float with a decimal point.

I don't think this will solve the problem because I tried it before.

But it might fix the next problem!

Can you check my motor logic?

You don't you check it by using serial.Print() statements to follow your logic and show your variable values.

There is no purpose to check variables used for motor power.

Are they NOT part of your LOGIC?

Can you specify the question?

I guess the question is: Did you bother to actually check your own logic?

Yes, but in a very beginner way.

Then the advanced way to to add the print statements so you can follow, step by step, through your logic.

1 Like

I'll try that out. :+1: