Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
80 views

Arduino Functions and Commands

The document discusses various Arduino functions and commands including while loops, arrays, string arrays, string variables, string literals, and provides a sample code for a basic program to turn on an LED while a button is pressed. It defines each concept, provides examples of code using them, and references sources for further information.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Arduino Functions and Commands

The document discusses various Arduino functions and commands including while loops, arrays, string arrays, string variables, string literals, and provides a sample code for a basic program to turn on an LED while a button is pressed. It defines each concept, provides examples of code using them, and references sources for further information.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Arduino Functions and Commands

1. Make a research of the following functions and commands;


a. While -
i. loop continuously, and infinitely, until the expression inside the
parenthesis, () becomes false. Something must change the tested
variable, or the while loop will never exit. This could be in your code, such
as an incremented variable, or an external condition, such as testing a
sensor.

var = 0;
while (var < 200) {
// do something repetitive 200 times
var++;
}

b. Array
i. consecutive group of memory locations that are of the same type. To refer
to a particular location or element in the array, we specify the name of the
array and the position number of the particular element in the array.

int n[ 10 ] ; // n is an array of 10 integers

void setup () {

void loop () {
for ( int i = 0; i < 10; ++i ) // initialize elements of array n to 0 {
n[ i ] = 0; // set element at location i to 0
Serial.print (i) ;
Serial.print (‘\r’) ;
}
for ( int j = 0; j < 10; ++j ) // output each array element's value {
Serial.print (n[j]) ;
Serial.print (‘\r’) ;
}
}

c. String Array -
i. The string is a data type that stores text rather than the integer values.
ii. The string is an array of characters or a set of characters that consists of
numbers, spaces, and special characters from the ASCII table.

char *StrAB[] = {"Welcome to string A to G in Arduino", "Here is string A", "Here is


string B", "Here is string C", "Here is string D", "Here is string E",

"Here is string F", "Here is string G" };

void setup() {

Serial.begin(9600);

void loop() {

for (int i = 0; i < 8; i++)

Serial.println(StrAB[i]);

delay(1000);

d. String Variables
i. are typically stored as arrays of chars, terminated by a null byte

char s1[ ] = { 'h', 'e', 'l', 'l', 'o', '\0' };

char s2[ ] = "hello";

char s3[ 20 ] = "hello";

char s4[ 5 ] = "hello";

char s5[ 3 ] = "hello";

e. String Literals
i. string of characters enclosed in double quotes, such as "To err is human
- To really foul things up requires a computer."
ii. are stored in C as an array of chars, terminted by a null byte
iii. are stored in C as an array of chars, terminted by a null byte
iv. may contain any valid characters, including escape sequences such as
\n, \t, etc. Octal and hexadecimal escape sequences are technically legal
in string literals, but not as commonly used as they are in character
constants, and have some potential problems of running on into following
text.

printf( "This will


print over three
lines, ( and will include extra tabs or spaces. )" );

Include the definition and function of each and a sample code. Don’t forget to
include the reference at the bottom of the page.

2. Make a research of a basic code for creating/coding a program of “Turn on LED while the
button is pressed”.(Full program code must be provided.)

const int button = 8;

const int led =7;

int ledflag=0;

void setup() {

pinMode(button,INPUT);

pinMode(led,OUTPUT);

digitalWrite(led,LOW);

void loop() {

if (digitalRead(button)==HIGH){
if (ledflag==0) {

ledflag=1;

digitalWrite(led,HIGH);

else {

ledflag=0;

digitalWrite(led,LOW);

delay(1000);

******Don’t forget to make a copy first. Do not edit the file. Thank you*******

Sources:

While. while - Arduino Reference. (n.d.). Retrieved September 5, 2022, from


https://www.arduino.cc/reference/en/language/structure/control-structure/while/

While. while - Arduino Reference. (n.d.). Retrieved September 5, 2022, from


https://www.arduino.cc/reference/en/language/structure/control-structure/while/

Arduino string - javatpoint. www.javatpoint.com. (n.d.). Retrieved September 5, 2022, from


https://www.javatpoint.com/arduino-string#:~:text=The%20string%20is%20a%20data,ch
aracters%20from%20the%20ASCII%20table.

Introduction to C / C++ programming character strings. C Programming Course Notes -


Character Strings. (n.d.). Retrieved September 5, 2022, from
https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/CharacterStrings.html#:~:tex
t=String%20Literals,terminted%20by%20a%20null%20byte
Simple on/off pushbutton. Arduino Project Hub. (n.d.). Retrieved September 5, 2022, from
https://create.arduino.cc/projecthub/reverendfuzzy/simple-on-off-pushbutton-f637a7

You might also like