Поворотный энкодер для коробки конфет не работает - PullRequest
0 голосов
/ 20 марта 2020

Я написал этот код для коробки конфет с контролем времени. Время и день его открытия будут контролироваться в меню с поворотным регулятором, двумя кнопками и ЖК-дисплеем. При нажатии кнопки переключателя открывается меню. Он начинается с понедельника и прокрутки по лоткам, вы можете перейти ко вторнику и так далее. После воскресенья должно открыться временное меню. Там вы можете выбрать время включения и выключения. Дни могут быть да (1) или нет (0).

К сожалению, я не могу заставить вращающийся датчик работать. Я надеюсь, что кто-то может помочь. С уважением.

#include <Wire.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

//button Pins
#define B 8 // Button to unlock
#define L  5 //Lock/ Relay
#define B1 6 //Button for yes/ and unlock
#define B2 7 //Button for no
#define PUSHP 4 //SW to enter menu
#define PINA 3
#define PINB 2


//Led
#define LR 10 //Led Pin for Red
#define LG 11 //Led Pin for green

//integers to safe yes(1) or no(0)

int Monday = 1;
int Tuesday = 1;
int Wednesday = 1;
int Thursday = 1;
int Friday = 1;
int Saturday = 1;
int Sunday = 1;

char Weekday = Monday;

int onTime = 1;
int offTime = 24;

uint8_t B1val = digitalRead(B1);
uint8_t B2val = digitalRead(B2);
uint8_t SWval = digitalRead(PUSHP);
uint8_t Bval = digitalRead(B);

int counter = 0;
int aState;
int aLastState;

volatile boolean turned;   // rotary was turned
volatile boolean fired;    // knob was pushed
volatile boolean up;  // true when turned cw

void isr ()
{
  if (digitalRead (PINA))
    up = digitalRead (PINB);
  else
    up = !digitalRead (PINB);
  turned = true;
}  // end of isr

void setup()
{
  lcd.begin();
  Serial.begin(9600);
  setSyncProvider(RTC.get);
  aLastState = digitalRead(PINA);
  //set pinmodes

  pinMode (PINA, INPUT);
  pinMode (PINB, INPUT);
  pinMode (B, INPUT);
  digitalWrite(B, LOW);
  digitalWrite(B1, LOW);
  digitalWrite(B2, LOW);
  digitalWrite(PUSHP, LOW);
  pinMode(B1, INPUT);
  pinMode(B2, INPUT);
  pinMode(PUSHP, INPUT);
  // Turn on the backlight and print a message.

}

void loop() {
  aState = digitalRead(PINA);
  Serial.println(Weekday);
  uint8_t Bval = digitalRead(B);
  uint8_t B1val = digitalRead(B1);
  uint8_t B2val = digitalRead(B2);
  uint8_t SWval = digitalRead(PUSHP);
  tmElements_t tm;
  printDay();
  isr();
  delay(10);
  Serial.print(B1val);
  Serial.print(B2val);
  Serial.print(SWval);
  if (hour() >= onTime && hour() <= offTime) {
    analogWrite(LG, 50); //set light to 50
    Serial.println("green");
  }
  else {
    digitalWrite(L, LOW);
    analogWrite(LR, 50); //set light to 50

  }

  if (Bval == 1) { //trying to unlock
    Serial.println("unlock");
    if (hour() >= onTime && hour() <= offTime  && 'Weekday' == 1) { //flash green led
      digitalWrite(L, HIGH); //unlock Lock
      digitalWrite(LG, HIGH); //turn green Led on
      delay(200);
      analogWrite(LG, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LG, HIGH); //turn green Led on
      delay(200);
      analogWrite(LG, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LG, HIGH); //turn green Led on
      delay(200);
      analogWrite(LG, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LG, HIGH); //turn green Led on
      delay(200);
      analogWrite(LG, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LG, HIGH); //turn green Led on
      delay(200);
      analogWrite(LG, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LG, HIGH); //turn green Led on
      delay(200);
      analogWrite(LG, 50); //set light to 50 to not hurt your eyes
    }
    else { //flash red led
      Serial.println("unlock false");
      digitalWrite(L, LOW); //lock locked
      digitalWrite(LR, HIGH); //turn red Led on
      delay(200);
      analogWrite(LR, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LR, HIGH); //turn red Led on
      delay(200);
      analogWrite(LR, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LR, HIGH); //turn red Led on
      delay(200);
      analogWrite(LR, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LR, HIGH); //turn red Led on
      delay(200);
      analogWrite(LR, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LR, HIGH); //turn red Led on
      delay(200);
      analogWrite(LR, 50); //set light to 50 to not hurt your eyes
      delay(200);
      digitalWrite(LR, HIGH); //turn red Led on
      delay(200);
      analogWrite(LR, 50); //set light to 50 to not hurt your eyes
    }
  }
  if (hour() >= onTime && hour() <= offTime) {
    Serial.println("unlocked");
    analogWrite(LG, 50); //set light to 50
  }
  else {
    Serial.println("locked");
    analogWrite(LR, 50); //set light to 50
  }
  if (SWval == 1) { //to enter menu when rotary encoder switch clicked
    menu();
  }
  else {
    lcd.clear();
    lcd.noBacklight(); // turn off backlight
  }
}
void menu() {

  Serial.println("menu");
Monday:
  Serial.println("Monday");
  lcd.backlight(); //turn on lcd
  aState = digitalRead(PINA);
  lcd.print("     Monday"); //display print Monday
  while (1 == 1) {
    Serial.println("while");
    aState = digitalRead(PINA);
    B1val = digitalRead(B1);
    B2val = digitalRead(B2);
    SWval = digitalRead(PUSHP);
    Serial.print(B1val);
    Serial.print(B2val);
    Serial.print(SWval);
    if (B1val == 1) {
      break;
    }
    if (B2val == 1) {
      break;
    }
    if (SWval == 1) {
      break;
    }
  }
  if (B1val == HIGH) {
    Serial.println("Monday yes");
    Monday = 1;
    lcd.setCursor(0, 1);
    lcd.print("      Yes  ");
    delay(100);
    goto Monday;
  }
  else if (B2val == HIGH) {
    Serial.println("Monday no");
    Monday = 0;
    lcd.setCursor(0, 1);
    lcd.print("      No  ");
    delay(100);
    goto Monday;
  }
  if (turned)
  {
    if (up)
      goto Tuesday;

    else
      goto Monday;
  }

  delay(100);
Tuesday:
  Serial.println("Tuesday");

  aLastState = aState;
  aState = digitalRead(PINA);
  lcd.clear();
  lcd.print("    Tuesday"); //display print Tuesday
  while (1 == 1) {
    B1val = digitalRead(B1);
    B2val = digitalRead(B2);
    SWval = digitalRead(PUSHP);

    if (B1val == 1) {
      break;
    }
    if (B2val == 1) {
      break;
    }
    if (SWval == 1) {
      break;
    }
  }
  if (B1 == HIGH) {
    Serial.println("Tuesday yes");
    Tuesday = 1;
    lcd.setCursor(0, 1);
    lcd.print("      Yes  ");
    delay(100);
    goto Tuesday;
  }
  else if (B2 == HIGH) {
    Serial.println("Tuesday no");
    Tuesday = 0;
    lcd.setCursor(0, 1);
    lcd.print("      No  ");
    delay(100);
    goto Tuesday;
  }

  if (turned)
  {
    if (up)
      goto Wednesday;

    else

      goto Tuesday;
  }

  delay(100);
Wednesday:
  Serial.println("Wednesday");

  aLastState = aState;
  aState = digitalRead(PINA);
  lcd.clear();
  lcd.print("   Wednesday"); //display print
  while (1 == 1) {
    B1val = digitalRead(B1);
    B2val = digitalRead(B2);
    SWval = digitalRead(PUSHP);

    if (B1val == 1) {
      break;
    }
    if (B2val == 1) {
      break;
    }
    if (SWval == 1) {
      break;
    }
  }
  if (B1 == HIGH) {
    Serial.println("Wednesday yes");
    Wednesday = 1;
    lcd.setCursor(0, 1);
    lcd.print("      Yes  ");
    delay(100);
    goto Wednesday;
  }
  else if (B2 == HIGH) {
    Serial.println("Wednesday no");

    Wednesday = 0;
    lcd.setCursor(0, 1);
    lcd.print("      No  ");
    delay(100);
    goto Wednesday;
  }
  if (turned)
  {
    if (up)
      goto Thursday;

    else
      aLastState = aState;
    goto Wednesday;
  }

  delay(100);
Thursday:
  aLastState = aState;
  aState = digitalRead(PINA);
  lcd.clear();
  lcd.print("    Thursday"); //display print
  while (1 == 1) {
    B1val = digitalRead(B1);
    B2val = digitalRead(B2);
    SWval = digitalRead(PUSHP);

    if (B1val == 1) {
      break;
    }
    if (B2val == 1) {
      break;
    }
    if (SWval == 1) {
      break;
    }
  }
  if (B1 == HIGH) {
    Thursday = 1;
    lcd.setCursor(0, 1);
    lcd.print("      Yes  ");
    delay(100);
    goto Friday;
  }
  else if (B2 == HIGH) {
    Thursday = 0;
    lcd.setCursor(0, 1);
    lcd.print("      No  ");
    delay(100);
    goto Thursday;
  }
  if (turned)
  {
    if (up)
      goto Friday;

    else
      aLastState = aState;
    goto Thursday;
  }

  delay(100);
Friday:
  aLastState = aState;
  aState = digitalRead(PINA);
  lcd.clear();
  lcd.print("     Friday"); //display print
  while (1 == 1) {
    B1val = digitalRead(B1);
    B2val = digitalRead(B2);
    SWval = digitalRead(PUSHP);

    if (B1val == 1) {
      break;
    }
    if (B2val == 1) {
      break;
    }
    if (SWval == 1) {
      break;
    }
  }
  if (B1 == HIGH) {
    Friday = 1;
    lcd.setCursor(0, 1);
    lcd.print("      Yes  ");
    delay(100);
    goto Friday;
  }
  else if (B2 == HIGH) {
    Friday = 0;
    lcd.setCursor(0, 1);
    lcd.print("      No  ");
    delay(100);
    goto Friday;
  }
  if (turned)
  {
    if (up)
      goto Saturday;

    else
      aLastState = aState;
    goto Friday;

  }
  delay(100);
Saturday:
  aLastState = aState;
  aState = digitalRead(PINA);
  lcd.clear();
  lcd.print("    Saturday"); //display print
  while (1 == 1) {
    B1val = digitalRead(B1);
    B2val = digitalRead(B2);
    SWval = digitalRead(PUSHP);

    if (B1val == 1) {
      break;
    }
    if (B2val == 1) {
      break;
    }
    if (SWval == 1) {
      break;
    }
  }
  if (B1 == HIGH) {
    Saturday = 1;
    lcd.setCursor(0, 1);
    lcd.print("      Yes  ");
    delay(100);
    goto Saturday;
  }
  else if (B2 == HIGH) {
    Saturday = 0;
    lcd.setCursor(0, 1);
    lcd.print("      No  ");
    delay(100);
    goto Saturday;
  }
  if (turned)
  {
    if (up)
      goto Sunday;

    else
      aLastState = aState;
    goto Saturday;

  }
  delay(100);
Sunday:
  aLastState = aState;
  aState = digitalRead(PINA);
  lcd.clear();
  lcd.print("     Sunday"); //display print
  while (1 == 1) {
    B1val = digitalRead(B1);
    B2val = digitalRead(B2);
    SWval = digitalRead(PUSHP);

    if (B1val == 1) {
      break;
    }
    if (B2val == 1) {
      break;
    }
    if (SWval == 1) {
      break;
    }
  }
  if (B1 == HIGH) {
    Sunday = 1;
    lcd.setCursor(0, 1);
    lcd.print("      Yes  ");
    delay(100);
    goto Sunday;
  }
  else if (B2 == HIGH) {
    Sunday = 0;

    lcd.setCursor(0, 1);

    lcd.print("      No  ");
    delay(100);
    goto Sunday;
  }
  if (turned)
  {
    if (up)
      goto Time;

    else
      aLastState = aState;
    goto Saturday;

  }

Time:
  Serial.println("time");

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("turn on:  "); //display print
  lcd.setCursor(9, 0);
  lcd.print(onTime);
  lcd.setCursor(12, 0);
  lcd.print(":00");
  lcd.setCursor(0, 1); //set cursor
  lcd.print("turn off: ");
  lcd.setCursor(9, 1);
  lcd.print(offTime);
  lcd.setCursor(12, 1);
  lcd.print(":00");

OnTime:
  while (B1 != HIGH) { //accept on time
    if (B2 == HIGH) {
      while (B2 == HIGH) {}
      goto OffTime;
    }

    if (turned)
    {
      if (up)
        onTime + 1;
      else
        onTime - 1;
      aLastState = aState;
    }
    if ( onTime < 0) {
      onTime = 24;
    }
    if ( onTime > 24) {
      onTime = 0;
    }
    lcd.setCursor(9, 0);
    lcd.print(onTime);
    lcd.setCursor(12, 0);
    lcd.print(":00");
    //   lcd.setCursor(9, 1); //set cursor
    //   lcd.print(offTime);
    //   lcd.print(":00");

  }



OffTime:

  while (B2 != HIGH) { //accept off time
    if (B1 == HIGH) {
      while (B1 == HIGH) {}
      goto OnTime;
    }

    if (turned)
    {
      if (up) {
        offTime + 1;
        Serial.println(offTime);
      }
      else {
        offTime - 1;
      }
    }
    if ( offTime > 24) {
      offTime = 0;
    }
    if ( offTime < 0) {
      offTime = 24;
    }
    //      lcd.setCursor(9, 0);
    //   lcd.print(onTime);
    //   lcd.print(":00");

    lcd.setCursor(9, 1); //set cursor
    lcd.print(offTime);
    lcd.setCursor(12, 1);
    lcd.print(":00");
    delay(100);
    if (B1val == 1) {
      loop();
    }

  }

}




void printDay()
{
  int day;
  day = weekday();
  if (day == 1) {
    Weekday = Sunday;
  }
  if (day == 2) {
    Weekday = Monday;
  }
  if (day == 3) {
    Weekday = Tuesday;
  }
  if (day == 4) {
    Weekday = Wednesday;
  }
  if (day == 5) {
    Weekday = Thursday;
  }
  if (day == 6) {
    Weekday = Friday;
  }
  if (day == 7) {
    Weekday = Saturday;
  }
}```

1 Ответ

0 голосов
/ 20 марта 2020

В зависимости от типа кодировщика (с электроникой c на печатной плате) или кодировщика barebone, порты могут быть разными, НО
работа с кодировщиками означает работу с прерываниями И избавление от всех задержек () в вашем l oop и рутины. Вот тестовый код для поворотного энкодера, который работает на UNO на 100%:

    /* This code is without the push button function
    These pins can not be changed 2/3 are special pins -> Interrupt */
    const uint8_t encoderPin1 = 2;
    const uint8_t  encoderPin2 = 3;

    volatile int lastEncoded = 0;
    volatile long encoderValue = 0;
    long lastencoderValue = 0;
    uint8_t lastMSB = 0;
    uint8_t lastLSB = 0;

    void setup() {
      Serial.begin (9600);

      pinMode(encoderPin1, INPUT);
      pinMode(encoderPin2, INPUT);

      digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
      digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
      /* Call updateEncoder() when any high/low changed seen
      on interrupt 0 (pin 2), or interrupt 1 (pin 3)*/
      attachInterrupt(0, updateEncoder, CHANGE);
      attachInterrupt(1, updateEncoder, CHANGE);
    }

    void loop() {
      //Do stuff here
      Serial.println(encoderValue);
      delay(1000); //just here to slow down the output, and show it will work even during a delay
    }

    void updateEncoder() {
      int MSB = digitalRead(encoderPin1); //MSB = most significant bit
      int LSB = digitalRead(encoderPin2); //LSB = least significant bit
      int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
      int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
      if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
      if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
      lastEncoded = encoded; //store this value for next time
    }

Попробуйте его (поскольку вы также используете 2/3 вывод) и, если он работает, интегрируйте его в свой код.
Замените isr () на функции тестовой программы.
Если она не работает, у нас проблема с HW, тогда может пригодиться схема и марка энкодера.

...