Robotics Notes
Robotics Notes
Content:
• Arrays:
o Types
▪ int
▪ char
o How to make and read
o Specifying
• Analog Pins
• Functions:
o const
o for
o analogRead
o analogWrite
o map
o if
Array
Arrays in Arduino are collections of variables that can be accessed using an index.
It has multiple types such as int arrays for integer variables and char arrays for characters.
Sample:
int sundae [4] = {2,3,4,5}
• array name
o sundae (can be anything)
• []
o index, it can be empty and it is the total number of elements
• {}
o content or elements of the array
o the counting of the elements starts from zero.
▪ Ex.
• No. 0 will be element 2, and number 3 will be element 5.
Arrays can be specified by placing the no. of the element into the index and not
placing a range.
Sample:
sundae[1];
• This will specify element 3 as it is counted as element number 1.
Analog Pins
These are the pins (A0 to A5) that are responsible for reading analog signals that
come from sensors. To set them up so that they can be read by the program is like so:
pinMode (A4, INPUT);
Functions
const function
Constant or const, defines variables as unchangeable.
for function
Can be used to specify a purpose for a series of pins.
For example:
Code: Chocolate
You have 6 LEDs that you want to blink in a running function, so if you want to
code it in a running function it will be like this: Code: Vanilla
for (int x = 0; x <= 6; x++){ digitalWrite (1,HIGH);
digitalWrite(x,HIGH); digitalWrite (1,LOW);
delay(100); delay(100);
digitalWrite(x,LOW); digitalWrite (2,HIGH);
delay(100); digitalWrite (2,LOW);
} delay(100);
• Variable name digitalWrite (3,HIGH);
o x digitalWrite (3,LOW);
o Default is 0 delay(100);
• <= digitalWrite (4,HIGH);
o Specifies pins starting from zero and any digitalWrite (4,LOW);
pin less than or equal to 6. Basically, it delay(100);
counts 0,1,2,3,4,5,6 pins. digitalWrite (5,HIGH);
• x++ digitalWrite (5,LOW);
o It means increments of 1. delay(100);
• You can also replace the pins you set with an digitalWrite (6,HIGH);
array. For example, we use the array sundae it digitalWrite (6,LOW);
will be like this: delay(100);
o for (int x = 0; x < sundae; x++){ Code chocolate is the
digitalWrite(x,HIGH); same as code vanilla
delay(100); just condensed.
digitalWrite(x,LOW);
delay(100);
}
analogRead function
Acts as input and reads the value of a specified analog pin. Arduino boards (A0 to
A5 pins) have a 10-bit analog-to-digital converter which means it will input voltages
between 0 and the operating voltage into integer values ranging from 0 and 1023.
analogWrite function
Acts as output and reads values 0 to 255.
map function
It converts values from 1 range to another. The map value can also be analogWrite
map (variable name, from low, from high, to low, to high);
Sample:
int anginit;
int ledlevel = map (anginit, 0, 1023, 0, 255);
• variable name – anginit
• from range - 0 and 1023
• to range (converted range) - 0 and 255
if function
It checks the conditions set if true.