Download presentation
Presentation is loading. Please wait.
1
Setting up a basic program with Arduino
Variable Declarations Program Setup References: Exploring Arduino – by Jeremy Blum Arduino Project Book – by Arduino
2
Single line comments denoted by //
Understanding Code Basics Comments: Single line Single line comments denoted by // Compiler ignores everything after the symbol Comment out a line to troubleshoot sval = sval / 5; // average sval = sval / 4; // scale to 8 bits ( ) sval = 255 - sval; // invert output Multiple line Multiple line comments start with /* and end with*/ Compiler ignores everything between the symbols Used for large descriptions and program details /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. (Another valid comment) */
3
Variable Declarations:
Understanding Code Basics Variable Declarations: Variables are a place in the Arduinos memory that holds information int –Primary for number storage void loop() { int i = 2; int j = 3; int k; k = myMultiplyFunction(i, j); // k now contains 6 Serial.println(k); char – Stores a character value: ex ‘A’ . Characters can be stored as numbers. Example Code char myChar = 'A’; char myChar = 65; // both are equivalent
4
Variable Declarations:
Understanding Code Basics Variable Declarations: byte –Stores an unassigned 8-bit number from 0-255 byte x; x = 0; x = x - 1; // x now contains rolls over in neg. direction x = 255; x = x + 1; // x now contains 0 - rolls over long –Extended size variables for number storage, store 32 bits (4 bytes) from -2,147,483,648 to 2,147,483,647 Example Code long speedOfLight = L; // see the Integer Constants page for explanation of the 'L'
5
Variable Declarations:
Understanding Code Basics Variable Declarations: float –Used for numbers that have decimal numbers Example Code float myfloat; float sensorCalbrate = 1.117; int x; int y; float z; x = 1; y = x / 2; // y now contains 0, ints can't hold fractions z = (float)x / 2.0; // z now contains .5 (you have to use 2.0, not 2)
6
Variable Declarations:
Understanding Code Basics Variable Declarations: Double –Double precision floating point number. On the Uno and other ATMEGA based boards, this occupies 4 bytes. That is, the double implementation is exactly the same as the float, with no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision. If you borrow code from other sources that includes double variables may wish to examine the code to see if the implied precision is different from that actually achieved on ATMEGA based Arduinos.
7
Variable Declarations:
Understanding Code Basics Variable Declarations: string – In general, used to represent text char Str1[15]; char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'}; char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'}; char Str4[ ] = "arduino"; char Str5[8] = "arduino"; char Str6[15] = "arduino"; Possibilities for declaring strings Declare an array of chars without initializing it as in Str1 Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2 Explicitly add the null character, Str3 Initialize with a string constant in quotation marks; the compiler will size the array to fit the string constant and a terminating null character, Str4 Initialize the array with an explicit size and string constant, Str5 Initialize the array, leaving extra space for a larger string, Str6
8
void setup – Used at the beginning of the program. It is where
Understanding Code Basics Program Setup: void setup – Used at the beginning of the program. It is where variables are initialized, pins are defined as inputs or outputs. Executes what is between the { } (curly braces) Only runs once. When the Arduino is started or reset. void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
9
void loop – Runs continuously after the setup. This is where
Understanding Code Basics Program Setup: void loop – Runs continuously after the setup. This is where inputs and outputs are turned on, motors controlled, sensors are Read. Executes what is between the { } (curly braces) void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
10
pinMode– Configures a specific pin number to be an input or an output.
Understanding Code Basics Program Setup: pinMode– Configures a specific pin number to be an input or an output. void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a second digitalWrite(13, LOW); // sets the digital pin 13 off }
11
digitalRead– Reads the value from a specified pin
Understanding Code Basics Program Setup: digitalRead– Reads the value from a specified pin int ledPin = 13; // LED connected to digital pin 13 int inPin = 7; // pushbutton connected to digital pin 7 int val = 0; // variable to store the read value void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output pinMode(inPin, INPUT); // sets the digital pin 7 as input } void loop() val = digitalRead(inPin); // read the input pin digitalWrite(ledPin, val); // sets the LED to the button's value
12
digitalWrite– Writes a HIGH or Low value to a specific pin
Understanding Code Basics Program Setup: digitalWrite– Writes a HIGH or Low value to a specific pin void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a second digitalWrite(13, LOW); // sets the digital pin 13 off }
13
delay– A delay (wait) time in milliseconds. The Arduino does
Understanding Code Basics Program Setup: delay– A delay (wait) time in milliseconds. The Arduino does Nothing during this specified amount of time. void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a second digitalWrite(13, LOW); // sets the digital pin 13 off }
14
if– checks for a condition and executes the proceeding statement
Understanding Code Basics Program Setup: if– checks for a condition and executes the proceeding statement if the condition was found to be true. else– follows the “if” and is for when the “if” condition is found to be false. if (condition1) { // do Thing A } else if (condition2) // do Thing B else { // do Thing C if (temperature >= 70) { //Danger! Shut down the system } else if (temperature >= 60 && temperature < 70) //Warning! User attention required } else //Safe! Continue usual tasks... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.