Conditional run on Controllable Speed DC Motor

Hello and good day,

This is my first time using a motor in my project, and I having problem with it.
It is basically a 12V DC motor that it speed can be controllable using a potentiometer.
But for it to run, the temperature sensor first need to reach a certain temperature, then the program will allow the motor to run.
I’ve tried running the motor program first, which is a success.
However, when I added a conditional statement to it, it wouldnt run at all.
Any help is much appreciated.
Thank you.

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int pwmPin = 12; // assigns pin 12 to variable pwm
int pot = A0; // assigns analog input A0 to variable pot
int c1 = 0;   // declares variable c1
int c2 = 0;   // declares variable c2

void setup()
{
  Serial.begin(9600);
  sensors.begin();

  pinMode(pwmPin, OUTPUT);
  pinMode(pot, INPUT);
}

float readSensorTemp() {
  sensors.requestTemperatures();
  Serial.print("Temperature: ");
  Serial.println(sensors.getTempCByIndex(0));
  return sensors.getTempCByIndex(0);
}

void loop()
{
  float Temp = readSensorTemp();
  if (Temp > 25.00) {
    c2 = analogRead(pot);
    c1 = 1024 - c2;
    digitalWrite(pwmPin, HIGH);
    delayMicroseconds(c1);
    digitalWrite(pwmPin, LOW);
    delayMicroseconds(c2);
  }
}

.

Try printing the value of Temp.

TheMemberFormerlyKnownAsAWOL:
Try printing the value of Temp.

The serial monitor shows something like this:

Temperature: 29.87

and it goes on indefinitely

I’d have to say that you’re not printing the value of Temp…

In readSensorTemp, read the sensor once .

How long does readSensorTemp take?
What frequency of PWM are you trying to generate?

Hi,
Try

 if(Temp>25.0){

Also do a CTRL-T to indent your code.

Tom..... :slight_smile:

Ah sorry, i misunderstood your instruction. i do print the value of Temp now.
It does updates it values as I put the sensor on heat source

TomGeorge:
Hi,
Try

 if(Temp>25.0){

Also do a CTRL-T to indent your code.

Tom..... :slight_smile:

Hi,

I do try to edit that part but it still doesn't work

What does your PWM frequency work out to? Which motor driver?

Printing is not instantaneous.
Measuring temperature is not instantaneous

JCA34F:
What does your PWM frequency work out to? Which motor driver?

I don't use any motor driver for that, my circuit is based on this guide: DC Motor Speed Control using Arduino and Potentiometer

I'm not sure about the PWM motor frequency, to be honest I'm not really knowledgeable on motor related stuff but only have general idea on how PWM works.

TheMemberFormerlyKnownAsAWOL:
Printing is not instantaneous.
Measuring temperature is not instantaneous

Sorry, I don't really get this.
But what I can understand, there will be 'lag' on reading and printing?
If so, I dont get how it could not make the code works, though since it will take the last value of Temp.
Since its already above the temperature, the motor should be able to run, right?
I'm pretty much beginner on this.

I’ve tried running the motor program first, which is a success.

Are you saying that something like this “works”?

void loop ()
{
  int c2 = analogRead(pot);
  int c1 = 1024 - c2;
  digitalWrite(pwmPin, HIGH);
  delayMicroseconds(c1);
  digitalWrite(pwmPin, LOW);
  delayMicroseconds(c2);
}

I’ve asked before, but I’ll try one more time; how long does readSensorTemp take?

TheMemberFormerlyKnownAsAWOL:
Are you saying that something like this “works”?

void loop ()

{
  int c2 = analogRead(pot);
  int c1 = 1024 - c2;
  digitalWrite(pwmPin, HIGH);
  delayMicroseconds(c1);
  digitalWrite(pwmPin, LOW);
  delayMicroseconds(c2);
}




I've asked before, but I'll try one more time; how long does readSensorTemp take?

I can control the speed, so to me it works before i added the conditional statement to it.
How do I check the duration?

fawked:
How do I check the duration?

Take the difference of the value returned by micros() before and after calling the function.
I forgot to mention earlier, by analogRead isn’t instantaneous either.

In simplistic terms your problem is that your home-grown PWM depends on loop() looping round really fast. So it goes HIGH/LOW - HIGH/LOW continually.

When you put the temperature check in it goes HIGH/LOW now hang about a bit while I read the temperature then HIGH/LOW hang about some more to read temperature again etc. So the LOW part is lasting A LOT LONGER than you think.

Time to learn how to use the hardware PWM via analogWrite() maybe?

Steve