FUNCTIONS Flashcards
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));