Переключение светодиодов Arduino: несколько методов с примерами кода

Чтобы переключить светодиод с помощью Arduino, вы можете использовать различные методы. Вот несколько примеров кода:

Метод 1: использование digitalWrite():

const int ledPin = 13; // Define the pin connected to the LED
void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
  digitalWrite(ledPin, HIGH); // Turn on the LED
  delay(1000); // Wait for 1 second
  digitalWrite(ledPin, LOW); // Turn off the LED
  delay(1000); // Wait for 1 second
}

Метод 2: использование прямого манипулирования портом (быстрее, чем digitalWrite()):

const int ledPin = 13; // Define the pin connected to the LED
void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
  PORTB |= (1 << PORTB5); // Turn on the LED (assuming it's connected to pin 13)
  delay(1000); // Wait for 1 second
  PORTB &= ~(1 << PORTB5); // Turn off the LED
  delay(1000); // Wait for 1 second
}

Метод 3. Использование битовых манипуляций с регистрами:

const int ledPin = 13; // Define the pin connected to the LED
void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
  digitalWriteFast(ledPin, HIGH); // Turn on the LED
  delay(1000); // Wait for 1 second
  digitalWriteFast(ledPin, LOW); // Turn off the LED
  delay(1000); // Wait for 1 second
}

в методе 3, вам необходимо подключить библиотеку FastGPIO.