Go Down

Topic: (SOLVED)n-mosfet powered heating element, need help with a timer. (Read 754 times) previous topic - next topic

copperking

I'm sorry and I realized this isn't the place to post this question, it goes into programming questions but maybe someone can help me, I am currently developing an electronic cigarette that will have a lot of bells and whistles, like capacitive touch screen with ohm readings for heating element- current- amps- puff counter- volts on display, potentiometer power control (to later be turned into digital pot), Angel Eye LED momentary switches, etc. (I'm sure you get the point). Right now, my problem is when you press the pushbutton it will fire the heating element, but there isn't a way to shut it off automatically, if, for example, the button was pressed in a pocket accidentally. I have looked for a way to do this in the forums but to no avail. either I am unable to understand the code or it isn't applicable to my design, I've looked all the timer examples off of github and on the examples provided in the ide. Any help would be greatly appreciated. I need the heating element to auto-cutoff after about 6-8 seconds of button being pressed and not come back on until button is released and pressed again, and I cant have it auto-run, so to speak either. Here is the code:



Code: [Select]

// INPUT:
const int pot = A0; // Assigns the analog pin for the potentiometer.
const int angelEye = A15; // Assigns the fire switch for the fire swtich that controls the power to the heating element (aka RDA).
// OUTPUT:
const int angelEyeLed = 2; // Assigns the LED for the angel eye.
const int power = 11; // Assigns power to n-channel MOSFET that controls the RDA.
// SMOOTHING:
const int numReadings = 10;
int readings[numReadings];  // The readings from the input.
int readIndex = 0; // The index of the current reading.
int total = 0; // The running total.
int average = 0; // The average.
// VARIABLES:
int potValue = analogRead(pot); // variable for the value of the potentiometer.
int angelEyeValue = analogRead(angelEye); // variable for the value of the state of the fire switch.
int ledValue = digitalRead(angelEyeLed); // unassigned value.
// ANGEL EYE LED FADING:
int brightness = 0;
int fadeAmount = 15;
//DEVELOPMENT IN PROGRESS
bool onOff = 0;

void setup() {
pinMode(power, OUTPUT); //Assigns the power pin (pin 5) as an OUTPUT.
pinMode(angelEyeLed, OUTPUT); //Assigns the Angel Eye LED (pin 40) as an OUTPUT.
pinMode(53, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(115200); // Starts Serial diagnostics for value evaluation.
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;}

 
void loop() {
//TURN OFF LED13 ON BOARD
digitalWrite (13, LOW);
//VALUE DESIGNATIONS:
potValue = analogRead(pot);           // Don't know why or how but these next three lines are needed to fire RDA and run LED.
angelEyeValue = analogRead(angelEye); //
ledValue = digitalRead(angelEyeLed);  //


float voltage= average * (8.2 / 1023.0); // Converts the potentiometer value to a voltage value to determine approximate power output
                                          // (Assuming you have two 18650 batteries connected in series).
//float voltage= potValue * (4.2 / 1023.0); //Enable this value and disable above value for 18650 batteries connected in parallel ( or one 18650)
//angelEyeValue = analogRead(angelEye); //Assigns a value according to status of Angel Eye switch.
//potValue = analogRead(pot); // Assigns value according to the position of the potentiometer.
//ledValue = digitalRead (angelEyeLed); //Assigns a value according to the brightness of the Angel Eye LED.
//DIAGNOSTICS:
Serial.println(average);       // Next three lines are for diagnostics.
Serial.println(angelEyeValue); //
Serial.println(voltage);       //
//ANGEL EYE LED SECTION:
analogWrite(angelEyeLed, brightness); // Angel Eye LED brightness assignment.
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {fadeAmount = -fadeAmount;} //Fading the LED up and down.
//else {if (angelEyeValue >= 550) {analogWrite(angelEyeLed, 195);};}; //Make the LED stay lit
if (average >= 0 && average <= 200) {delay(140);};    // Next ten lines are used for fading frequency (speed) of the LED according to amount of power from POT.
if (average >= 200 && average <= 300) {delay(125);};   
if (average >= 300 && average <= 400) {delay(110);};   
if (average >= 400 && average <= 500) {delay(95);};   
if (average >= 500 && average <= 600) {delay(80);};   
if (average >= 600 && average <= 700) {delay(65);};
if (average >= 700 && average <= 800) {delay(50);};
if (average >= 800 && average <= 900) {delay(35);}
if (average >= 900 && average <= 1000) {delay(20);};   
if (average >= 1000 && average <= 1023) {delay(10);};   
// ANGEL EYE SWITCH SECTION:
digitalWrite(53, HIGH); // used for 5 volt power to the Angel eye pushbutton.
if (angelEyeValue >= 550) {analogWrite (power, average/4);} else {analogWrite(power, 0);}; // pwm control to the RDA.
if (angelEyeValue >= 550) {onOff = true;} // was trying to use this
  else (onOff = false);                   // somehow for AUTO-FIRE SAFETY.
Serial.println (onOff);
// POTENTIOMETER SMOOTHING:
total = total - readings[readIndex];
readings[readIndex] = analogRead(pot);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings)
readIndex = 0;
average = total / numReadings;
delay(1);

if (potValue <= 15) {digitalWrite(53, LOW);} else {digitalWrite(53, HIGH);}
}



MorganS

Code: (Don't do this) [Select]
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;}

Please run the auto formatter (on the tools menu) to fix this up. It will seem like it makes your sketch take up a lot more space on the screen but whitespace is very important to be able to read and understand the code.

Code: (Also, don't do this) [Select]
if (average >= 200 && average <= 300) {delay(125);};   
if (average >= 300 && average <= 400) {delay(110);};   
if (average >= 400 && average <= 500) {delay(95);};   
if (average >= 500 && average <= 600) {delay(80);};   
if (average >= 600 && average <= 700) {delay(65);};
if (average >= 700 && average <= 800) {delay(50);};
if (average >= 800 && average <= 900) {delay(35);}
if (average >= 900 && average <= 1000) {delay(20);};   
if (average >= 1000 && average <= 1023) {delay(10);}; 

This kind of variable delay in the middle of your code is going to make everything else more difficult. If you want to fade an LED, just make sure it's on a PWM pin and use analogWrite() to set the brightness.

So, to answer the original question, you need to work out when the button was first pressed and record that time. Use millis() to get the time. Then every time through your loop, check the current time. Follow the examples given in http://www.gammon.com.au/millis
"The problem is in the code you didn't post."

copperking

Thank you so much for the link about millis()! that helped a lot!
Quote
This kind of variable delay in the middle of your code is going to make everything else more difficult. If you want to fade an LED, just make sure it's on a PWM pin and use analogWrite() to set the brightness.
I will try that, by changing the int fadeAmount instead of delaying the whole program. I am pretty sure there is a better way to delay the speed at which the LED gets bright and dim, I'm still in the beginnings of figuring out Arduino programming. I've bought books on C++ and studying them, I've had Arduino for 1 1/2 weeks now and love it! Thanks again for the help.

Go Up