Boolean,for,while,if,else,values etc. - Page 1
Forum section: electronics / beginners

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: February 17, 2013, 09:12:40 pm
content: Hello i recently started programming arduino (using the IDE on a C based language) but i have a problem understanding the use of some stuff.My first problem is the command boolean i have no idea how to use it but even when i googled it and searched about it i understood something but not the whole meaning of a boolean.also how i can use for and while to loop things inside a loop.another thing is the if else commands.can i use if and else many times within a loop so that i cabn check and if something is true or false??the last thing i would like to know is the values and the variables.How do i read a value and then if this value is HIGH(for example) the arduino will do something?also how do i initialize a variable?Another last question.Can i use many loops in a sketch??? Likeint....int...void setup(){.....}void loop(){.....}void loop(){......}Thank you.And if you can suggest a website for code learning and stuff.Thank you again.

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: February 17, 2013, 09:37:09 pm
content: thank you.

author: HackedFridgeMagnet
author_rank: Super Contributor
timestamp: February 17, 2013, 09:37:59 pm
content: I am sure there are other ways to get started using c++ but here is a tutorialhttp://www.cprogramming.com/tutorial/lesson1.htmlorhttp://www.cplusplus.com/doc/tutorial/Once you get started you need a reference, in which you can look up any language specifics that you come across.http://www.cplusplus.com/reference/Arduino uses C++ but tries to hide the more difficult pieces and also make it more readable using "Macros".

author: IanB
author_rank: Super Contributor
timestamp: February 17, 2013, 09:48:29 pm
content: I think Mechatrommer was a bit unkind, but this does sort of belong in the beginners section.The answers to most of your questions will become clear after more time and more learning. It may help if you can find some introductory books or web sites on programming. Your questions are not very specific to Arduino, they are questions about programming languages and computers in general.To answer one of them, boolean is about mathematical logic where the values are "true" and "false". You should normally encounter boolean algebra in high school mathematics at some point.In an if/else statement the condition has to evaluate to a boolean true/false value. If the result is true the "if" part is executed, if the result is false the "else" part is executed.You can store boolean values in boolean variables and make combinations of logical tests this way. For example:true AND true = truetrue AND false = falsetrue OR false = trueExample:IF the television is plugged in AND the television is switched on THEN I can watch televisionA = "the television is plugged in"B = "the television is switched on"IF A AND B THEN I can watch television

author: Psi
author_rank: Super Contributor
timestamp: February 17, 2013, 11:17:48 pm
content: Just be aware of the difference between human OR (English language) and programming OR.If someone says to you"I will buy you either an osilloscope OR a multimeter for Christmas"Then the English language says there are two possibilities for Christmas. You get an oscilloscope OR You get a multimeter.However with OR in programming both options at once satisfy the OR statement as TRUE.So you could get the oscilloscope ,or the multimeter, or the oscilloscope and the multimeter together.IF (A=1) OR (B=1) THEN print('Both are 1')A=0 B=0 = nothingA=0 B=1 = Message is displayedA=1 B=0 = Message is displayedA=1 B=1 = Message is displayed <- This is different to english OR

author: Psi
author_rank: Super Contributor
timestamp: February 17, 2013, 11:39:09 pm
content: Quote from: ChrisGreece52 on February 17, 2013, 09:12:40 pmCode:void loop(){for (LoopA=1;LoopA<=10;LoopA++){// LoopA is 1 to 10// whatever code is here will run 10 timesLoopB = 5;while (LoopB>0){LoopB = LoopB - 1;// for every loop of loopA you get 5 loops of loopB// whatever code is here will run 10 * 5 = 50 times before exiting}}}Quote from: ChrisGreece52 on February 17, 2013, 09:12:40 pmyesCode:for (LoopA=1;LoopA<=10;LoopA++){if (MyVariableA==5){//Do Something if variable is 5} else {//Do Something if variable is any value except 5}if (MyVariableB==5){//Do Something if variable is 5} else {//Do Something if variable is any value except 5}}

author: IanB
author_rank: Super Contributor
timestamp: February 18, 2013, 01:06:04 am
content: Quote from: Psi on February 17, 2013, 11:17:48 pmThat sort of depends.For instance:IF "you can afford to buy an oscilloscope" OR "you get given an oscilloscope" THEN you can have an oscilloscopeNow if you can afford to buy an oscilloscope and you get given an oscilloscope anyway, you will definitely have access to an oscilloscope. If both conditions are true, there is no doubt that the result is true.

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: February 18, 2013, 09:07:34 am
content: Quote from: IanB on February 17, 2013, 09:48:29 pmThank you this helped a bunch really. As for the boolean in mathematics i am in the second year in high school and we still didnt learn them (the educational system is bad in Greece very bad) But i got the meaning of boolean and its like the and/or/not gates in electronics right ?

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: February 18, 2013, 09:12:02 am
content: Quote from: HackedFridgeMagnet on February 17, 2013, 09:37:59 pmThank you i will check these sites out.

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: February 18, 2013, 09:12:29 am
content: Quote from: Psi on February 17, 2013, 11:17:48 pmThank you this helped me a lot .

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: February 18, 2013, 09:13:04 am
content: Quote from: Mechatrommer on February 17, 2013, 11:28:01 pmThank you.

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: February 18, 2013, 09:18:23 am
content: Quote from: Psi on February 17, 2013, 11:39:09 pmFinally got it thank you .

author: psycho0815
author_rank: Regular Contributor
timestamp: February 18, 2013, 07:57:30 pm
content: As for the loop() in the Arduinosketch, that's rather stupidly named imho.It's not a loop in terms of a programming construct, like for and while are, it's just a method that's incidentally called loop.Basically what the Arduino-precompiler does to your code, is turn it into something like this:Code:#include Arduino.h // and possibly some other includesint main(){init(); // Setting the timers and stuffsetup(); // Your setup method from your sketchwhile(1){loop(); // Your loop method}// here come your actual methods ...}so it just takes your loop-method and runs it within an endless while-loop, which is propably why it is named like that. I can see how that might be confusing...