Помогите нужно написать код для arduino uno щоб, при кожному натиску на кнопку менялися диод, нужен код на 3 диода(led), схемка на фото, код на 2 диода тоже добавил но надо именно на 3 диода (рабочая)

Приложения:

Ответы

Ответ дал: Аноним
1

Відповідь:

Щоб написати код для Arduino Uno з трьома світлодіодами, які змінюються при натисканні кнопки, можна використати наступний код:

Код :

// constants

const int buttonPin = 2; // the pin that the button is connected to

const int led1Pin = 3;  // the pin that the first LED is connected to

const int led2Pin = 4;  // the pin that the second LED is connected to

const int led3Pin = 5;  // the pin that the third LED is connected to

// variables

int buttonState = 0; // the current state of the button

int ledState = 0;   // the current state of the LEDs

void setup() {

 // define input and output pins

 pinMode(buttonPin, INPUT);

 pinMode(led1Pin, OUTPUT);

 pinMode(led2Pin, OUTPUT);

 pinMode(led3Pin, OUTPUT);

}

void loop() {

 // read the state of the button

 buttonState = digitalRead(buttonPin);

 // check if the button is pressed

 if (buttonState == HIGH) {

   // increment the LED state

   ledState++;

   // cycle through the three states

   if (ledState > 2) {

     ledState = 0;

   }

   // set the LED pins to the appropriate state

   switch (ledState) {

     case 0:

       digitalWrite(led1Pin, LOW);

       digitalWrite(led2Pin, LOW);

       digitalWrite(led3Pin, LOW);

       break;

     case 1:

       digitalWrite(led1Pin, HIGH);

       digitalWrite(led2Pin, LOW);

       digitalWrite(led3Pin, LOW);

       break;

     case 2:

       digitalWrite(led1Pin, HIGH);

       digitalWrite(led2Pin, HIGH);

       digitalWrite(led3Pin, LOW);

       break;

   }

 }

}

Я надіюся, що зміг допомогти, гарного дня!

Вас заинтересует