• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Arduino UNO , przyciski i diody
#11
Więc tak wpisałem ten kod lecz nie wiem czy dobrze to zrobiłem bo wywala mi błędy 
Kod:
int buttonPin1 = 2; // Start button
int buttonPin2 = 3; //button
int buttonPin3 = 4;
int buttonPin4 = 5;
int greenLedPin = 10; // green led start status int
int redLedPin = 11; //red led stop status
int motorPin = 12; // the motor's pin
int motorPin1 = 13;
int buttonStatus1 = 0;
int buttonStatus2 = 0;
int buttonStatus3 = 0;
int buttonStatus4 = 0;

void setup() {
pinMode(motorPin, OUTPUT);
pinMode(motorPin1 ,OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3 ,INPUT_PULLUP);
pinMode(buttonPin4 ,INPUT_PULLUP);
digitalWrite(redLedPin, LOW);//Set the red led initially to High
digitalWrite(motorPin, LOW);//Set the motorpin initially to LOW
digitalWrite(motorPin1 ,LOW);
digitalWrite(greenLedPin ,LOW);

}

void loop() {
 
buttonStatus1 = !digitalRead(buttonPin1);
buttonStatus2 = !digitalRead(buttonPin2);
buttonStatus3 = !digitalRead(buttonPin3);
buttonStatus4 = !digitalRead(buttonPin4);

  if (buttonStatus1 == HIGH && buttonStatus2 == LOW) { // if the start button is pressed (AND stop button not)
    unsigned long newTime;
unsigned long oldTime;
  }
void loop (){
newTime = millis();
if(newTime - oldTime >= 1000){
  digitalWrite(motorPin, HIGH); // turn the motor ON
digitalWrite(greenLedPin, HIGH); //turn the green led indicator ON
digitalWrite(redLedPin, LOW); //turn the red led indicator O

oldTime = newTime;
}
}
   
digitalWrite(motorPin, HIGH); // turn the motor ON
digitalWrite(greenLedPin, HIGH); //turn the green led indicator ON
digitalWrite(redLedPin, LOW); //turn the red led indicator OFF
}
 
  if (buttonStatus1 == LOW && buttonStatus2 == HIGH) { // if stop button is pressed (AND the start off)
digitalWrite(motorPin, LOW); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, HIGH); //turn the red led indicator ON
}
if (buttonStatus2 == HIGH && buttonStatus3 == LOW) { // if stop button is pressed (AND the start off)

digitalWrite(motorPin1, HIGH); // turn the motor OFF
digitalWrite(greenLedPin, HIGH); //turn the green led indicator OFF
digitalWrite(redLedPin, LOW); //turn the red led indicator O
}
  if (buttonStatus2 == LOW && buttonStatus3 == HIGH) { // if stop button is pressed (AND the start off)
digitalWrite(motorPin1, LOW); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, HIGH); //turn the red led indicator O
}

  if (buttonStatus4 == HIGH && buttonStatus3 == LOW) { // if stop button is pressed (AND the start off)
digitalWrite(motorPin1, LOW); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, LOW); //turn the red led indicator O
}
  if (buttonStatus4 == HIGH && buttonStatus2 == LOW) { // if stop button is pressed (AND the start off)
   
digitalWrite(motorPin, HIGH); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, LOW); //turn the red led indicator O
}
}
 
Odpowiedź
#12
Dwa razy funkcje loop masz, nie kopiuj z innych programów tak urywkami jak nie jesteś pewnien co kopiujesz, na pewno w jednym loopie musisz zamknąć wszystko chyba ze tworzysz odzielna funkcje o innej nazwie i potem ja przywołujesz. Kolejna sprawa to zmienne oldTime i newTime przerzuć na początek programu przed setup a nie w loop bo źle będzie Ci program działać mianowicie za każdym przejściem loop będzie tworzyć te zmienne a nic do nich nie przypisujesz więc zapewne w części będzie ciągle 0 i nie będzie odliczać dobrze czasu. Popraw te dwie rzeczy i podeślij kod wraz z błędem kotry pokazuje kompilator
 
Odpowiedź
#13
Nie mam pojęcia czy dobrze wpisałem te dwie rzeczy poniżej kod oraz błędy

Błędy


sterownik27032022:61:3: error: expected unqualified-id before 'if'

  if (buttonStatus1 == LOW && buttonStatus2 == HIGH) { // if stop button is pressed (AND the start off)

  ^~

sterownik27032022:66:2: error: expected unqualified-id before 'if'

  if (buttonStatus2 == HIGH && buttonStatus3 == LOW) { // if stop button is pressed (AND the start off)

  ^~

sterownik27032022:72:3: error: expected unqualified-id before 'if'

  if (buttonStatus2 == LOW && buttonStatus3 == HIGH) { // if stop button is pressed (AND the start off)

  ^~

sterownik27032022:78:3: error: expected unqualified-id before 'if'

  if (buttonStatus4 == HIGH && buttonStatus3 == LOW) { // if stop button is pressed (AND the start off)

  ^~

sterownik27032022:83:3: error: expected unqualified-id before 'if'

  if (buttonStatus4 == HIGH && buttonStatus2 == LOW) { // if stop button is pressed (AND the start off)

  ^~

sterownik27032022:89:1: error: expected declaration before '}' token

}

^

exit status 1

expected unqualified-id before 'if'
Kod:
int buttonPin1 = 2; // Start button
int buttonPin2 = 3; //button
int buttonPin3 = 4;
int buttonPin4 = 5;
int greenLedPin = 10; // green led start status int
int redLedPin = 11; //red led stop status
int motorPin = 12; // the motor's pin
int motorPin1 = 13;
int buttonStatus1 = 0;
int buttonStatus2 = 0;
int buttonStatus3 = 0;
int buttonStatus4 = 0;

unsigned long newTime;
unsigned long oldTime;

void setup() {
pinMode(motorPin, OUTPUT);
pinMode(motorPin1 ,OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3 ,INPUT_PULLUP);
pinMode(buttonPin4 ,INPUT_PULLUP);
digitalWrite(redLedPin, LOW);//Set the red led initially to High
digitalWrite(motorPin, LOW);//Set the motorpin initially to LOW
digitalWrite(motorPin1 ,LOW);
digitalWrite(greenLedPin ,LOW);

}

void loop() {
 
buttonStatus1 = !digitalRead(buttonPin1);
buttonStatus2 = !digitalRead(buttonPin2);
buttonStatus3 = !digitalRead(buttonPin3);
buttonStatus4 = !digitalRead(buttonPin4);

  if (buttonStatus1 == HIGH && buttonStatus2 == LOW) { // if the start button is pressed (AND stop button not)
   
  }

newTime = millis();
if(newTime - oldTime >= 1000){
 
  digitalWrite(motorPin, HIGH); // turn the motor ON
digitalWrite(greenLedPin, HIGH); //turn the green led indicator ON
digitalWrite(redLedPin, LOW); //turn the red led indicator O

oldTime = newTime;
}

   
digitalWrite(motorPin, HIGH); // turn the motor ON
digitalWrite(greenLedPin, HIGH); //turn the green led indicator ON
digitalWrite(redLedPin, LOW); //turn the red led indicator OFF
}
 
  if (buttonStatus1 == LOW && buttonStatus2 == HIGH) { // if stop button is pressed (AND the start off)
digitalWrite(motorPin, LOW); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, HIGH); //turn the red led indicator ON
}
if (buttonStatus2 == HIGH && buttonStatus3 == LOW) { // if stop button is pressed (AND the start off)
  delay(9000);
digitalWrite(motorPin1, HIGH); // turn the motor OFF
digitalWrite(greenLedPin, HIGH); //turn the green led indicator OFF
digitalWrite(redLedPin, LOW); //turn the red led indicator O
}
  if (buttonStatus2 == LOW && buttonStatus3 == HIGH) { // if stop button is pressed (AND the start off)
digitalWrite(motorPin1, LOW); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, HIGH); //turn the red led indicator O
}

  if (buttonStatus4 == HIGH && buttonStatus3 == LOW) { // if stop button is pressed (AND the start off)
digitalWrite(motorPin1, LOW); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, LOW); //turn the red led indicator O
}
  if (buttonStatus4 == HIGH && buttonStatus2 == LOW) { // if stop button is pressed (AND the start off)
    delay (3000);
digitalWrite(motorPin, HIGH); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, LOW); //turn the red led indicator O
}
}
 
Odpowiedź
#14
wiesz że i tak program będzie się blokował na kilka sekund w Twoich kilku linijkach delay?  trzeba użyć kilka razy tej funkcji z operacją newTime - oldTime z tym że inne zmienne old musisz użyć i bez żadnych delay wtedy program zawsze wychwyci włączenie przycisku.
Dobra nie będę za dużo pisał, dzisiaj mam chęci i czas aby coś napisać to poprawię Tobie kod aby działał tak jak powinien. Napisałem na pw aby nie spamować forum. A Ty w ramach nauki i rekompensaty Smile przeanalizuj kod pod kątem różnic i spróbuj wyciągnąć wnioski i wiedzy ile dasz rady aby wiedzieć na przyszłość
 
Odpowiedź
#15
Przed ifami, które wywalają błędy zdążyłeś już dać "}" kończący pętlę loop(). Stąd prawdopodobnie błąd.
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości