FUNCTIONS Flashcards
(37 cards)
pinMode(pin, mode)
Purpose: Sets the mode of a specified pin (INPUT, OUTPUT).
Example:
pinMode(13, OUTPUT);
digitalWrite(pin, value)
Purpose: Write a HIGH or LOW value to a pin.
Example:
digitalWrite(13, HIGH);
digitalRead(pin)
Purpose: Reads the value from a specified digital pin.
Example:
int buttonState = digitalRead(7);
analogWrite(pin, value)
Purpose: Write an analog value (PWM wave) to a pin.
Example:
analogWrite(9, 128);
analogRead(pin)
Purpose: Reads the analog value from a pin.
Example:
int sensorValue = analogRead(A0);
delay(ms)
Purpose: Pauses the program for a specified time.
Example:
delay(1000);
millis()
Purpose: Returns the number of milliseconds since the board started.
Example:
unsigned long time = millis();
Serial.begin(baudrate)
Purpose: Initializes serial communication at a certain baud rate.
Example:
Serial.begin(9600);
Serial.print(data)
Purpose: Prints data to the serial monitor.
Example:
Serial.print(“Hello, world!”);
Serial.println(data)
Purpose: Prints data followed by a new line.
Example:
Serial.println(“Line 1”);
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
Purpose: Attaches an interrupt to a pin.
Example:
attachInterrupt(digitalPinToInterrupt(2), blink, RISING);
detachInterrupt(pin)
Purpose: Disables an interrupt attached to a pin.
Example:
detachInterrupt(digitalPinToInterrupt(2));
map(value, fromLow, fromHigh, toLow, toHigh)
Purpose: Re-maps a number from one range to another.
Example:
int output = map(analogRead(A0), 0, 1023, 0, 255);
constrain(value, min, max)
Purpose: Constrains a number to be within a range.
Example:
int val = constrain(analogRead(A0), 100, 900);
randomSeed(seed)
Purpose: Initializes the pseudo-random number generator.
Example:
randomSeed(analogRead(0));
random(min, max)
Purpose: Generates a random number between a given range.
Example:
int randNum = random(1, 10);
noTone(pin)
Purpose: Stops the tone on a pin.
Example:
noTone(8);
tone(pin, frequency)
Purpose: Generates a square wave of a certain frequency on a pin.
Example:
tone(8, 1000);
shiftOut(dataPin, clockPin, bitOrder, value)
Purpose: Shifts out a byte of data one bit at a time.
Example:
shiftOut(11, 12, MSBFIRST, 0xFF);
shiftIn(dataPin, clockPin, bitOrder)
Purpose: Shifts in a byte of data one bit at a time.
Example:
byte myData = shiftIn(11, 12, MSBFIRST);
pulseIn(pin, value)
Purpose: Reads a pulse (HIGH or LOW) on a pin.
Example:
unsigned long duration = pulseIn(7, HIGH);
min(x, y)
Purpose: Returns the smaller of two numbers.
Example:
int smaller = min(10, 20);
max(x, y)
Purpose: Returns the larger of two numbers.
Example:
int larger = max(10, 20);
abs(x)
Purpose: Returns the absolute value of a number.
Example:
int absolute = abs(-5);