Advanced I/O (Tone Output and Shift I/O etc.)

This library is used to generate a square wave, input/output serial data and read a pulse.

tone

Description

Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. This is useful for playing sounds with a buzzer.

Syntax

tone(int pin, int frequency, int duration)

Parameters

pin: The pin on which to generate the tone
frequency: The frequency of the tone [Hz]
duration: The duration of the tone in milliseconds (optional)

Returns

Nothing

noTone

Description

Stops the generation of a square wave triggered by tone(). If you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.

Syntax

noTone(int pin)

Parameters

pin: The pin on which to stop generating the tone

Returns

Nothing

shiftOut

Description

Shifts out a byte of data one bit at a time. Starts from either the most (the leftmost) or least (the rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available. This is a software implementation; see also the SPI library, which provides a hardware implementation that is faster but works only on specific pins.

Syntax

shiftOut(int dataPin, int clockPin, bitOrder, unsigned char value)

Parameters

dataPin: The pin on which to output each bit
clockPin: The pin to toggle once the dataPin has been set to the correct value
bitOrder: Which order to shift out the bits; either MSBFIRST or LSBFIRST
value: The data to shift out (8-bit data)

Returns

Nothing

shiftIn

Description

Shifts in a byte of data one bit at a time. Starts from either the most (the leftmost) or least (the rightmost) significant bit. For each bit, the clock pin is pulled high, the next bit is read from the data line, and then the clock pin is taken low. This is a software implementation.

Syntax

unsigned char shiftIn(unsigned char dataPin, unsigned char clockPin, bitOrder)

Parameters

dataPin: The pin on which to input each bit
clockPin: The pin to toggle to signal a read from the dataPin
bitOrder: Which order to shift in the bits; either MSBFIRST or LSBFIRST

Returns

The value read (8-bit data)

shiftOutEx

Description

Shifts out multiple bytes (up to 32 bits) of data one bit at a time. Starts from either the most (the leftmost) or least (the rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available. This is a software implementation.

Syntax

shiftOutEx(int dataPin, int clockPin, bitOrder, int len, unsigned long value)

Parameters

dataPin: The pin on which to output each bit
clockPin: The pin to toggle once the dataPin has been set to the correct value
bitOrder: Which order to shift out the bits; either MSBFIRST or LSBFIRST
len: Number of output bits
value: The data to shift out (32 bits or less. In the case of MSBFIRST, the most significant bit should be filled.)

Returns

Nothing

pulseIn

Description

Reads a pulse (either HIGH or LOW) on a pin. For example, if the value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out.

Syntax

unsigned long pulseIn(int pin, int val, unsigned long timeout)

Parameters

pin: The number of the pin on which you want to read the pulse
val: Type of pulse to read: either HIGH or LOW
timeout: The number of microseconds to wait for the pulse to start; default is one second (option)

Returns

The length of the pulse (in microseconds) or 0 if no pulse started before the timeout


Example

Melody using tone.


        #include <Arduino.h>
        void setup(){
         
        }
         
        void loop(){
            tone(3, 523); // C4
            delay(200);
            noTone(3);  
            tone(3, 587); // D4
            delay(200);
            noTone(3);  
            tone(3, 659); // E4
            delay(400);
            noTone(3);  
        }