Author Topic: Two blinking LEDs in arduino, code complies but second led doesn't blink.  (Read 2537 times)

0 Members and 1 Guest are viewing this topic.

Offline raspberrypiTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 358
  • Country: us
I basicaly copied and pasted the code for the first LED but changing all the variables of the second LED with a "2" at the end and used pin 11.

No errors but doesn't blink. I'm thinking they have to be in one loop or two different loops?

Code: [Select]
/*
 Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 The analogWrite() function uses PWM, so if
 you want to change the pin you're using, be
 sure to use another PWM capable pin. On most
 Arduino, the PWM pins are identified with
 a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

 This example code is in the public domain.
 */

int led = 9;           // the PWM pin the LED is attached to
int brightness = 1;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
int led2 = 11;           // the PWM pin the LED is attached to
int brightness2 = 1;    // how bright the LED is
int fadeAmount2 = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup2() {
  // declare pin 9 to be an output:
  pinMode(led2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop2() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
I'm legally blind so sometimes I ask obvious questions, but its because I can't see well.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
The setup and loop functions are predefined and called by the Arduino main (entry) function. Your loop2 is your own invention and I cannot see it being called (typically from within loop)...

[2c]
« Last Edit: March 06, 2017, 07:52:55 am by obiwanjacobi »
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 
The following users thanked this post: Ian.M

Offline raspberrypiTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 358
  • Country: us
The setup and loop functions are predefined and called bij the Arduino main (entry) function. Your loop2 is your own invention and I cannot see it being called (typically from within loop)...

[2c]

Ok so what does that mean? Whats bij? So loop is a set variable and not a name you make up like led.Red? So I should change that back to just loop
I'm legally blind so sometimes I ask obvious questions, but its because I can't see well.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12865
No.  setup() and loop() are predefined names for two functions called from the C main() function.

Lets take a look 'under the hood' at the Arduino main() function:
Code: [Select]
int main(void)
{
init();

initVariant();

#if defined(USBCON)
USBDevice.attach();
#endif

setup(); // <== calls your setup() function
   
for (;;) {
loop(); // <== calls your loop() function
if (serialEventRun) serialEventRun();
}
       
return 0;
}
As the Arduino IDE creates main() for you, and hides it, you are only allowed ONE setup() function, which must set up both LEDs one after the other, and ONE loop() function, which must process the changes for both LEDs.
« Last Edit: March 06, 2017, 07:41:19 am by Ian.M »
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Ok so what does that mean? Whats bij? So loop is a set variable and not a name you make up like led.Red? So I should change that back to just loop

It means if you want a new function called setup2 to be executed, you need to call it yourself. Bij was probably a typo for by.
setup and loop are indeed predefined names, which called by convention.
setup is called exactly once when the arduino starts. loop is called repetitively for you, behind the scenes.

If you wanted multiple 'stages' in setup and loop you could do something like this:

Code: [Select]
void setupLeds() { /* implement */ }
void setupWhateverElse() { /* implement */ }
void setup(){
  setupLeds();
  setupWhateverElse();
}

void processLeds() { /* implement */ }
void processWhateverElse() { /* implement */ }
void loop(){
  processLeds();
  processWhateverElse();
}

That is to say, you can easily define your own functions. But you need to call them yourself.

EDIT: Remember though, that now a delay in one of the functions will delay the other one as well. There are many libraries out there to help you run things say every x millisecond. Here is my library: TimedAction. You may need to rename WProgram.h to Arduino.h in TimedAction.h because I'm lazy and because of the Untold History of Arduino.
« Last Edit: March 06, 2017, 07:21:28 am by alexanderbrevig »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Arduino code expects to call exactly one setup() function at startup.  Either put all of your setup code in the predefined location or call your other setup functions from within setup().

Arduino code expects to call exactly one loop() function.  Same drill as above:  Put all of your code inside loop() or call your other code from withing loop().
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Ok so what does that mean? Whats bij? So loop is a set variable and not a name you make up like led.Red? So I should change that back to just loop

Yes, sorry: 'bij' is dutch for 'by'   ;D

'loop' is not a variable its a function name as explained above. led.Red looks like variable 'led' that is set to an instance of a class and Red is a member function of that class that is being called.

If you change loop2 into loop - the compiler will see two functions with the same name and that will be an error.

Sounds like you need to read some basic C/C++ tutorials first...
« Last Edit: March 06, 2017, 08:02:03 am by obiwanjacobi »
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline raspberrypiTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 358
  • Country: us
Ok so what does that mean? Whats bij? So loop is a set variable and not a name you make up like led.Red? So I should change that back to just loop



Yes, sorry: 'bij' is dutch for 'by'   ;D

'loop' is not a variable its a function name as explained above. led.Red looks like variable 'led' that is set to an instance of a class and Red is a member function of that class that is being called.

If you change loop2 into loop - the compiler will see two functions with the same name and that will be an error.

Sounds like you need to read some basic C/C++ tutorials first...

I think that would help. I watched this series: https://youtu.be/w-yfj1VXW8I
This guy seems to leave out big chunks of info that I figured out by just looking at the code and changing it. Pretty much just learning through logic and deductive reasoning.

But this is the first time I have heard anyone tell me that the arduino program is C++. Which I have no clue about. I almost* understand what you guys are talking about, but I have no idea as to syntax or what the code words are. So if I want to get serious about this I need to know C++? What the hell is python then?

If I'm going to take the time to learn code I'm going to learn the most useful one, both as a hobbiest and as an engineer, even though my school is focused on chemistry, automation of lab experiments would be a HUGE tool to have. Many chem reactions require things to be added drop by drop in a certain temp range, and/or pH range, over the course of an hour or even longer. Setting up a simple device that controls valves, and hot plates, while monitoring temp would allow you to run many reactions at once.

So C or C++?
« Last Edit: March 06, 2017, 12:20:08 pm by raspberrypi »
I'm legally blind so sometimes I ask obvious questions, but its because I can't see well.
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Arduino is based on the Wiring Framework which is written in C++.
Arduino code, is therefore C++ (some call it a superset, see 1).

Python is a different programming language, which can also run on micro controllers through MicroPython - but this is not Arduino.

C is an older language with simpler constructs and most times results in more efficient machine code (program runs faster, using less memory or both).

Since you've got an Arduino. Focus on C++ now. If you find you like programming, I'd say you should have a look at Python too :) For instance, you could make a program that communicates with a few Arduinos in order to control you lab :)
Good luck!

1 In fact, there's a few things the Arduino/Wiring editor does which makes the code not compatible with any C++ compiler without some easy tweaks. This has to do with declaration, and you should not worry about it yet. I just mention it in case someone tells you it's not C++ - and they would kind of be correct.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Yes, Arduino is C++ but most Arduino code I see is plain C. Start there for a basic understanding of the 'code-words', constructs and logic behind code/programming. That knowledge is transferrable to C++ if you want to advance later on.

Learn about:
- key words/code constructs: conditional-test/flow control (if-else), loops (for, while) and perhaps the case/switch statement
- Variables
- Functions

By this time, you will know what topic you want to learn more about. It's good to have a concrete project in mind. Start very simple (like you did with these LEDs) and build upon that with little increments. Do research each time you get stuck. Share your code for review to let others suggest new things to you.

Good luck!
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline raspberrypiTopic starter

  • Frequent Contributor
  • **
  • !
  • Posts: 358
  • Country: us
Focus on C++ now. If you find you like programming, I'd say you should have a look at Python too :) For instance, you could make a program that communicates with a few Arduinos in order to control you lab :)
Good luck!
[/size]

I don't think I will. I took Vbasic my second semester at college back in the day and found it tedious. But some times it is cool to see something you created actually do something useful. I have alot more patience now that I'm older mainly because I have to since I lost alot of my mobility. So C or C++? Why don't these arduino tutorials mention the word C++? The people teaching it seem to also think their audience has great disdain for math which is a shame as math is the programing language of the universe.
 
I'm legally blind so sometimes I ask obvious questions, but its because I can't see well.
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
I don't think I will. I took Vbasic my second semester at college back in the day and found it tedious. But some times it is cool to see something you created actually do something useful. I have alot more patience now that I'm older mainly because I have to since I lost alot of my mobility. So C or C++? Why don't these arduino tutorials mention the word C++? The people teaching it seem to also think their audience has great disdain for math which is a shame as math is the programing language of the universe.
It may surprise you when you get down into it.
Definitly go for C++ and Arduino now, since that's what you've got.
The reluctance to mention C++ has to do with the history of where and how Arduino came to be. It was generally understood there that C++ was inherently hard. So, by avoiding using the name and profive a very human readable naming scheme (digitalWrite etc) they rather wanted to focus on the fact it is EASY. It's totally approachable by people with no C++ knowledge, though you will sooner or later stumble upon some problem; at which point knowing that it is C++ would help you find solutions while googling.

Your code from your first post will blink both LEDs if you simply add a "call" to setup2 from setup, and to loop2 from loop. Try adding setup2(); just before the last } in you setup, and loop2(); just before the last } in your loop. A "call" is often easy to spot because of the () following the name of the function that is called.
When C++ sees this it knows that you want to leave where you are, and run the statements found in that other function before you get back to where you was, and the code continues.
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
So C or C++? Why don't these arduino tutorials mention the word C++? The people teaching it seem to also think their audience has great disdain for math which is a shame as math is the programing language of the universe.

Since you like math, you may like the C++ standards document:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf

I wouldn't suggest it for learning C++, but it is good if you really want to know how things work in C++, and reading the index and skim-reading interesting parts will give you an idea what is possible.

In general learning C++ is a good idea, if you want to do more embedded programming, because C++ is available and still used for nearly every microcontroller. But Arduino is not a good environment for lerning C++. I would recommend something like Qt Creator or Visual Studio (both free IDEs for the PC, creating programs which run on the PC, with Qt Creator on Mac and Linux, too), with single-step and breakpoint debug support, which is very helpful for beginners to see how a program works and how the variables change. The Arduino way of using Serial.print is crude.

I think the best way to learn a new language is to write programs with it, which solve tasks that you want to solve. I did this and used various books like The C++ Programming Language and read about language features, and tried and modified the examples.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12865
99.9% of the time you can ignore the 'C++' stuff and program it as if it was plain 'vanilla' ANSI C - which is probably actually easier than BASIC once you get your head around the syntax and the compiler's tendency to report syntax errors on a line after the one that has the real problem.   There are far more good ANSI C tutorials than there are tutorials for any specific dialect of BASIC.

However 'Arduino' doesn't support ANSI C standard character I/O fresh out the box, and AVR GCC has a cut-down stdio library, so even if you fix that and fool the IDE into using vanilla ANSI C, some printf() and scanf() format specifiers don't work. 

You can also mix and match - be aware of what the IDE is doing between your back and stuff what you've learnt from C tutorials into either setup() or loop() as appropriate, avoiding using printf() or scanf() by doing I/O using the Arduino Serial library.  You'll run into a few C++'isms that aren't legal C, which you can look up when you meet them.
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5030
  • Country: ro
  • .
You should watch this series when you have the time: https://www.youtube.com/playlist?list=PLE72E4CFE73BD1DE1
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf