int - spaghetti-on-a

advertisement
TOOLBAR
TABS
TEXT EDITOR
MESSAGE
CONSOLE
DISPLAY WINDOW
PLAY
STOP
NEW
OPEN
SAVE
EXPORT
STRUCTURE
A variable is a place to store a piece of data. It
has a name, a value, and a type.
STRUCTURE
A variable is a place to store a piece of data. It
has a name, a value, and a type.
The setup() Called once when the program is
started. Used to define initial enviroNment
properties such as screen size, background color,
loading images, etc. before the draw() begins
executing. Variables declared within setup() are not
accessible within other functions, includingdraw().
There can only be one setup() function for each
program and it should not be called again after it's
initial execution.
STRUCTURE
A variable is a place to store a piece of data. It
has a name, a value, and a type.
The setup() Called once when the program is
started. Used to define initial enviroNment
properties such as screen size, background color,
loading images, etc. before the draw() begins
executing. Variables declared within setup() are not
accessible within other functions, includingdraw().
There can only be one setup() function for each
program and it should not be called again after it's
initial execution.
Draw () Called directly after setup() and continuously
executes the lines of code contained inside its block
until the program is stopped or noLoop() is called.
The draw() function is called automatically and
should never be called explicitly. The number of
times draw() executes in each second may be
controlled with
the delay() and frameRate() functions.
*note the word void means the process does
not return a value
*note the () are used to insert parameters
SYNTAX
The curly brackets { and } denote the beginning
and the end of each process
SYNTAX
The curly brackets { and } denote the beginning
and the end of each process
Comments
Comments are ignored by the computer, but useful
for communicating complex procedures
// two forwards slashes denote comments
// all text on the same line will be ignored
/*
a forward slash followed by an asterisk allows for a
continuous
comment until the reverse
*/
SYNTAX
The curly brackets { and } denote the beginning
and the end of each process
Comments
Comments are ignored by the computer, but useful
for communicating complex procedures
// two forwards slashes denote comments
// all text on the same line will be ignored
/*
a forward slash followed by an asterisk allows for a
continuous
comment until the reverse
*/
;
Denotes the end of a statement and must be used
for all expressions
VARIABLES
Syntax
Expressions
int light = analogRead(0);
Data type
constant
operator
function
parameter
statement terminator
Variable Types
variables to hold data. Variables can be of different types:
whole numbers are referred to as integer variables, true/false data is
Referred to as Booleans, and factional numbers are floats
Name Conventions
When we declare a variable (which is a made
up name) we also need to tell what
type it is and (if needed) to give it an initial
value. We use the following format:
type name = value;
For example:
int thisClass = 24;
Variables usually start with lower case and
when we want to composite more than one
word, we use upper case for the next word.
This is also referred to as intercapping. For
example:
names or newNames or newPeopleNames
Caution: A name cannot start with a number,
contain an empty space or any
special characters except the underscore. For
example 1thing, x-y, or the plan is
invalid names, but thing1, x_y and the_plan
are valid names.
boolean, which is 1-bit long and can take values of either true or false
boolean isInside = false;
char, which is 16-bit long and therefore can store 216 (= 65,536) different
Characters
(assuming that each character corresponds to one number which is its ASCII code)
char firstLetter = ‘A’;
byte, which is an 8-bit element and therefore can store 28 (=256) different binary
patterns.
byte b = 20;
int, which is 32-bit long can define integer (whole) numbers
Integers can be as large as 2,147,483,647 and as low as -2,147,483,648
int number_of_squares = 25;
float, which is 32-bits long can define real numbers
Floating-point numbers can be as large as 3.40282347E+38 and as low as 3.40282347E+38.
float pi = 3.14;
Long, which is 64-bits long can define large integers (whole) numbers.
A long integer has a minimum value of -9,223,372,036,854,775,808 and a maximum value
of 9,223,372,036,854,775,807
long a = 100000;
•Arduino uses unsigned longs. Unlike standard longs unsigned longs are 32 bit and won't
store negative numbers, making their range from 0 to 4,294,967,295
Unsigned long = 100000;
color, which is a group of three numbers to define a color using red, green, and blue.
Each number is between 0 and 255.
color c = color(255, 0, 0);
Or
color r = red(pixel[i])
String, which is a collection of characters used for words and phrases
String myName = ”Jpnathan";
*NOTE
TYPE SIZE DESCRIPTION
boolean 1-bit True or false
char 16-bits Keyboard characters
byte 8-bits or 1-byte 0-255 numbers
int 32-bits Integer numbers
float 32-bits Real fractional numbers
color 4 bytes or 32-bytes Red, Green, Blue, and Transparency
String 64-bits Set of characters that form words
Cast
A variable of one type can be cast (i.e. converted) to another type. For example,
float dist = 3.5;
int x = int(dist);
the value of the float variable dist can be cast to an integer one x. After the
casting x will be 3 (i.e. the fractional or decimal part is omitted). The following
command allows casting between different types:
boolean(), int(), float(), str(), byte().
For example,
float dist = 3.5;
String s = str(dist);
will create the string value "3.5" (not the float number 3.5).
For booleans we usually start with the prefix
“is”. For example:
isLightOn or isItRaining
As an example of initializing variables and data let’s define
information about a circle.
The following types, variables, and initializations can be used:
String name = "MyCircle";
int location_x = 22;
int location_y = 56;
float radius = 4.5;
Boolean isNurbs = false;
GETTING STARTED
OPERATIONS
Arithmetic Operations
OPERATOR USE DESCRIPTION
+
op1 + op2
//Adds op1 and op2
op1 - op2
//Subtracts op2 from op1
*
op1 * op2
// Multiplies op1 by op2
/
op1 / op2
// Divides op1 by op2
% 1op1 % op2 //Computes the remainder of
dividing op1 by op2
The sequence in which the operations will be
executed follows the order
(,),*, /,%, +, postfix operators ( )
multiplicative * / %
additive + -
Shortcuts
x+=1 is equivalent to x=x+1 or
y/=z is equivalent to y=y/z.
OPERATOR USE EQUIVALENT TO
+=
-=
*=
/=
%=
op1 += op2
op1 -= op2
op1 *= op2
op1 /= op2
op1 %= op2
op1 = op1 + op2
op1 = op1 - op2
op1 = op1 * op2
op1 = op1 / op2
op1 = op1 % op2
Logical and relational operations/statements
Logical operations define the truthfulness of
something. For example the word if
represents a guess that needs to be tested. In
Processing if-statements have the
following format:
if( condition )
…;
else
…;
if (pinFiveInput < 500)
{
// do Thing A
}
else if (pinFiveInput >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
if(a==b)
// if a is equal tob
if(a!=b)
// if a is not equal to b
if(a>b)
// if a is greater than to b
if(a>=b)
// if a is greater than or equal to b
if(a<b)
// if a is
if(a<=b)
// if a is less than or equal to b
less than b
* Note we use ‘=‘ when assigning values as in char name = jonathan
To combine conditions, we use the AND and OR operators
represented by &&
and || symbols.
For example
if(a>b && a >c)
// if a is greater than b and a is greater than c
if(a>b || a >c) /
// if a is greater than b or a is greater than c
If(!a)
// if not a (a having been declared)
/
A loop is a repetition of statements. It allows
statements to be defined, modified,
or executed repeatedly until a termination
condition is met.
for(start condition; end condition; modification
step){
….;
}
For example a loop from 0 to 9 is:
for(int i=0; i<10; i=i+1){
println(i); // will printout the value of i
}
i=i+1
i++
i+=1
0123456789
The while statement continually executes a
block of statements while a condition
remains true. The syntax is:
while (expression) {
statement
}
The while statement evaluates expression, which must return a boolean value. If the expression
returns as true, then the while statement executes the statement(s) associated with it. The while
statement continues testing the expression and executing its block until the expression returns as
false.
For example, in the loop
int i=0;
while(i<10){
println(i); // will printout i
i++;
}
the result is:
0123456789
Two more commands are associated with loops: continue and break. The continue command skips
the current iteration of a loop. The break command will force to exit the loop. For example, consider
the following loop:
for(int i=0; i<10; i++){
if(i==5)continue;
if(i==8) break;
println(i); // will printout the value of i
}
The result will be 0123467.
The reason is that when i becomes 5 the rest of the
statements are skipped and when i becomes 8 the loop is forced to exit.
Two more commands are associated with loops: continue and break. The continue command skips
the current iteration of a loop. The break command will force to exit the loop. For example, consider
the following loop:
for(int i=0; i<10; i++){
if(i==5)continue;
if(i==8) break;
println(i); // will printout the value of i
}
The result will be 0123467.
The reason is that when i becomes 5 the rest of the
statements are skipped and when i becomes 8 the loop is forced to exit.
boolean
A boolean holds one of two values, true or false. (Each boolean variable
occupies one byte of memory.)
delay()
Description
Pauses the program for the amount of time (in
miliseconds) specified as parameter. (There are
1000 milliseconds in a second.)
Syntax
delay(ms)
Parameters
ms: the number of milliseconds to pause
(unsigned long)
Returns
nothing
simple FUNCTIONS
Normalizing, divides value by the maximum value within a set i.e. returns a value
Between 0.0 and 1.0
norm(value, low,high)
float x = norm(102.0, 0.0,255.0); // x = .4 (note once declared ‘x’ does not need to be
preceded by ‘float’
Once normalized values can be applied to a range using simple arithmetic
Range 0.0 to 255.0
X* 255.0
.4* 255.0 = 102.0
Constrain, constrains a value within a range
constrain(value, min, max)
float x = constrain(102.0, 0.0,100.0); // x = 100.0
map, re-maps one range of values to another range of values constrains a value
*note map will not constrain values
map(value, fromMin, fromMax,toMin,toMax)
float x = map(.4, 0.0,1.0, 0.0,255.0); // x = 102.0
ARRAY
An array is a list of data. It is possible to have an array of any
type of data. Each piece of data in an array is identified by
an index number representing its position in the array. The
first element in the array is [0], the second element is [1],
and so on. Arrays are similar to objects, so they must be
created with the keyword new. Every array has a
variable length which is an integer value for the total
number of elements in the array.
int[] numbers = new int[3];
numbers[0] = 90;
numbers[1] = 150;
numbers[2] = 30;
or
int[] numbers = {90,150,30};
ARRAY
VALUE
myData {
0
ARRAY
NAME
60
30
1
ELEMENT
NUMBER
90
2
120
3
150
4
180
5
210
6
}
Creating (Declaring) an Array
All of the methods below are valid ways to
create (declare) an array.
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello
Arrays are zero indexed
int myArray[10]={9,3,2,4,3,2,7,8,9,11};
// myArray[9] contains 11
// myArray[10] is invalid and contains
random information (other memory address)
Arrays are often manipulated inside for loops,
where the loop counter is used as the index for
each array element. For example, to print the
elements of an array over the serial port, you
could do something like this:
int i;
for (i = 0; i < 10; i = i + 1) {
Serial.println(myPins[i]);
CLASS
A class is a composite of data and methods (functions)
which may be instantiated as objects. The first letter of a
class name is usually uppercase to separate it from other
kinds of variables.
class ClassName {
statements }
Download