Two blinking LEDs in arduino, code complies but second led doesn't blink. - Page 1
Forum section: electronics / general

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 06, 2017, 06:38:22 am
content: 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:/*FadeThis example shows how to fade an LED on pin 9using the analogWrite() function.The analogWrite() function uses PWM, so ifyou want to change the pin you're using, besure to use another PWM capable pin. On mostArduino, the PWM pins are identified witha "~" 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 toint brightness = 1; // how bright the LED isint 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 effectdelay(30);}int led2 = 11; // the PWM pin the LED is attached toint brightness2 = 1; // how bright the LED isint 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 effectdelay(30);}

author: obiwanjacobi
author_rank: Super Contributor
timestamp: March 06, 2017, 06:42:58 am
content: 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)...

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 06, 2017, 06:50:10 am
content: Quote from: obiwanjacobi on March 06, 2017, 06:42:58 amOk 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

author: Ian.M
author_rank: Super Contributor
timestamp: March 06, 2017, 07:05:30 am
content: 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:int main(void){init();initVariant();#if defined(USBCON)USBDevice.attach();#endifsetup(); // <== calls your setup() functionfor (;;) {loop(); // <== calls your loop() functionif (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.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: March 06, 2017, 07:16:23 am
content: Quote from: raspberrypi on March 06, 2017, 06:50:10 amIt means if you want a new function calledsetup2to be executed, you need to call it yourself. Bij was probably a typo for by.setupandloopare indeed predefined names, which called by convention.setupis called exactly once when the arduino starts.loopis called repetitively for you, behind the scenes.If you wanted multiple 'stages' insetupandloopyou could do something like this:Code: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 renameWProgram.htoArduino.hinTimedAction.hbecause I'm lazy and because of theUntold History of Arduino.

author: rstofer
author_rank: Super Contributor
timestamp: March 06, 2017, 07:31:14 am
content: 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().

author: obiwanjacobi
author_rank: Super Contributor
timestamp: March 06, 2017, 07:54:15 am
content: Quote from: raspberrypi on March 06, 2017, 06:50:10 amYes, sorry: 'bij' is dutch for 'by''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...

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 06, 2017, 12:18:08 pm
content: Quote from: obiwanjacobi on March 06, 2017, 07:54:15 amI think that would help. I watched this series:https://youtu.be/w-yfj1VXW8IThis 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++?

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: March 06, 2017, 12:35:29 pm
content: Arduino is based on the Wiring Framework which is written in C++.Arduino code, is therefore C++ (some call it a superset, see1).Python is a different programming language, which can also run on micro controllers throughMicroPython- 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 tooFor instance, you could make a program that communicates with a few Arduinos in order to control you labGood luck!1In 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.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: March 06, 2017, 12:35:42 pm
content: 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- FunctionsBy 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!

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 06, 2017, 01:30:41 pm
content: Quote from: alexanderbrevig on March 06, 2017, 12:35:29 pmI 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.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: March 06, 2017, 01:39:39 pm
content: Quote from: raspberrypi on March 06, 2017, 01:30:41 pmIt 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" tosetup2fromsetup, and toloop2fromloop. Try addingsetup2();just before the last}in yousetup, andloop2();just before the last}in yourloop. 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.

author: FrankBuss
author_rank: Unknown
timestamp: March 06, 2017, 03:21:17 pm
content: Quote from: raspberrypi on March 06, 2017, 01:30:41 pmSince you like math, you may like the C++ standards document:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdfI 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 likeThe C++ Programming Languageand read about language features, and tried and modified the examples.

author: Ian.M
author_rank: Super Contributor
timestamp: March 06, 2017, 05:04:00 pm
content: 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 youfix 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.

author: mariush
author_rank: Super Contributor
timestamp: March 06, 2017, 05:17:39 pm
content: You should watch this series when you have the time:https://www.youtube.com/playlist?list=PLE72E4CFE73BD1DE1