Super Noob needs help with basic code.

Hey all, thanks for taking the time to have a look at this.

I have never done anything like this before and I cant seem to find a dumb-ed down enough explanation of code for me to get an understanding of things with.

This is on the Arduino site:


setup()

The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board.

Example

int buttonPin = 3;

void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT); }

void loop() { // ... }


Fist off it talks about what the setup() is and then the loop() is, but never anywhere I've searched is there anything on what the VOID part means or why its there??

If everything after the setup() sets the basic parameters of what the loop() needs to run then why is int buttonPin = 3; above the setup() line?

Also why are there a () after the word setup and loop??

one last thing. Does anyone know of a good place to go to find an understanding of the structure of the code. Just how do i assemble things??

Thanks for the help !!!!

Cheers, Brant

setup and loop are names of functions. Some functions can return a value. The type of value that the function can return precedes the name of the function when it is defined. If the function does not return a value, as setup and loop do not, the function return type is void.

Following the name of the function is a list of arguments to the function, in parentheses. If the function takes no arguments, as setup and loop do not, the parentheses are still required.

Outside of a function body, the only code that can be present in a sketch is that required to declare and initialize global variables. Variables can be global or local in scope. Global variables are declared outside of any function, and can be accessed anywhere. Local variables, on the other hand, are declared inside a function, and can only be accessed from within that function. In the example, buttonPin is a global variable, accessible from any function, including setup and loop.

Any basic C or C++ book will devote the first chapter to the very basic issues you are asking about.

Thank you so much for your help. I will take your advice and get a book...!

You don't even really need a book.. there are many GREAT online references.

The structure of Arduino is C, in fact, Arduino is just C with a wrapper on it.. when it compiles to the code the chip actually runs, it is done with our old friend GCC... the GNU C Compiler.

Using C is a great idea to learn- because most modern programming languages are expansions upon the basic structure of C. Once you learn C, you will be able to roughly understand many modern languages.. most notable being C++ and Java. What you learn now applies as you learn and go forward.

So i got myself a book on C. I’m now reading a section on escape from print to be used in an instance where you want to print out some " and not have them interpreted by the program.

They say to use the \ in front of something you want ignored. The code looks like this.


#include <stdio.h>

int main()
{
printf(“He said, “Ta da! I am a text string.””);

return(0);
}


This works fine. Just on a whim to see if it only applyed to the " and also to see if you can get the \ to print out i tried the next code.


#include <stdio.h>

int main()
{
printf(“He said, “Ta da! I am \a text string.””);

return(0);
}


notice the extra \ in front of the letter a. interestingly the letter a is gone when the printf executes. Also when i run this program my computer beeps. It doesn’t come threw the speakers, but instead comes out of the cpu box itself. its all so strange.

my questions are:

  1. How can you print the \ if its even possible to do.

and

  1. Why does my computer beep?

  2. Why do C programs need the int main() and the arduino sketches don’t seem to need it? I’m assuming they need void loop() instead?

1) you can print it by escaping the escape character like this : \ 2) \a is short for alarm [or, atleast that's how I remember it, maybe I've made it up in my head...] 3) The arduino sketch does not need one because it is already defined behind the scenes. Ant it is needed because it's the only place the computer 'knows' where to begin. It needs a known start to get started.

Great stuff. I see you're digging right in.

yeah, print formatting and regex (regular expression) stuff can be a real pain... in fact, there's an entire programming language (ok, several) dedicated to just that function.. PERL, the Practical Extraction and Reporting Language. Don't get too bogged down in that stuff at the moment if it gets confusing, even pros have to pull out their regex references at times. Don't bother trying to memorize all of them right away.

Flow control, variable manipulation, type conversion, and looping- those are probably the most often used functions.. nail those, and the rest are cake.

My first c program…!

…success and failure.

I heard somewhere, probably untrue, that chess was created for a king and the creator was rewarded by receiving one grain of rice for the first square and that doubled and was totaled all the way up to the 64th square…starts out small but its a LOT of rice.

The origination of chess is not my concern. I am curious to know just how much rice that would be. My puny brain can only think up to square 16 before i lose track and get a head ache. I’m not of mensa stock, sue me.

so i wrote a program to do it for me. unfortunately it craps out about 31 squares into it. How can i coax my computer into producing more and larger numbers?


#include <stdio.h>

int main()

{
unsigned long total = 1;
int b = 65;
unsigned long rice = 1;
int square = 1;

while(square<b)
{
printf(“Square %d has %d grains of rice for a total of %d\n”,square, rice, total);

square = square+1;
rice = rice+rice;
total = total+rice;
}

return(0);
}

unfortunately you might be out of luck. i think you will need a “octaword” to go anywhere above 2^31. I have no experience dealing with those on an arduino or even in C.

double total; float rice;
// %f for printing, maybe

Which should give you an inexact but "close" result, or you could do:

unsigned long long total, rice;
// (I don't know if there is printf support for long long.)

(is this to run on an arduino, or some desktop machine?)

I just wanted to run this on my desktop in command prompt.

I'm trying to learn some C so i can understand how to use the arduino. So far its helping me to understand a bit of the arduino code by just getting how things are put together. I may not be able to write a program yet, but i can kind of read them to a point. I'm not even half way done with my c book yet tho. I will try the long long command and see if it works.....Thanks !

Before you jump into trying to use the long long type, spend some time re-reading earlier parts of the book where it talked about basic types, and the range of values that can be stored in each type. For the Arduino, you can click on each of the types here: http://arduino.cc/en/Reference/HomePage You'll see, from the information here, and the output of your program, why it quit after 31 squares.

Thanks Paul,

I see that the number just got too large for the unsigned long to handle. Is there any way around that???

I see that the number just got too large for the unsigned long to handle. Is there any way around that???

Unsigned long is the largest native integer data type on the Arduino.

Real data types, like float, can hold larger values, but not with the same level of accuracy.

Of course, you are not limited to native integer types. One of the standard C exercises has students implementing 64 bit or 128 bit integers on platforms that do not support them.

Non-trivial, but not all that difficult. Not a beginner task, though.

Actually, the avr-gcc documentation claims that there is support for 64bit long long integers.

You probably need your own printing function, though... An interesting exercise.

Big numbers are fun. I vaguely recall the very first program I ever wrote, which was something like:

10 let a = 2
20 let a = a ^ a
30 print a
40 goto 20

I didn’t actually have the background to understand why it “crashed” so quickly, but it was pretty neat, nevertheless…

This seems to work on Arduino. The 64bit math functions are pretty big; this compiles to over 10k of code:

void setup()
{
  Serial.begin(9600);
}

void printll(unsigned long long n)
{
  char output[30];
  byte d=0;
  while (n > 0) {
    output[d++] = '0' + (char)(n % 10ULL);
    n = n / 10ULL;
  }
  while (d-- > 0) { //reverse digits and print
    Serial.print(output[d]);
  }
}

void loop()
{
  unsigned long long total = 1;
  char b = 65;
  unsigned long long rice = 1;
  byte square = 1;
  
  while(square<b)
  {
    Serial.print("Square, grains total: ");
    Serial.print(square, DEC);
    Serial.print(", ");
    printll(rice);
    Serial.print(", ");
    printll(total);
    Serial.println("");
    square = square+1;
    rice = rice+rice;
    total = total+rice;
  }

  while (1) ;  //halt
}