简体   繁体   English

如何读取旋转编码器

[英]How to read rotary encoder

Hello I'm wondering why doesn't this code work, since the encoder is working 100% and I'm sure that the chunk of code that is supposed to do the reading part is working as well, please check the code if you can, there has to be some kind of really dumb mistake I guess .. or is it possible that the arduino uno isn't capable of reading so many pins ?您好,我想知道为什么这段代码不起作用,因为编码器 100% 工作,而且我确定应该执行读取部分的代码块也工作正常,如果可以,请检查代码,我猜一定有某种非常愚蠢的错误......或者arduino uno是否可能无法读取这么多引脚? Here is the code这是代码

Problem is being cause by the getTemperature function问题是由 getTemperature 函数引起的

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

#define LDR A0
#define Fan 3
#define ONE_WIRE_BUS 2
#define BACKLIGHT 5
    #define outputA 6
    #define outputB 7

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

float temperature = 0;

int start = 0 , start1 = 0;
unsigned long timer = 0;
unsigned long tx;

    int counter = 0;
    int aState, aLastState;

void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
  lcd.begin(16, 2);
  pinMode(Fan , OUTPUT);
  pinMode(A1,INPUT);
  pinMode(BACKLIGHT,OUTPUT);
  pinMode(4,INPUT_PULLUP);
  setEncoder();  
}


boolean isMin=true;
int temp1=4,temp2=15;
long pressed=millis();
void loop(void)
{
  encoderRead();
  getTemperature();
  if(digitalRead(4)==LOW&&millis()-pressed>=1000){
    isMin = !isMin;
    pressed=millis();
    tx=millis();
  }
  if(isMin){
    //temp2 = map(counter , 0 , 100 , 1 , 60);                       //For setting 15 min time from range of 1-60 minutes
    //tx=millis();
  }else{
    //temp1 = map(counter , 0 , 100 , 1 , 24);                       //For setting 5 hour time from range of 1-24 hour
    //tx=millis();
  }
  String s = "HODINY: "+String(temp1)+"\nMINUTY: "+String(temp2);
  Serial.println(s);
  Serial.println(counter);
  String t1 , t2;

  if (temp1 < 10)
    t1 = " " + String(temp1);
  else
    t1 = String(temp1);

  if (temp2 < 10)
    t2 = " " + String(temp2);
  else
    t2 = String(temp2);

  if (millis() - tx < 600000) {                                            //LCD displays stops every 10 minutes (600000milli seconds)
    lcd.setCursor(0, 0);
    lcd.print("Temp: " + String(temperature));
    lcd.setCursor(0, 1);
    lcd.print("T1: " + String(t1) + " H T2: " + String(t2) + " M");
    digitalWrite(BACKLIGHT,HIGH);
  }
  else
    digitalWrite(BACKLIGHT,LOW);                                          //No data on display

                                                                          //1. LDR - detects LIGHTS ON (daytime)
  if (analogRead(LDR) >= 700 && start == 0) {                             //If it is day time
    start = 1;                                                             //Set start (day) as 1
    start1 = 0;                                                            //Set start1 (night) as 0
    timer = millis();                                                      //Reset timer
    digitalWrite(Fan , HIGH);                                               //Arduino has FANS OFF
    //Serial.println("ZACAL SOM CAST JEDNA");
  }
                                                                           //1. LDR - detects LIGHTS OFF (night time)
  else if (analogRead(LDR) <700 && start1 == 0) {                        //If it is night time
    start = 0;                                                             //Set start (day) as 0
    start1 = 1;                                                            //Set start1 (night) as 1
    timer = millis();                                                      //Reset timer
    digitalWrite(Fan , LOW);                                              //Turn ON Fan When nite time is detected Arduino has FANS ON
    //Serial.println("ZACAL SOM CAST DVA");
  }

  //DAYTIME PROGRAM
  if (start == 1) {
    digitalWrite(Fan , HIGH);                                               //2. Arduino has FANS OFF and waits for temp to reach >90 deg (temperature rises during daytime)
    getTemperature();                                                      //3. Once >90 deg, Arduino runs "Timer A" for 15 minutes (set by pot #A)
    //Serial.println(temperature);
    if (temperature >= 90)                                                 //If temperature is greater then 90
    {
      start = 2;                                                           //Go to step 2
      timer = millis();                                                    //Reset timer
    }
  }
  else if (start == 2) {                                                   //Step2- If it is day time and temperature is greater 90 and timer is then 1-60 minutes set by pot
    if((millis() - timer)/1000 <= temp2 * 60) {                           //Wait for timer 'A' to expire, Arduino has fans off
        //Serial.println((millis() - timer)/1000);
        digitalWrite(Fan , HIGH);
        //Serial.println("CAKAM TIME A");
    }else{
    getTemperature();
    digitalWrite(Fan,LOW);
    start = 3;                                                             //Go to step 3
    timer = millis();                                                      //Reset timer
    }                       
  }
  else if (start == 3 && (millis() - timer)/1000 <= temp1  * 3600) {        //Step3-If it is day time and timer is less then 1-24 hour set by pot
    getTemperature();                                                      //5. Also immediately after 15 minute “Timer A” expires, Arduino runs “Timer B” that runs for 4 hours (set by pot#B).
    if(temperature>=77){                                                   //When temperature is more than 77deg turn on the FAN
      getTemperature();
      digitalWrite(Fan,LOW);
      //Serial.println("ZNIZUJEM TIMER B");
    }
    else if (temperature < 77){                                             //If regular daytime temperature is less then 76 turn OFF FAN
      digitalWrite(Fan , HIGH);                                              //Turn OFF Fan
      //Serial.println("STOJIM");
      getTemperature();
    }
    //Serial.println("CAKAM TIMER B");
  }
  else if(start ==3) {                                                      //If it is day time and timer is greater then 1-24 hour set by pot
    start = 1;                                                              //6. Once 4 hour “Timer B” expires and temperatures have been regulated at >78 <77 during this time, FAN OFF. LOOP TO #2 above (daytime program).
    //Serial.println("ZNOVA ZACINAM DEN");
  }

  //NIGHT TIME PROGRAM
  if (start1 == 1 && (millis() - timer)/1000 < temp1 * 3600) {              //Step1-If it is night time and timer is less then 1-24 hour set by pot
    getTemperature();                                                       //3. Also, as soon as nite time is detected by LDR “Timer B” (set by pot #B) is to run for 4 hours.
    if (temperature >= 70) {                                                //If temperature is greater then 70 turn ON FAN
      //Serial.println("ZNIZUJEM NOC");
      digitalWrite(Fan , LOW);                                             //Turn ON Fan
    }
    else if (temperature <= 69){                                             //If temperature is less= then 69 turn OFF FAN
      //Serial.println("ZVYSUJEM NOC");                         
      digitalWrite(Fan , HIGH);                                              //Turn OFF Fan
    }
  }
  else if (start1 == 1 && (millis() - timer)/1000 >= temp1 * 3600) {        //If it is night and timer is greater then 1-24 hour set by pot
    getTemperature();
    if (temperature >= 66) {                                                //Timer 'B' expired, FANs on until temp is lower than 66deg.
      //Serial.println("KONIEC BCKA IDEM CAKAT NA MENEJ AKO 66");                                             
      digitalWrite(Fan , LOW);                                                
      getTemperature();
    }
                                                                            //Once temperature is lower than 66deg = Arduino runs “Timer A” for 15 minutes (set by pot #A)
    else if(temperature < 66){
      digitalWrite(Fan , HIGH);                                              //Turn OFF Fan
      start1 = 3;                                                           //Go to step 2
      //Serial.println("MENEJ AKO 66");
      timer = millis();                                                     //Reset timer
    }
  }
  else if (start1 == 3 && (millis() - timer)/1000 > temp2 * 60) {           //If it is night time and timer is greater then 1-60 minutes set by pot
                                                                            //7. After “Timer A” expires, LOOP TO #2 (night time program)
    start1 = 1;                                                             //Go to step 1 night time
    timer = millis();                                                       //Reset timer
    //Serial.println("ZACINAM ZNOVA NOCICKU");
  }
}

void getTemperature(){
  sensors.requestTemperatures();
  temperature = sensors.getTempFByIndex(0);
}
void encoderRead(){
  aState = digitalRead(outputA);
  if(aState != aLastState){
    if(digitalRead(outputB)!=aState){
      if(counter<=100){
        counter++;
      }
    }else{
      if(counter>=0){
       counter--;
      }
    }
  }
  aLastState = aState;
}
void setEncoder(){
  pinMode(outputA,INPUT);
  pinMode(outputB,INPUT);
  aLastState = digitalRead(outputA);
}

And this is the code that I've tested and the readigs were just fine这是我测试过的代码,readigs 很好

#define outputA 6
#define outputB 7

int counter = 0;
int aState,aLastState;
void setup(void)
{
  Serial.begin(9600);
  pinMode(4,INPUT_PULLUP);
  setEncoder();  
}

void loop() {
  encoderRead();
  Serial.println(counter);
}

void encoderRead(){
  aState = digitalRead(outputA);
  if(aState != aLastState){
    if(digitalRead(outputB)!=aState){
      if(counter<=100){
        counter++;
      }
    }else{
      if(counter>=0){
       counter--;
      }
    }
  }
  aLastState = aState;
}
void setEncoder(){
  pinMode(outputA,INPUT);
  pinMode(outputB,INPUT);
  aLastState = digitalRead(outputA);
}

代码太长,arduino 缺少信号,如此长的代码需要中断才能与 rotart 编码器一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM