pwm • compostion • loop for some period

Hi, okay I'm quite new with Arduino, but at the end of the week I would like to have this working. Not to difficult I think. Who can help me?

I want to make a pwm composition of 2 or more dc motors. After set a certain speed, I want Arduino to keep this setting running [looping] for some seconds. Then another speed starts looping. Then another speed starts looping. Then another speed starts looping.

How to write this down correctly?

Thanks a lot!

after you declare variables and all that, and set up the output pins in void setup, you can do something like this: startFrequency = 100; void loop(){ for (x=1 to 100){ digitalWrite (motorPin, HIGH); delay (startFrequency); digitalWrite (motorPin, LOW); next x; } startFrequency = startFrequency+50; if (startFrequency == 600){ startFrequency = 100; } }

Thanks for the reply!
I understand the whole pwm thing,
but

What does the startFrequency mean and how do I declare it?
and what does ‘next x’ mean?
Should I make several of these to make different speedloops?

and how exactly does 1 setting loop for some time in this way? and can I skip easy to other settings?

what i have now

#define GREEN 13 // LED connected to
// digital pin 13
#define BLUE 12
void setup()
{

pinMode(GREEN, OUTPUT); 
pinMode(BLUE, OUTPUT);
startFrequency = 100;
}
void loop()
{
for (x=1 to 100){
digitalWrite (GREEN, HIGH);
delay (startFrequency);
digitalWrite (GREEN, LOW);
next x;
}
startFrequency = startFrequency+50;
)if (startFrequency == 600) {
  startFrequency = 100;
}
}

and these are the errors

sketch_oct04b.cpp: In function ‘void setup()’:
sketch_oct04b:9: error: ‘startFrequency’ was not declared in this scope
sketch_oct04b.cpp: In function ‘void loop()’:
sketch_oct04b:13: error: ‘x’ was not declared in this scope
sketch_oct04b:13: error: expected `;’ before ‘to’
sketch_oct04b:19: error: ‘startFrequency’ was not declared in this scope

many thanks in advance!

That was pseudo code; that isn’t how you write a “for” loop, and there is no “next” like there is in BASIC.
(uncompiled, untested

#define GREEN 13 
#define BLUE 12

int startFrequency;

void setup()
{
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  startFrequency = 100;
}

void loop()
{
  for (int x=0 x <100; ++x){
    digitalWrite (GREEN, HIGH);
    delay (startFrequency);
    digitalWrite (GREEN, LOW);
    // maybe another delay here?
  }
  startFrequency += 50;

 if (startFrequency == 600) {
   startFrequency = 100;
 }
}

Exercise: Now eliminate those delays.

Go back and modify that post. Select the code and hit the # icon, then save.

That code from crossroads was not C code it was the idea of what you had to do.

Look up how to use a for loop,
Look up how to declare variables.

ok, In a way this 'if' doesn't make sense to me for what I want. Maybe I'm Wrong.

I wrote somthing more specific for what I want

#define GREEN 13 
#define BLUE 12

int pwm1;
int pwm2;
int pwm3;
int pwm4;
int pwm5;
int pwm6;

void setup()
{
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pwm1 = 1;
  pwm2 = 2;
  pwm3 = 500;   
  pwm4 = 1000;
  pwm5 = 2000;   
  pwm6 = 5000;
}

void loop()
{
  {
    digitalWrite (GREEN, HIGH);
    delay (pwm1);
    digitalWrite (GREEN, LOW);
    delay (pwm2);
}
   {
    digitalWrite (GREEN, HIGH);
    delay (pwm3);
    digitalWrite (GREEN, LOW);
    delay (pwm5);
    digitalWrite (GREEN, HIGH);
    delay (pwm3);
    digitalWrite (GREEN, LOW);
    delay (pwm6);
} 
}

now, how to run just

  {
    digitalWrite (GREEN, HIGH);
    delay (pwm1);
    digitalWrite (GREEN, LOW);
    delay (pwm2);
}

for 20 seconds

and then

  {
    digitalWrite (GREEN, HIGH);
    delay (pwm3);
    digitalWrite (GREEN, LOW);
    delay (pwm5);
    digitalWrite (GREEN, HIGH);
    delay (pwm3);
    digitalWrite (GREEN, LOW);
    delay (pwm6);
}

for 30 seconds

I guess it should be not too difficult, right? hmm

You need to look at the "blink without delay" example to achieve what you want.

When posting code, please use code tags (use the # icon on the editor's toolbar).

Thanks! will look for it

  {
    digitalWrite (GREEN, HIGH);
    delay (pwm3);
    digitalWrite (GREEN, LOW);
    delay (pwm5);
    digitalWrite (GREEN, HIGH);
    delay (pwm3);
    digitalWrite (GREEN, LOW);
    delay (pwm6);
}

Why are there { and } around this code? Learn when, and when not to, use { and }.

Yes! I think this will do!
I’ll the ~pwm outputs, the analogueWrite and I found some millis() code to give it a certain time period.

int GREEN = 9;      // LED connected to digital pin 9
int val = 0;         // variable to store the read value

void setup()
{
  pinMode(GREEN, OUTPUT);   // sets the pin as output
}

void loop()
{
  {unsigned long startTime = millis();
unsigned long interval = 10000UL; // 10 seconds

while(millis() - startTime < interval)
{
  analogWrite(GREEN, 255);  // analogWrite values from 0 to 255
}
{
  {unsigned long startTime = millis();
unsigned long interval = 10000UL; // 10 seconds

while(millis() - startTime < interval)
{
  analogWrite(GREEN, 100);  //  analogWrite values from 0 to 255
}
  }
}
  }
}

Why are there { and } around this code? Learn when, and when not to, use { and }.

To PaulS: thanks for the announcement, I putted them there to show I want this to be one of the looping blocks and I assumed there should be some code in front ... like it was not finished yet But you're also right, have to learn ;)

What you think of the latest code I made so far? is it reasonable?

like it was not finished yet

Only use { and } when you need to not as a place holder for a function.

What you think of the latest code I made so far?

Bit rubbish to be honest, you haven't got your head round the 'blink without delay' idea. You don't need to be continually writing analogWrite(GREEN, 255); That code is just the same as putting a 10 second delay at the end of that statement.

What you think of the latest code I made so far? is it reasonable?

Still far too many curly braces. It is not properly indented (the Tools + Autoformat menu item will help with that).

It is OK to use global variables for startTime and interval.

Using while loops this way gains no advantage over delay().