您好,我想知道为什么此代码不起作用,因为编码器100%工作,并且我确定应该用于阅读的代码块也能正常工作,请检查代码如果可以的话,我想一定会有某种愚蠢的错误。或者arduino uno可能无法读取这么多的图钉?这是代码
问题是由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);
}
这是我测试过的代码,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);
}
答案 0 :(得分:0)
代码太长,arduino缺少信号,要使这种长代码与rotart编码器一起工作,就需要中断。