Go Down

Topic: Understanding this line " ? HIGH : LOW; " (Read 8578 times) previous topic - next topic

Zoandar

No doubt the wrong choice of terminology on my part? Sorry about that. Should I have said "constant" or "function"? Still learning. :)
Zoandar
  ---v/\/\/\v---

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


In Mazzimo's book, when he wanted to set up an LED on pin 13 he used the command

Code: [Select]
#define LED 13

But in Maik's Quick Start Guide book, he uses the command

Code: [Select]
const unsigned int LED_PIN = 13;

As they saying goes - "same difference". :)


Indeed, well some years ago this was actively debated among even professional programmers.  Among most people I know, it's pretty well settled that the const int construct is preferred, but #define certainly works.

As one of the earlier ranters pointed out, basing Arduino on C++ exposes the beginner to all sorts of these intricacies.  (But as also correctly responded, the ecosystem would not be here if it were something else.)

Zoandar

:)

I guess that command "const" sort of says it all, doesn't it? I looked back through the code as you were replying. Sorry about that mistake - it is a bad habit I have - referring to 'any' letter or name defined to hold a value as a variable when sometimes it is a constant. I know constants can't later be reassigned a different value in the same program where variables can change many times. I need to learn to use the correct term. :) Hopefully it will implant in my mind as I write more code and use both forms. Thanks for setting me straight.

Is there any way to set this forum so it 'auto-saves' what I am typing in a post? I'm doing this on my LG Ally, and I had a scheduled reminder alarm go off just before I was ready to post. It dumped me out of Opera Mobile 10.2 and I lost all this and had to retype it. Or maybe some way to make Opera keep it? The Opera History had Post Reply in this forum, but the text box was empty.
Zoandar
  ---v/\/\/\v---

Simpson_Jr


Whatever the case, in order to work with the Arduino platform I have to learn the language provided. (Unless someone wrote a MS Basic to Arduino code converter). :)
Thanks.


I wouldn't like to keep you from your learning experience, I'm more or less in the same boat, but... http://www.mcselec.com/ has an easy basic-compiler for most 8-bit AVR chips. It's been a while since it was last updated, but there's still quite a lot of support for it. Some of the projects made with it are simply amazing.

I haven't hooked it up to the test-bench to compare with C/C++, it may... consume a little more memory or run a little slower. I haven't noticed big differences though...

One thing I really like about it is the amount of chips supported. From Attinys with 6 pins and just 1 KB
to 256KB atmegas. Arduino may also be able to support those chips, but loads.... aren't yet.

Fastavr and GreatCowbasic also use Basic, but haven't tested those.

pwillard

Some of these things being discussed are also on my list of peeves.

The whole "it's C++" thing is a bit misleading. I'm not arguing that Arduino is NOT C++, far from it.  I have seen some get quite adamant about all this stuff saying, "It's C++... How dare you say just  'C'  because the AVRGCC compiler embedded in Arduino does support things like OBJECT and CLASSES".  Yes, arguably cool... but this will, in my opinion, just plain baffle a newcomer that wants to play with LED's.

The important thing to understand, I think, is that C++ supports the entire predecessor ANSI C standard library. All the standard C functions should be available but as a successor, C++ also defines additional library functions and new features that are best saved for more advanced programming.

So I would advise that a "beginner" start with a basic C book or guide first and not C++ and then see how far that gets them.   Save delving into the C++ "objects and classes" environment  for when the basics of C are better understood.

Arduino examples that obfuscate should also be verboten and redacted.

PaulS

Quote
So I would advise that a "beginner" start with a basic C book or guide first and not C++ and then see how far that gets them.   Save delving into the C++ "objects and classes" environment  for when the basics of C are better understood.

Total agreement here!

gorbs

led_state = (led_state == LOW) ? HIGH : LOW;

It checks a conditional and then assigns the output to led_state.

Take everything to the right of the first =

(condition to check) ? return this if equates to true : return this if equates to false;

Thus, if led_state is equal to LOW, then set it to HIGH. If it's not equal to LOW, set it to LOW.

Remember the words in all capitals LOW,HIGH are constants.

Similarly, I could do something like:

result = ( amount >= 10 ) ? TRUE : FALSE;

which is the same as:

if (amount >= 10) {
 result = TRUE;
}
else {
 result = FALSE;
}

Zoandar



Whatever the case, in order to work with the Arduino platform I have to learn the language provided. (Unless someone wrote a MS Basic to Arduino code converter). :)
Thanks.


I wouldn't like to keep you from your learning experience, I'm more or less in the same boat, but... http://www.mcselec.com/ has an easy basic-compiler for most 8-bit AVR chips. It's been a while since it was last updated, but there's still quite a lot of support for it. Some of the projects made with it are simply amazing.

I haven't hooked it up to the test-bench to compare with C/C++, it may... consume a little more memory or run a little slower. I haven't noticed big differences though...

One thing I really like about it is the amount of chips supported. From Attinys with 6 pins and just 1 KB
to 256KB atmegas. Arduino may also be able to support those chips, but loads.... aren't yet.

Fastavr and GreatCowbasic also use Basic, but haven't tested those.


Thanks, but I was sort of joking when I wrote that. I'll keep these in mind for later though. Might come in handy!
Zoandar
  ---v/\/\/\v---

Zoandar


led_state = (led_state == LOW) ? HIGH : LOW;

It checks a conditional and then assigns the output to led_state.

Take everything to the right of the first =

(condition to check) ? return this if equates to true : return this if equates to false;

Thus, if led_state is equal to LOW, then set it to HIGH. If it's not equal to LOW, set it to LOW.

Remember the words in all capitals LOW,HIGH are constants.

Similarly, I could do something like:

result = ( amount >= 10 ) ? TRUE : FALSE;

which is the same as:

if (amount >= 10) {
 result = TRUE;
}
else {
 result = FALSE;
}


So it works much like the old MS Basic's IF..THEN...ELSE command. I used to use that a lot. Thanks for the info!
Zoandar
  ---v/\/\/\v---

Zoandar



So I would advise that a "beginner" start with a basic C book or guide first and not C++ and then see how far that gets them.   Save delving into the C++ "objects and classes" environment  for when the basics of C are better understood.

Arduino examples that obfuscate should also be verboten and redacted.


So if I were to read a basic C tutorial it would not immediately launch me into code that the Arduino can't use?

Well, still not, I am afraid. I went here:

http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Form%20of%20a%20C%20program

and looked at the beginning of the tutorial on the structure of a C program. Although it is similar, the Arduino IDE doesn't like their lack of things like using the "void" command before using a function (subroutine) name, etc. From a grass-roots point of view, still confusing when trying to learn how to write code for Arduino. Had I not learned today about not needing the "main" function from you good folks, I would have been stumped right there, because it is not listed in the Arduino Reference (since it isn't used) yet it is the first thing the C tutorial says is needed.

Is there anywhere someone has put together a site that lists every usable programming command for Arduino, along with example code for each command? That would be a very good tool to find. Sadly the Arduino 0022 Reference only comes close.
Zoandar
  ---v/\/\/\v---

oldPGMguy

being new at this..2 days..C or C++ is hard for me after doing Visual Basic or PBasic for basic stamps....I would save snippets of code..I plan to do the same in C....sooner or later , I will grasp it
I'm old and started too late with microcontrollers

Zoandar

I am keeping a log of the things I am encountering and learning. My memory isn't that great. :)

I just realized as I am typing in the next project in the book I am reading that it seems constants always are UPPERCASE and variables or functions are lowercase. Is that 'standard' for Arduino?
Zoandar
  ---v/\/\/\v---

Zoandar

I have another unexplained use of something which I would like to understand. In the code below, what does the following line using a single "&" do?

Code: [Select]
digitalWrite(LED_BIT0, result & B001);

I know that "&&" means boolean "AND". I see a listing in the reference which starts with "&" and talks (cryptically) about pointers, but I don't think this is a pointer. I think it is doing something with the output value and a binary number (in this case "1") together. I just don't know "what" it is doing:

Code: [Select]
//BinaryDice/DiceWithButton.pde

const unsigned int LED_BIT0 = 12;
const unsigned int LED_BIT1 = 11;
const unsigned int LED_BIT2 = 10;
const unsigned int BUTTON_PIN = 7;

void setup() {
  pinMode(LED_BIT0, OUTPUT);
  pinMode(LED_BIT1, OUTPUT);
  pinMode(LED_BIT2, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
  randomSeed(analogRead(A0));
}
int current_value = 0;
int old_value = 0;

void loop() {
  current_value = digitalRead(BUTTON_PIN);
  if (current_value != old_value && current_value == HIGH) {
    output_result(random(1, 7));
    delay(50);
  }
}

void output_result(const long result) {
  digitalWrite(LED_BIT0, result & B001);
  digitalWrite(LED_BIT1, result & B010);
  digitalWrite(LED_BIT2, result & B100);
}
Zoandar
  ---v/\/\/\v---

Coding Badly

I just realized as I am typing in the next project in the book I am reading that it seems constants always are UPPERCASE and variables or functions are lowercase. Is that 'standard' for Arduino?


Purely personal preference.  You are free to make constant names using any legal identifier characters.  By convention, constants are uppercase.

Go Up