Arduino IDE 0012 Alpha questions (newbie)

Hi,

I’m new to this, so forgive me if I’ve missed something obvious. I did check the FAQ and google a bit, but no luck.
I have 2 questions…

  1. Is there any way to change the font in the IDE on Windows? I looked in the preferences file, and tried changing Monospaced to Courier New, but it didn’t make any difference…
    Why? Because a lowercase L is EXACTLY the same a digit 1! Insane!
    Especially as L has been my favourite loop variable for years!

  2. Are there any bitwise operators? Like an AND or OR. I was messing about and made my board count in binary on 8 LEDs, and found the lack of an AND and the lack of a power function a complete nightmare!
    I don’t come from a C programming background, I’m a Delphi (Pascal programmer), so might just not know the notation in C. I did look in the language docs, but didn’t find either.

Ta in advance,

Steve.

Bitwise AND in C is “&”, bitwise OR is “|”, bitwise XOR is “^” (caution: “^” is NOT exponentiation!!!)

(the similar logical operators are “&&” and “||”)

if ( (UARTSTATUS & TXEMPTY) && tty->outcount > 0) {
  // send data to the uart
}

Ahhh… That’s better… Thought having no bitwise operators would be a bit of a crazy thing to leave out!

Now if only I could change that font!

Here’s what I came up with for my binary counter, any comments?

void setup()                    // run once, when the sketch starts
{
  for (int i=1; i <= 8; i++){
  pinMode(i, OUTPUT);      // sets the digital pin as output
  digitalWrite(i,LOW);
  }
}

void loop()                     // run over and over again
{
  for (int i=0; i<=255; i++){
    int mask=1;
    for (int bit=1; bit<=8; bit++){
      if ((i&mask)!=0) digitalWrite(bit,HIGH);
      else digitalWrite(bit,LOW);
      mask=mask*2;
    }
    delay(200);
  }
}

Welcome to the world of Arduino and C.

You may find that the C language is a little rough if you are used to the elegance of Delphi, but it’s not difficult picking up the essentials you need to make the arduino sing.

There is nothing wrong with the C code in your sketch, but your outputs start at pin 1 and this is used for the serial port on Arduino ports with USB or RS-232. So pin 2 may be a better place to start.

There are some C constructs that you may find useful and I have modified your sketch to illustrate these. Feel free to use or ignore any of these suggestions, they are stylistic and down to personal preference.

  • You can use the C preprocessor to define constants (conventionally written in upper case in C ) to make code more legible

  • LOW and HIGH are Boolean values (even though C does not have a strict Boolean type like Pascal) so you can use Boolean expressions instead of doing an if/than test

  • you can use the shift left operator << to handle the mask. Shifting left 1 (mask = mask << 1) is exactly the same to the compiler as mask = mask2 but using the shift operator make your intentions clearer. Also, in C mask <<=1 is the same as mask = mask <<1. This works for all operators, for example mask=2 ;

#define START_PIN  2  // defines the start pin, 
#define END_PIN 9

void setup()                    // run once, when the sketch starts
{
  for (int i=START_PIN; i <= END_PIN; i++){
  pinMode(i, OUTPUT);      // sets the digital pin as output
  digitalWrite(i,LOW);
  }
}

void loop()                     // run over and over again
{
  for (int i=0; i<=255; i++){
    int mask=1;
    for (int bit = START_PIN; bit <= END_PIN; bit++){  
      digitalWrite(bit, i & mask);  // HIGH if mask & 1 else LOW
      mask = mask << 1; // shift mask left (could also be: mask <<= 1);
    }
    delay(200);
  }
}

Some differences between C and Delphi/Pascal that can catch you out are:

  • Semicolons are part of the syntax, leaving them out can generate errors but adding one just after a conditional test can create subtle bugs
  • Typing in C is weak, the compiler wont catch many errors that it can in Delphi
  • Array syntax is different in C and the C string handling is extremely primitive!
  • Pascal records are structures in C

Have fun!

Hi Mem,

Thanks for the welcome and feedback, very valuable. Do I detect a closet Delphi fan there?
I was wondering about bit wise shifts, but didn’t know the syntax in C. I came from an assembler and machine code programming background before Delphi, so I love twiddling about with bits.

I had to admit I smiled at the idea of define constructs to make C more readable, it needs more than that! I thought C’s main reason d’etre was to be as unreadable as possible! Why have && when you could have AND? ;D

Thanks for the boolean trick in the digitalWrite, very neat. I am used to doing such things in Delphi, so it’s good to have those.

The semicolons are pretty much the same as in Delphi, although using one on the end of the command preceding an else is slightly odd for me. In Delphi doing that would terminate the if statement and then the else would trip up the compiler.

I wasn’t aware of the 1st output being used for programming, although the penny did drop when I uploaded my sketch and the first LED was flashing away. Didn’t seem to cause any problems.
I’m using a Moderndevice BBB.

Anyway, that’s the language issues handled, thanks very much… Now if only I could change the font so l and 1 don’t look the same, I’d be a very happy man :smiley:

Steve.

On the font issue. I’m on a Mac, not a PC but also wanted more customization of the IDE.

My solution was to check the “External Editor” preference and then use a totally different editor. So now I write the code in Xcode, then compile and upload it in the Arduino IDE. I just made a new Xcode project called Arduino and added all of my .pde source files to it. Of course it will not compile, but who cares? I had to tell Xcode that .pde was a C file.

So I write the code in my favorite editor, save, switch to Arduino IDE, compile, and upload if it succeeds.

So see if you can pull off the same thing with your favorite C/C++ IDE on the PC. It made a big difference for me.

Why have && when you could have AND?

33% greater productivity :slight_smile:

33% greater productivity :slight_smile:

Not quite sure how you measure that… I’m sure the one less keypress is outweighed by all the “WTF?!” moments when you’ve accidental typed only 1 instead of 2, compiled the code and it’s done something weird! :stuck_out_tongue:

So far my “WTF?” moments have been restricted to forgetting an if comparison is double equal sign, not a single! That really does throw the program logic… It appears an assignment always returns true! :-[

Hi Tankslappa,

a trick is to reverse the expression order:
If ( 3 == value)

That way you get a compiler error if you want to compare with a constant value but drop one of the equal signs

And yes, I have always been very fond of Delphi :wink:

a trick is to reverse the expression order:
If ( 3 == value)

Now that is a handy tip!

Many thanks!

I’ve now finished soldering all my PCB mounted component to bit of veroboard with pins, so I can plug them into my breadboard and have a proper play :slight_smile:

It appears an assignment always returns true!

Not that I expect this to improve your perception of C, but the value of the assignment expression a=b is actually [i]b[/i]. So your observation is correct except when zero is assigned.

Back to the question about why && when we could have AND or % when MOD is available: Admittedly this is a matter of taste, but I find that reading well-formatted C code is easier than most languages, precisely because numbers are numbers, names are names, and operators and delimiters are, well, these other little squiggles. It wasn’t long after I migrated from Pascal before I started to enjoy the succinctness and readability of {} versus begin/end and % over MOD. Hopefully you will too!

Mikal

Thanks for that info Mikal.

For well formatted C code, this might be true, but I don’t think I’ve ever seen any! ;D

Not to worry, I’ve got the hang of the difference now, I think!

I’m still doing my day to day work in Delphi, so doing the two languages in my head at the same time has cause me a few EH? moment!

At least it’s not as bad as when I have to do VB work, I spend the next week in Delphi forgetting to put semi colons on the end of lines!

At least it’s not as bad as when I have to do VB work…

It could be worse! It could be COBOL! :slight_smile:

MULTIPLY B BY B GIVING B-SQUARED.  
MULTIPLY 4 BY A GIVING FOUR-A.  
MULTIPLY FOUR-A BY C GIVING FOUR-A-C.  
SUBTRACT FOUR-A-C FROM B-SQUARED GIVING RESULT-1.  
COMPUTE RESULT-2 = RESULT-1 ** .5.
SUBTRACT B FROM RESULT-2 GIVING NUMERATOR.
MULTIPLY 2 BY A GIVING DENOMINATOR.
DIVIDE NUMERATOR BY DENOMINATOR GIVING X.

LOL!

Don’t dis COBOL, I made a nice wedge of cash from knowing that on the run up to 2000!

However, I won’t be sad if I never have to do it again… Too much typing!