Variable On For a Few Seconds With Pause and Loop

Guys,

I have a e cigarette, hooked up to a fish pump and a 3.7v lipo battery.

Think small portable Smoke Machine.

I want to hook it up to an Audrino and it trigger (turn on) for variable 3-5 seconds, pause for 10-15 seconds and then turn on again for 3-5 seconds. Loop forever.

Which code would you recommend?

Thank you,

GR

None - smoking is bad for everyone :)

can you show code & diagram on how you would turn on and off your fish pump?

then once you get there, have a look at the "blink" or "blink without delay" example to understand how to repeat something forever

It doesn't make a lot of sense to talk about turning a variable on or off. Variables are like Mr. Spock - completely immune to sweet talk.

Turning pins on or off is a different story.

PaulS: It doesn't make a lot of sense to talk about turning a variable on or off. Variables are like Mr. Spock - completely immune to sweet talk.

Turning pins on or off is a different story.

I think what OP meant with for variable 3-5 seconds the basic english language meaning (i.e. want to be able to set the duration between 3 and 5 seconds) not a programing code variable ;)

Guys,

By variable I mean duration.

So duration of 3-5 seconds, wait 10-15 seconds duration and loop.

I have no code besides simple LED blink code.

Not sure how to code 3-5 second duration or 10-15 second wait duration.

Here is the link to the Fog Machine.

http://valkyriestudios.net/making-a-mini-fog-machine/

Thanks,

Here is what I have found. Or is there a better code??

/*

  • Random On/Off for (Smoke Machine)
    *Connect to ground & digital pin 4 (smokePin)
    */

int smokePin = 4; // digital pin 4
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time

void setup() // run once, when the sketch starts
{
randomSeed (analogRead (0)); // randomize
pinMode(smokePin, OUTPUT); // sets the digital pin as output
}

void loop() // run over and over again
{
randOn = random (3000, 5000); // generate ON time between 3 and 5 seconds
randOff = random (10000, 15000); // generate OFF time between 10 and 15 seconds
digitalWrite(smokePin, HIGH); // sets the SMOKE on
delay(randOn); // waits for a random time while ON
digitalWrite(smokePin, LOW); // sets the SMOKE off
delay(randOff); // waits for a random time while OFF
}

That will work if you don't want the Arduino to do anything else because using delay() will prevent that.

What else might you want it to do ? Well, perhaps you want to add a software on/off pushbutton or pushbuttons to make the periods longer or shorter whilst running or a pushbutton to turn continuous smoke on and off, etc, etc

If you need such functions then there are alternative timing techniques using millis() that will let you implement them and operate immediately without waiting for a delay() to complete.

long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time

Why don't time variables have Time in the name? Wouldn't onTime and offTIme make more sense? That the values are randomly generated is irrelevant, don't you think?

You don't need longs to hold values up to 15000.

 randOn = random (3000, 5000);    // generate ON time between 3 and 5 seconds

Well, actually, between 3.0000 and 4.9999 seconds...

a duration is always positive, instead of long, go for unsigned long (or unsigned int if short enough)

Guys,

Took suggestions. The Audrino is just going to be running the pump, no buttons. Will add a power switch for the battery. Then mount in my Halloween costume.

I took out long, and changed text.

@J-M-L , I have no idea about long vs unsigned????

Am I missing anything else??

/*
* Random On/Off for  (Smoke Machine)
*Connect to ground & digital pin 4 (smokePin)
*Contributions by @J-M-L, PaulS, UKHeliBob
*/
const byte smokePin = 4; // Digital Pin 4 of the Arduino needs to connect to power of fish pump
unsigned long onTime = 0;                  // variable for the ON time
unsigned long offTime = 0;                 // variable for the OFF time


void setup()                      // run once, when the sketch starts
{
randomSeed (analogRead (0));    // randomize
pinMode(smokePin, OUTPUT);        // sets the digital pin as output
}

void loop()                       // run over and over again
{
onTime = random (3000, 5000);    // generate ON time between 3 and 5 seconds
offTime = random (10000, 15000);    // generate OFF time between 10 and 15 seconds
  digitalWrite(smokePin, HIGH);   // sets the SMOKE on
  delay(onTime);                // waits for a random time while ON
  digitalWrite(smokePin, LOW);    // sets the SMOKE off
  delay(offTime);               // waits for a random time while OFF
}

@J-M-L , I have no idea about long vs unsigned????

it's all documented :)

--> long

--> unsigned long

with 5 posts now you are a veteran, Please correct your post above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

I would change the type of your pin variableint smokePin = 4; // digital pin 4intoconst byte smokePin = 4; // Digital Pin 4 of the Arduino needs to connect to ....and if you add comments, make them meaningful...

J-M-L,

Thanks.

I corrected the code in Post #9.

Now to buy another Audrino...

Great

Guys,

My Audrino arrived.

The code that we helped created is below:

/*
* Random On/Off for  (Smoke Machine)
*Connect to ground & digital pin 4 (smokePin)
*Contributions by @J-M-L, PaulS, UKHeliBob
*/
const byte smokePin = 4; // Digital Pin 4 of the Arduino needs to connect to power of fish pump
unsigned long onTime = 0;                  // variable for the ON time
unsigned long offTime = 0;                 // variable for the OFF time


void setup()                      // run once, when the sketch starts
{
randomSeed (analogRead (0));    // randomize
pinMode(smokePin, OUTPUT);        // sets the digital pin as output
}

void loop()                       // run over and over again
{
onTime = random (3000, 5000);    // generate ON time between 3 and 5 seconds
offTime = random (10000, 15000);    // generate OFF time between 10 and 15 seconds
  digitalWrite(smokePin, HIGH);   // sets the SMOKE on
  delay(onTime);                // waits for a random time while ON
  digitalWrite(smokePin, LOW);    // sets the SMOKE off
  delay(offTime);               // waits for a random time while OFF
}

But then I realized that I could hook up the mini air pump and the clearomizer to the Audrino.

So I updated the code below

/*
* Random On/Off for  (Smoke Machine)
*digital pin 4 (smokePin) is the Kanger T2 clearomizer
*digital pin 5 (airpump) is mini air pump
*Contributions by @J-M-L, PaulS, UKHeliBob
*/
const byte smokePin = 4; // Digital Pin 4 of the Arduino needs to connect to the clearomizer
const byte airpumpPin = 5; // Digital Pin 5 of the Arduino needs to connect to the airpump

unsigned long onTime = 0;                  // variable for the ON time
unsigned long offTime = 0;                 // variable for the OFF time


void setup()                      // run once, when the sketch starts
{
randomSeed (analogRead (0));    // randomize
pinMode(smokePin, OUTPUT);        // sets the digital pin as output
pinMode(airpumpPin, OUTPUT);        // sets the digital pin as output
}

void loop()                       // run over and over again
{
onTime = random (3000, 5000);    // generate ON time between 3 and 5 seconds
offTime = random (10000, 15000);    // generate OFF time between 10 and 15 seconds
  digitalWrite(smokePin, HIGH);   // sets the SMOKE on
  delay(onTime);                // waits for a random time while ON
  digitalWrite(smokePin, LOW);    // sets the SMOKE off
  delay(offTime);               // waits for a random time while OFF
}

I added airpumpPin 5. But I am stuck on how to random airpumpPin & smokePin together.

Power has to be sent to the Airpump and Smoke (clearomizer) at the same time. Then both Pin 4 & Pin 5 have the same random time.

Need some help.

Thank you

Power has to be sent to the Airpump and Smoke (clearomizer) at the same time. Then both Pin 4 & Pin 5 have the same random time.

So, what IS the problem? You choose a time to turn both pins off, and then turn both pins on. Periodically, you see if it is time to turn both pins off. It's really no different from dealing with a single pin.

if you want different random times for each then you hit the limit of this simplistic design - it is time you go Read again #6

@PaulS

I am not sure how to add airpumpPin to the section below:

void loop()                       // run over and over again
{
onTime = random (3000, 5000);    // generate ON time between 3 and 5 seconds
offTime = random (10000, 15000);    // generate OFF time between 10 and 15 seconds
  digitalWrite(smokePin, HIGH);   // sets the SMOKE on
  delay(onTime);                // waits for a random time while ON
  digitalWrite(smokePin, LOW);    // sets the SMOKE off
  delay(offTime);               // waits for a random time while OFF
}

Guys,

I have another Noobie question.

I am using a 3.3v Arduino.

I checked the digital Pin output with a volt meter and I am getting 3.2v.

But when I connect a 3V mini fish pump, it is not turning on.

But when i directly connect a 3.7v lipo battery to the mini pump it works.

Then when I connect the mini pump to VCC, the pump works.

The mini pump specs:
Rated voltage: DC3V
Operating voltage range: DC2V-3.2V
Rated current: <150mA

Cartomizer:
2ohms

What am I doing wrong?

But I am stuck on how to random airpumpPin & smokePin together.

Power has to be sent to the Airpump and Smoke (clearomizer) at the same time. Then both Pin 4 & Pin 5 have the same random time.

  digitalWrite(smokePin, HIGH);   // sets the SMOKE on
  delay(onTime);                // waits for a random time while ON
  digitalWrite(smokePin, LOW);    // sets the SMOKE off

This section of code turns the smoke on and off. So what is so hard about writing to a second pin at the same time as the smoke pin, or at least immediately after it ?

As to your other question

The mini pump specs:
Rated voltage: DC3V
Operating voltage range: DC2V-3.2V
Rated current: <150mA

Rated current: <150mA. How much less than 150mA ? An Arduino pin can supply only 20mA if I remember correctly. You may have blown up at least one pin output when you tried to power the pump from it.

@UKHeliBob

I have very little experience with Audrino code.

With the code below, will the two pins receive the same value?

digitalWrite(smokePin, HIGH);   // sets the SMOKE on
  delay(onTime);                // waits for a random time while ON
  digitalWrite(smokePin, LOW);    // sets the SMOKE off

digitalWrite(airpumpPin, HIGH);   // sets the SMOKE on
  delay(onTime);                // waits for a random time while ON
  digitalWrite(airpumpPin, LOW);    // sets the SMOKE off

Also I read about the 20mA of a pin.

Then I hooked up a N Mosfet to get it to work.