Currently I'm working with a heating element and a sharp IR sensor. Essentially the heating element is heating a page with thermochromic ink. Once the sensor is interacted with the heating element turns on. When I take away my hand from the sensor the heating element turns off. This is working fine for me. Essentially I'm now looking to integrate a fan into the circuit. I've purchased a 2 pin 5v fan online and I'm now looking to turn the heating element on for 30 seconds, once the heating element turns off I'm looking to turn on the fan for thirty seconds. The heating element is being powered by a 12v power supply via a tip122 transistor as a switch. I was thinking of using a similar circuit for the 5v fan and power it with a 3 AA batteries in series, which would be 4.5 volts. Although it's not 5 in theory it should still power it enough.
I suppose I'm wondering how to set up the code to turn on the fan once the heating element turns off after 30 seconds.
Here is the current state of the code for the heating element only, all help would be greatly appreciated on this, thanks folks
#include <SharpIR.h>
#define ir A5
#define model 1080
const int heatPin1 = 7; // the number of the LED pin
SharpIR sensor(GP2YA41SK0F, A5);
int SharpIR = A5;
boolean sharpIR = true;
void setup() {
// initialize the LED pin as an output:
pinMode(heatPin1, OUTPUT);
digitalWrite( heatPin1, LOW );
Serial.begin(9600);
}
void loop(){
delay(500);
unsigned long pepe1 = millis(); // takes the time before the loop on the library begins
int distance = sensor.getDistance(); // this returns the distance to the object you're measuring
Serial.print(" distance: "); // returns it to the serial monitor
Serial.println(distance);
unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(pepe2);
if ((distance < 7) && ( distance > 1)) {
digitalWrite(heatPin1, HIGH);
}
else
//delay(5000);// leave on for 5 seconds
{digitalWrite(heatPin1, LOW); // Turn Relay OFF
//delay(000);
}
}
//}
I will never understand why people write code this way. That is not how I think of the comparisons needed to determine if a value is in a range. I think in terms of being above the low end of the range first.
You KNOW where you turn the relay (that is NOT a relay) off. Add a delay(), turn the fan on, add another delay, then turn the fan off.
Or, look at the blink without delay example to see how to do things without using delay. A boolean variable, fanNeedsToRun, can be set to true when the fan needs to come on later. If the variable is true, and the appropriate amount of time has passed, turn the fan on. If the variable is true, and the appropriate amount of time has passed, turned the fan off AND set the variable to false.
Thank for you reply Paul, At the moment I'm just trying to get the heat pad to turn on for ten seconds and cut off. I need to get this working first before I go near the fan. I did what you asked with regards to adding the delay but it seems as if the heat pad stays on, even though I have set the delay for 10 seconds. Here is the current code
#include <SharpIR.h>
#define ir A5
#define model 1080
const int heatPin1 = 7; // the number of the LED pin
SharpIR sensor(GP2YA41SK0F, A5);
int SharpIR = A5;
boolean sharpIR = true;
void setup() {
// initialize the LED pin as an output:
pinMode(heatPin1, OUTPUT);
digitalWrite( heatPin1, LOW );
Serial.begin(9600);
}
void loop(){
//delay(500);
unsigned long pepe1 = millis(); // takes the time before the loop on the library begins
int distance = sensor.getDistance(); // this returns the distance to the object you're measuring
Serial.print(" distance: "); // returns it to the serial monitor
Serial.println(distance);
unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(pepe2);
if ((distance < 7) && ( distance > 1)) {
digitalWrite(heatPin1, HIGH);
}
else
{digitalWrite(heatPin1, LOW); // Turn Relay OFF
delay(10000);
}
}
//}
I know this a thread about software but your hardware choice for the fan will be problematic. The TIP122 is an ancient, 50 year old design part that will result in your fan only receiving about 3.3-3.4 volts. This is due to the high voltage drop of 1.2 volts across the C-E junction that occurs with a silicon Darlington transistor.
You can avoid the problem by using a modern logic level n channel mosfet like the FQP30N06L or RFP30N06LE (and there are hundreds more devices that are suitable). The mosfet voltage drop will be virtually zero with a fan load, the result being full battery voltage reaching the fan.
Thanks for getting back to me. I've made the changes to the code but it seems that the the heat pad isin't turning off after ten seconds. The longer I leave my hand in front of the sensor the hotter the pad becomes. Here's the code currently.
#include <SharpIR.h>
#define ir A5
#define model 1080
const int heatPin1 = 7; // the number of the LED pin
SharpIR sensor(GP2YA41SK0F, A5);
int SharpIR = A5;
boolean sharpIR = false;
void setup() {
// initialize the LED pin as an output:
pinMode(heatPin1, OUTPUT);
digitalWrite( heatPin1, LOW );
Serial.begin(9600);
}
void loop() {
unsigned long pepe1 = millis(); // takes the time before the loop on the library begins
int distance = sensor.getDistance(); // this returns the distance to the object you're measuring
Serial.print(" distance: "); // returns it to the serial monitor
Serial.println(distance);
//unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
//Serial.print("Time taken (ms): ");
//Serial.println(pepe2);
if ((distance < 7) && ( distance > 1)) {
digitalWrite(heatPin1, HIGH);
delay(10000);
}
else
{ digitalWrite(heatPin1, LOW); // Turn heat pad OFF
}
}
//}
You STILL have useless code.
You have excessive white space. I hate having to scroll so much to find code that should be right there.
You are STILL not turning the heater off at the end of the 10 seconds.
I really do NOT understand what you are trying to do. Specifically, why do you want to turn the heater on for 10 second blasts as long as the distance that the sensor sees is between 1 and 7 somethings?
I'm looking to turn the pad on for ten seconds to reveal an image underneath the thermo ink. Then I want the heat pad to cool down as quick as possible and return the thermo ink back to its original state.
This is why I was going to integrate the fan into the circuit to cool it quicker.
I feel at the moment as long as my hand is in font of the sensor the heat pad stay on. Essentially I'm looking to switch the circuit once my hand interacts with the sensor, even if I keep my hand in front of the sensor I want the circuit to turn on for 10 seconds only.
I feel at the moment as long as my hand is in font of the sensor the heat pad stay on.
It does. If that is not what you want, you need to change the code.
If you want the CHANGE to the distance being in the range 1 to 7 to control the heater, rather than when the distance IS between 1 and 7, something like this:
bool needToHeat = true;
void loop()
{
int distance = sensor.getDistance(); // No useless comments needed
if(distance > 1 && distance < 7)
{
if(needToHeat)
{
digitalWrite(heatPin1, HIGH); // Turn the heater on
delay(10000);
digitalWrite(heatPin1, LOW); // Turn the heater off
needToHeat = false;
}
}
else
{
needToHeat = true;
}
}
Notice that there is no unneeded code and no excessive white space and that there is NOTHING else on any line with a { or a }.
Yes, Tom that is exactly what I'm trying to achieve but I have integrated the 5v fan as yet. I'm a little unsure as to how to do this. I would presume you would switch the fan with a smiliar circuit in parallel via a tip122 running off 5v as it's a 5v fan?