INPUT AND OUTPUT FUNCTIONS
Arduino includes functions for handling input and output. You've already seen some of these in the example programs throughout the book.
pinMode(pin, mode)
Reconfigures a digital pin to behave either as an input or an output.
Example:
pinMode(7,INPUT); // turns pin 7 into an input
digitalWrite(pin, value)
Turns a digital pin either on or off. Pins must be explicitly made into an output using pinMode before digitalWrite will have any effect.
Example:
digitalWrite(8,HIGH); // turns on digital pin 8
int digitalRead(pin)
Reads the state of an input pin, returns HIGH if the pin senses some voltage or LOW if there is no voltage applied.
Example:
val = digitalRead(7); // reads pin 7 into val
int analogRead(pin)
Reads the voltage applied to an analog input pin and returns a number between 0 and 1023 that represents the voltages between 0 and 5 V.
Example:
val = analogRead(0); // reads analog input 0 into val
analogWrite(pin, value)
Changes the PWM rate on one of the pins marked PWM. pin may be 11,10, 9, 6, 5, 3. value may be a number between 0 and 255 that represents the scale between 0 and 5 V output voltage.
Example:
analogWrite(9,128); // Dim an LED on pin 9 to 50%
shiftOut(dataPin, clockPin, bitOrder, value)
Sends data to a shift register, devices that are used to expand the number of digital outputs. This protocol uses one pin for data and one for clock. bitOrder indicates the ordering of bytes (least significant or most significant) and value is the actual byte to be sent out. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access