02-01-2022, 04:15
Rozwiązałem problem w inny sposób, myślę że sposób jest bardziej praktyczny. Proszę o podpowiedz co robię źle gdyż program nie kompiluje się.
Przy jego pisaniu korzystałem z:
Kurs Arduino #6: Przyciski - Prawidłowe podłączenie i obsługa - YouTube
Przy jego pisaniu korzystałem z:
Kurs Arduino #6: Przyciski - Prawidłowe podłączenie i obsługa - YouTube
Kod:
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin1 = 12;
const int ledPin2 = 13;
bool state1 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int delayTime = 20;
void setup() {
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode (buttonPin1, INPUT_PULLUP);
pinMode (buttonPin2, INPUT_PULLUP);
}
void loop() {
if (isButtonPresed(buttonPin1)&&(ledPin2 == LOW)) { // jak przycisk-1 został wciśnięty i LED2 nie świeci to zapal LED1
digitalWrite(ledPin1 == HIGH);
}
if (isButtonPresed(buttonPin1)||(isButtonPresed(buttonPin2))) { // jeżeli został wciśnięty przycisk-1 lub przycisk-2 to zgaś LED1 i LED2
digitalWrite(ledPin1 == LOW);
digitalWrite(ledPin2 == LOW);
}
if (isButtonPresed(buttonPin2)&&(ledPin1 == LOW)) { // jak przycisk-2 został wciśnięty i LED1 nie świeci to zapal LED2
digitalWrite(ledPin2 == HIGH);
}
if (isButtonPresed(buttonPin1)||(isButtonPresed(buttonPin2))) { // jeżeli został wciśnięty przycisk-1 lub przycisk-2 to zgaś LED1 i LED2
digitalWrite(ledPin1 == LOW);
digitalWrite(ledPin2 == LOW);
}
bool isButtonPresed (int button) { // likwidacja drgania styków dla wszystkich przycisków
if ((digitalRead(button) == LOW) {
delay(delayTime);
if (digitalRead(button) == LOW) {
return true;
}
}
return false
}
}