Constants and Methods PDF

Title Constants and Methods
Course Internet of Things - Arduino 
Institution Georgian College
Pages 3
File Size 37.3 KB
File Type PDF
Total Downloads 80
Total Views 158

Summary

Constants and Methods...


Description

Constants The Arduino language has the following constant variables that can be used throughout any Arduino application: true/false You can always use true and false just as they are. These are predefined, never change, and are always available: if(variable == true) { doSomething(); } else { doSomethingElse(); } HIGH/LOW These define the voltage level on a digital pin, either 5V or 0V. These make your code more readable: digitalWrite(13, HIGH); INPUT/OUTPUT These are constants used for setting pins that can be used either for output or for input: pinMode(11, OUTPUT); As you’ll notice, the OUTPUT constant is used to set the value of the pin using the pinMode() method. This seems like a good place to segue into the core methods of the Arduino language. Methods

Now you should learn about some of the methods of the Arduino language: pinMode(pinNumber, mode) Remember that the digital pins of the Arduino controller can be set to either input or output, which is to say that they’ll send values to a controller or receive values from a controller. Before we use a digital pin, though, we need to establish in which direction the information on that controller will be flowing. Usually you do this in the setup() method, because you’ll rarely need to do it more than once in an application. digitalWrite(value) This method sets a digital pin to HIGH if value is high or LOW if value is low, which is to say that it sends 5V or 0V through the pin. This can work only on pins that have been set to OUTPUT using pinMode(). For example: pinMode(11, OUTPUT); digitalWrite(11, HIGH); If the pinMode() method hasn’t been used to set the pin to be OUTPUT, then calling digitalWrite will have no effect. int digitalRead(pinNumber) This method reads the state of a pin that is in input mode. This can be either HIGH or LOW, that is, 5V or 0V, and no other value. The digitalRead method is used for reading buttons, switches, anything that has a simple on and off, and any control that returns a simple true or false, 1 or 0, or other type of binary value. It returns the value of the pin read as an integer.

analogRead(pinNumber) This reads the value of an analog pin, returning a range from 0 to 1,023, that represents the voltage being passed into the pin. As you’ll discover later, it is im- portant that any controllers you connect to your Arduino controller send analog signals in the range between 0 and 5V because higher values will not be read and could damage the board. This method returns an integer and so can be used as shown here: int analogVal = analogRead(11);...


Similar Free PDFs