help with code pulse

Hey guys I can't seem to find out how to write the code for a pulse square wave. I can use two pins or just one. What I'm tring to do is make a 6v fan drive high(6v)1000rpm and drop to a background (3v)500rpm at a given rate. I would like to make the background and frequency adjustable. I have tried many code combinations and the code situation has become to resemble a hectic mess lol. I have a optocoupler to keep the 6vcc out of the arduino. Thanks guys

ps I'll have the code Ive been working posted later tonight when I get off work.

Give model (and link?) of the fan.

Weedpharma

read blinkwithoutdelay in the examples
read about millis() and micros()

harrelle:
how to write the code for a pulse square wave.

analogWrite(motorPin, nnn); where nnn is a value from 0 to 255 representing the required duty cycle ?

Or have I missed something?

...R

ok guys try not to spit up your drink in laughter

const int mainamp = 9;
const int background = 11;
const int bkgrdampsetting = A0;     // potentiometer
const int pulse = A1;                     // potentiometer
long pulsevalue = 0;
int bkgrdampoutput = 0;
unsigned long previousMillis = 0;


void setup() {

Serial.begin(9600);
pinMode(mainamp, OUTPUT);
pinMode(background, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {

unsigned long currentMillis = millis();
bkgrdampoutput = analogRead(bkgrdampsetting);
pulsevalue = analogRead(pulse);
bkgrdampoutput = map(bkgrdampsetting, 0, 1023, 0, 255);
pulsevalue = map(pulse, 0, 1023, 0, 1000);

if(currentMillis - previousMillis >= pulsevalue) {
  previousMillis = currentMillis;   
  
  if (mainamp == LOW)
    mainamp = HIGH;
  else
    mainamp = LOW;
   
   digitalWrite(mainamp,HIGH) 
  if (mainamp == LOW)
    background = bkgrdampoutput;
   else 
     background = LOW;
     
     
  



// print out the value you read:

Serial.print("pulse time = " );
Serial.print(pulsevalue);
Serial.print("\t background volts = " );
Serial.println(bkgrdampoutput);

 }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

voltage_control.ino (1.33 KB)

harrelle:
ok guys try not to spit up your drink in laughter

Nobody is laughing, but I am a bit perplexed ...

Did you read Reply #3
Is there some reason why it is not relevant ?

Why do you need to change the frequency ?

You have not told us what your program actually does and what you would like it to do.

...R

yes I have read reply #3

It's a project to push air/smoke in various adjustable patterns for effects. Like Morse code or smoke rings ect. I don't have the fan yet I'm just trying the code out to see if I can get it to work on a led lol

harrelle:
yes I have read reply #3

But you still could not be bothered to answer my questions ? ?

Why should I bother to help ?

...R

Robin2
I don't know what your asking me to provide??? I've stated my goal, what I have already done and that have researched the code example you have listed

harrelle:
I don't know what your asking me to provide???

I asked 4 questions in Reply #5 and you have only answered the first one

The other three questions were

Is there some reason why it is not relevant ?

Why do you need to change the frequency ?

You have not told us what your program actually does and what you would like it to do

...R

Pulse width modulation orPWM. Is available. It is a square wave of various frequencies you pick.

Robin gave you the keys to that kingdom.

Duty cycle is the term for the pulse width.

harrelle:
yes I have read reply #3

It's a project to push air/smoke in various adjustable patterns for effects. Like Morse code or smoke rings ect.

Thanks for the help Robin 2

I'll proceed with this somewhere else ......

Hi,
If you read the OP project, he has a fan that needs to run at two speeds, slow and fast.
He is happy to have an output that is HIGH for HIGH speed and LOW for LOW speed.
He does not need PWM, the blink without delay is an ideal application as it will work at the very low cycle that it looks like he needs.
Changing the frequency and duty with two analog inputs, to make the fan speed modulate a smoke generator I assume.

Tom.... :slight_smile:

TomGeorge:
If you read the OP project, he has a fan that needs to run at two speeds, slow and fast. He is happy to have an output that is HIGH for HIGH speed and LOW for LOW speed.
He does not need PWM,

Full marks for figuring that out. Seriously.

...R