The goal of my project is to turn the DC motor on for a few seconds as long as the temperature is above 75 degrees F. My question is how do I get the motor to do that? Right now my loop is running repeatedly and the motor constantly turns on and off when the temperature is greater than 75. How do I get the motor to only turn on for a few seconds and then turn off when the temperature is greater than 75 degrees?
const int temperaturePin = 0;
const int motorPin = 9;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
}
float getVoltage(int pin)
{
return (analogRead(pin) * 0.004882814);
}
void loop() {
//void motorOnThenOff();
int speed;
{float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin);
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0/5.0) + 32.0;
if (degreesF > 75)
{
digitalWrite(motorPin, HIGH);
delay(1000);
digitalWrite(motorPin, LOW);
}
Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.print(degreesC);
Serial.print(" deg F: ");
Serial.println(degreesF);
delay(500);
}
}
/code]
Let's make sure we know what you want.
If the temp is below 75, do not run the motor.
If the team is above 75, run the motor for a few seconds and never run again.
Is this right?
Or do you want some time to elapse and the check the temp again, if above 75, run the motor again?
Yes that is correct. Run the motor once for a few seconds once the temp is above 75 and then never again.
756E6C
April 12, 2017, 4:00am
4
const int temperaturePin = 0;
const int motorPin = 9;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
//void motorOnThenOff();
int speed;
{ float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin);
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0 / 5.0) + 32.0;
if (degreesF > 75.0)
{
digitalWrite(motorPin, HIGH);
delay(1000); // a few seconds ?
digitalWrite(motorPin, LOW);
}
Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.print(degreesC);
Serial.print(" deg F: ");
Serial.println(degreesF);
delay(500);
}
}
void loop() {
}
float getVoltage(int pin)
{
return (analogRead(pin) * 0.004882814);
}
That code works but it only reads the temperature one time. I need the temperature to be read every few seconds, and when it is over 75, I need the motor to turn on for a moment.
Run the motor once for a few seconds once the temp is above 75 and then never again.
I need to constantly read the temperature in order to know when to run the motor.
MrGingerAvenger:
Run the motor once for a few seconds once the temp is above 75 and then never again.
I need to constantly read the temperature in order to know when to run the motor.
In other words, you want an electronic thermometer with a display. And a switch to turn on a fan one time if temperature is > 75. And then the switch can burn up!
What voltage and current does the fan require? what are you going to power the Arduino?
Paul
I am not sure what it requires, it is the motor that comes with the starter pack for arduino. I will be using a 9V battery to power it.
Hi,
I think you are looking for.
Read temperature at fixed intervals, if at anytime the temperature goes up through 75DegF run the motor for 2 Seconds.
So it will only activate the motor ONCE but everytime the temperature is increasing through 75DegC.
I think thats what the OP wants.
Tom...
I thought maybe I fixed it with the following code, but this doesn't work either.
const int temperaturePin = 0;
const int motorPin = 9;
int initial= 0;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
}
float getVoltage(int pin)
{
return (analogRead(pin) * 0.004882814);
}
void loop() {
//void motorOnThenOff();
int speed;
{float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin);
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0/5.0) + 32.0;
if (initial = 0)
if (degreesF > 75)
{
digitalWrite(motorPin, HIGH);
delay(1000);
digitalWrite(motorPin, LOW);
initial=1;
}
Serial.print(" deg F: ");
Serial.println(degreesF);
delay(1000);
}
}/code]
Hi,
What you need to do is compare the temperature with the previously read temp
pseudo code;
newtemp = readtemp
oldtemp < 75 and newtemp >75
TRUE = then run motor for 2Seconds
FALSE = then do nothing
oldtemp = newtemp
loop back
Tom...
TomGeorge:
Hi,
What you need to do is compare the temperature with the previously read temp
pseudo code;
newtemp = readtemp
oldtemp < 75 and newtemp >75
TRUE = then run motor for 2Seconds
FALSE = then do nothing
oldtemp = newtemp
loop back
Tom...
I took your advice and made it to here, but unfortunately it still isn't working like I want it to. See the code bellow.
const int temperaturePin = 0;
const int motorPin = 9;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
}
float getVoltage(int pin)
{
return (analogRead(pin) * 0.004882814);
}
void loop() {
for(int x=0;x<3500;x++)
{
float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin);
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0/5.0) + 32.0;
int readtemp = degreesF;
int oldtemp = 0;
int newtemp = readtemp;
float array[3500];
array[x]=degreesF;
float fifth_value_array = array[4];
if ((fifth_value_array < 75) && (newtemp > 75))
{
digitalWrite(motorPin, HIGH);
delay(2000);
digitalWrite(motorPin, LOW);
}
Serial.print(" deg F: ");
Serial.println(degreesF);
delay(1000);
}
}
Hi,
Try this, you do not need an array.
const int temperaturePin = A0;
const int motorPin = 9;
const int indicatorPin = 13;
float voltage, degreesC, degreesF;
int oldtemp = 0;
int newtemp = 0;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(indicatorPin, OUTPUT);
digitalWrite(indicatorPin, LOW);
}
void loop() {
readtemp();
newtemp = degreesF;
if (newtemp > 75 && oldtemp < 75)
{
digitalWrite(motorPin, HIGH); // if temperature is >75 and was <75
digitalWrite(indicatorPin, HIGH);
delay(2000);
digitalWrite(motorPin, LOW);
digitalWrite(indicatorPin, LOW);
}
else
{
digitalWrite(motorPin, LOW); // therewise keep motor OFF
digitalWrite(indicatorPin, LOW);
}
Serial.print(" deg F: ");
Serial.println(degreesF);
oldtemp = newtemp;
delay(1000);
}
void readtemp()
{
voltage = analogRead(temperaturePin) * 0.004882814; // read temp voltage
degreesC = (voltage - 0.5) * 100.0; // convert to C
degreesF = degreesC * (9.0 / 5.0) + 32.0; // convert to F
}
I have used the on board LED on pin13 as a motor ON indicator.
Tom...
TomGeorge:
Hi,
Try this, you do not need an array.
const int temperaturePin = A0;
const int motorPin = 9;
const int indicatorPin = 13;
float voltage, degreesC, degreesF;
int oldtemp = 0;
int newtemp = 0;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(indicatorPin, OUTPUT);
digitalWrite(indicatorPin, LOW);
}
void loop() {
readtemp();
newtemp = degreesF;
if (newtemp > 75 && oldtemp < 75)
{
digitalWrite(motorPin, HIGH); // if temperature is >75 and was <75
digitalWrite(indicatorPin, HIGH);
delay(2000);
digitalWrite(motorPin, LOW);
digitalWrite(indicatorPin, LOW);
}
else
{
digitalWrite(motorPin, LOW); // therewise keep motor OFF
digitalWrite(indicatorPin, LOW);
}
Serial.print(" deg F: ");
Serial.println(degreesF);
oldtemp = newtemp;
delay(1000);
}
void readtemp()
{
voltage = analogRead(temperaturePin) * 0.004882814; // read temp voltage
degreesC = (voltage - 0.5) * 100.0; // convert to C
degreesF = degreesC * (9.0 / 5.0) + 32.0; // convert to F
}
I have used the on board LED on pin13 as a motor ON indicator.
Tom... :)
I tried this, but the motor isn't running at all when the temp is above 75, or at any other time.
Hi,
Does the LED on the UNO turn ON?
On the monitor can you see the Temperature changing?
Tom...
Hi,
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,
Why you do not turn the fan ON when it is>75 and so you do not have to use a delay just turn off when less than <74. Maybe I am missing something.
Hi,
The OP only wants the fan ON for 2Seconds when the the temperature goes up through 75F.
Tom....
nyphot
April 23, 2017, 6:36am
20
There's enough help above on how to do what you want, but to me it doesn't make sense to do what you want, although of course it's your project not mine and you have your reasons
But I'm sure we're keen to know why you only turn the fan on for two seconds on the way up thru 75 and let it get hotter hotter after that if the short run of the fan didn't take it back below 75.