BASIC to Arduino C. Below is a short list of common C and BASIC functions. It is designed to take a BASIC programmer and get him started in Arduino C.
This is of course only a short list of the most common functions. More will be added to this web site in time.
------------------- BASIC TO ARDUINO C -------------------
Comments / Remarks / Telling the compiler to ignore a line
Basic:
REM
or
“ ‘ ” (a single quote)
Examples:
rem x = 3
‘ Ignore this comment.
C Language: double forward slashes “//”
Examples:
// x = 3;
// Ignore this comment.
Or
A forward slash “/”, and an asterisks “*”, followed by text, followed by another asterisks “*” and another forward slash “/”.
/*
This is a series of comments
Another comment
And another
*/
------------------- BASIC TO ARDUINO C -------------------
Indication of End of a Line
Basic: At the end of a line of code no special punctuation is required.
Examples:
x = 3
y = 4
C Language: At the end of a line of code a semicolon “;” is required.
Examples:
x = 3;
y = 4;
------------------- BASIC TO ARDUINO C -------------------
Variables (“Variants” for BASIC)
BASIC: use a variable and the system takes care of what kind of variable it is based on how you use it these super variables are called “variants”.
Examples:
X = 5 rem X is an integer.
Y = 5/2 rem Y is a real number.
Z = TRUE rem Z is a boolean number (logically true or false).
C: All variables MUST be declared BEFORE they are used as to how they will be used.
int X; // x is an integer.
Float Y; // x is a “float” ( a number with a decimal point—a “floating point” number to be exact.)
Bool Z;
------------------- BASIC TO ARDUINO C -------------------
Mathematics (for the most part it’s the same)
Basic: x = y/3
C Language: x = y/3;
------------------- BASIC TO ARDUINO C -------------------
Assignments (for the most part it’s the same)
Basic: x = 3
C Language: x = 3;
------------------- BASIC TO ARDUINO C -------------------
Comparison
Basic:
If x = 3 then y = true
If x = 3 then
Print “done”
Print “for now”
Else
Print “not done”
Endif
Others:
Not equals: <>
Less than: <
Greater than: >
C Language:
If ( x ==3) y = true;
// no “then” needed in C. Also note the DOUBLE equal signs ==
If (x == 3 )
{
y = true;
z = true;
}
Else
a=10;
End if
Others:
Not equals: !=
Less than: <
Greater than: >
Warning: Beware of accidentally using the single equal sign (e.g. "if (x = 10)" is WRONG, but "if (x==10)" is RIGHT!). This won’t result in an error, but will not be right. In the C language, a single equal sign ALWAYS MEANS ASSIGNMENT. A DOUBLE equals is used for comparison.
------------------- BASIC TO ARDUINO C -------------------
Counting / incrementing / decrementing
Basic:
x = x + 1
x = x-1
C Language adding 1 to x:
x = x + 1;
x = x + +;
+=x
C Language subtracting 1 from x:
x = x - 1;
x = x --;
-=x
------------------- BASIC TO ARDUINO C -------------------
Loops
Basic “for” loop:
For x = 1 to 4
Print “hello”
C Language “for” loop :
For (x=1; x<5; x++)
{
Println “hello”
}
------------------- BASIC TO ARDUINO C -------------------
Printing
Basic:
print x
print “hello”
C Language:
println( x);
println “hello”;
------------------- BASIC TO ARDUINO C -------------------
Concatenation
Basic:
x = “hello”& “world”
C Language:
x = “hello”, “world”
------------------- BASIC TO ARDUINO C -------------------
Structure
The structure of a BASIC program differs in a few different ways than that of a C program. See the examples below.
Remember: BASIC has "subroutines" and C language has "functions". Both work about the same. In Basic you use "gosub" to call a subroutine. In C you just use the name of the function.
In BASIC you can make infinite loops with "goto" statements. In C (on the arduino) you use "loop".
Examples:
Basic:
X = 3
Here
// your looping code goes here
gosub mysub
Goto here
Mysub
Rem do some stuff
return
C Language:
// Note: The word “void” you see at the start of most functions means that a function does not return any values.
int buttonPin = 3; // You need to initialize your variables before doing anything else.
// Note: the function below called “setup” is a function used to initialize variables, pin modes, start using libraries, etc. It runs only once. The function called “loop” will loop forever.
void setup()
{
Serial.begin(9600); // start writing outputs to console window
// your setup code goes here
}
void loop() // this code will loop forever. Just what we need for robots.
{
// your looping code goes here
}
BASIC to Arduino C: Click HERE to go to the official Arduino web site.