3 PWM outputs problem

Hello
I am trying to modify this code to have multiple PWM outputs. Could you tell me which part needs to be corrected? software says expected initializer before 'digitalWrite'

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);

void loop1() 
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1);                 // wait for a second
void loop2() 
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);               // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delay(1);                 // wait for a second
void loop3() 
  digitalWrite(10, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(70);               // wait for a second
  digitalWrite(10, LOW);     // turn the LED off by making the voltage LOW
  delay(1);                 // wait for a second
}

That code is simply hopeless.

What the compiler is complaining about is you define a function loop1 but do not enclose it in braces { } like you did when you defined setup. Only you forgot the closing } one.

However this is just a sticking plaster. That code is no where close to conceptually correct.

To produce PWM you use analogWrite.
You need a loop function.
Your three functions loop1, loop2, and loop3 will not run unless called.

You can not define a function inside another function definition.

To fix that code it needs to look like this:-

/*
  Blink
 
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop1(){
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1);  
}

// wait for a second
void loop2() {
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);               // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delay(1);  
}
  // wait for a second
void loop3() {
  digitalWrite(10, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(70);               // wait for a second
  digitalWrite(10, LOW);     // turn the LED off by making the voltage LOW
  delay(1);                 // wait for a second
}

void loop(){
  loop1();
  loop2();
  loop3();
}

And by fix I mean make it run, not do what you want.

THANK YOU sooo much Mr Mike.

one last thing;

To produce PWM you use analogWrite.

According to your advise should i convert digitalWrite ==> to anologWrite to work with PWM outputs like this??

if you have time one more question, you dont have to reply;

I have ready everything writren about PWM in this web page. Since i am soo Newbie, i could not understand... :frowning:

where can i find a code to play with values to change Hz and duty cycles for 3 outputs?

it will be very much apreciated if you just copy and paste the link to be a map for me.

/*
  Blink
 
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop1(){
  analogWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  analogWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1); 
}

// wait for a second
void loop2() {
  analogWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);               // wait for a second
  analogWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delay(1); 
}
  // wait for a second
void loop3() {
  analogWrite(10, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(70);               // wait for a second
  analogWrite(10, LOW);     // turn the LED off by making the voltage LOW
  delay(1);                 // wait for a second
}

void loop(){
  loop1();
  loop2();
  loop3();
}

where can i find a code to play with values to change Hz and duty cycles for 3 outputs?

You can not use the duty cycle to change anything but the timbre of a note, and then not by much.

Changing the frequency of the PWM will change the frequency of a note but this is not the way to do it. Use the tone function if you want an audio output.

I have ready everything writren about PWM in this web page. Since i am soo Newbie, i could not understand.

Try this page of mine:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

convert digitalWrite ==> to anologWrite to work with PWM outputs like this

No not quite. Once you set an output producing PWM with an analogWrite it will keep on outputting the same signal until you tell it to stop.

Dear Mr Mike,
Good days,

This page is very useful. Many people write lots of unnecessary things to make their page look complicated. However this one is to help people to have an idea about electronics. Thank you.

Try this page of mine:-
PWM

I have tried the code on the board;

normally we can have any Hz value to drive leds when delay values are changed.

void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second

However on this 3 led blink code given;

when delay values are changed, the Hz becomes very slow and unsable. What can be do to work it just like single blink code?

/*
  Blink
 
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop1(){
  analogWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  analogWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1);
}

// wait for a second
void loop2() {
  analogWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);               // wait for a second
  analogWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delay(1);
}
  // wait for a second
void loop3() {
  analogWrite(10, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(70);               // wait for a second
  analogWrite(10, LOW);     // turn the LED off by making the voltage LOW
  delay(1);                 // wait for a second
}

void loop(){
  loop1();
  loop2();
  loop3();
}

when delay values are changed, the Hz becomes very slow and unsable.

Slow yes, that is because the delays are very long, unstable no the timing will not be unstable.

What can be do to work it just like single blink code?

The problem is the way you have written it. Using delays causes what is known as blocking, that is during the delay you can not do anything else. So in that code you have if you change the time for just one LED then it will affect the time of the other two.

What you need to do is to use a technique given in the "blink without delay" example in the IDE. As some people find it hard to follow there are some examples with more words:-
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once

Thank you once more Mr Mike,

after searching and trying for almost 4 hours i have found this beautiful code!!!

as a wish from arduino masters;

i am lost in library errors starting with include
if there would be build in a code to simply change duty cycle and Hz of multiple leds it would be perfect and very usefull for many people...

const int ledPin1 =  12;      // the number of the LED1 pin
const int ledPin2 =  13;      // the number of the LED2 pin

int ledState1 = LOW;             // ledState used to set the LED1
int ledState2 = LOW;  
long previousMillis1 = 0;        // will store last time LED1 was updated
long previousMillis2 = 0;        // will store last time LED2 was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval1 = 10;           // interval at which to blink LED1 (milliseconds)
long interval2 = 20;           // interval at which to blink LED2 (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);   
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis1 > interval1) {
    // save the last time you blinked LED1 
    previousMillis1 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;

    // set  LED1 with the ledState of the variable:
    digitalWrite(ledPin1, ledState1);
  }
    if(currentMillis - previousMillis2 > interval2) {
      // save the last time you blinked LED2 
    previousMillis2 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledState2);
  }
}

i am lost in library errors starting with include

See this:-

if there would be build in a code to simply change duty cycle and Hz of multiple leds it would be perfect and very usefull for many people.

Are you after code to blink LEDs or code to make tones?

It is a simple matter to extend that code you posted to handle more LEDs, simply add extra code. You will see the code that handles the first LED is almost the same as the code that handles the second, only a few variables are changed. Just repeat this for a third and change the same variables.