我为我的学校制作这个项目,我正在使用 Arduino 制作整个冰箱,我使用 ds18b20 温度传感器,tm1637 来显示内部当前温度,并使用固态继电器在我选择的特定温度下启动电机,问题我使用 ec11 旋转编码器来改变 ssr 激活的温度,但是当我向任何方向转动旋钮时,它只会增加值而不会减少。我单独测试了编码器很多次,它工作得很好,但是当我将它添加到代码中时,它根本不起作用,但是当我删除 (printtemperarure) 函数时,它再次工作,我需要尽快帮助
注意:当我单独测试显示器和编码器时,它不断产生零而没有接触它
#include <TM1637Display.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int oneWireBus = 2;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
const int CLK = 3;
const int DIO = 4;
TM1637Display display = TM1637Display(CLK, DIO);
const uint8_t celsiusSymbol[] = {
SEG_A | SEG_B | SEG_F | SEG_G,
SEG_A | SEG_D | SEG_E | SEG_F
};
#define ContactA 10
#define ContactB 11
//#define SW_PIN 12
// ----- Logic
boolean LastStateA;
boolean CurrentStateA;
boolean CurrentStateB;
const int SSR_PIN = 9; // Example pin, replace with your SSR pin
// Threshold temperature
int motorThreshold = 20.0;
int newmotorThreshold = motorThreshold;
bool thresholdAdjusted = false;
// ==================================
// setup()
// ==================================
void setup() {
// ----- Configure Serial port
Serial.begin(9600);
Serial.println("Initializing...");
display.clear();
display.setBrightness(7);
sensors.begin();
// Set SSR pin as output
pinMode(SSR_PIN, OUTPUT);
Serial.println("Initialization complete.");
// ----- Display initial count
// ----- Configure encoder
pinMode(ContactA, INPUT_PULLUP);
pinMode(ContactB, INPUT_PULLUP);
LastStateA = stateContactA();
}
void adjustThresholdTemperature() {
/*
The encoder output comprises two 90 degree offset squarewaves.
The direction of rotation is:
- clockwise if the state of output A is opposite to output B
- counterclockwise if the state of output A is the same as output B
*/
// ----- Check encoder output
CurrentStateA = stateContactA();
// ----- Record changes
if (CurrentStateA != LastStateA) {
CurrentStateB = digitalRead(ContactB); // Check state of ContactB
if (CurrentStateA == CurrentStateB)
{
// Clockwise rotation, increase threshold temperature
motorThreshold += 2; // Example adjustment, change as needed
thresholdAdjusted = true; // Set flag to indicate threshold adjustment
if (motorThreshold % 2 == 0) {
Serial.print("motorThreshold: ");
Serial.println(motorThreshold / 1);
};
}
if (CurrentStateA != CurrentStateB)
{
// Counterclockwise rotation, decrease threshold temperature
motorThreshold -=2; // Example adjustment, change as needed
thresholdAdjusted = true; // Set flag to indicate threshold adjustment
if (motorThreshold % 2 == 0) {
Serial.print("motorThreshold: ");
Serial.println(motorThreshold / 1);
};
}
// Counterclockwise rotation
LastStateA = CurrentStateA;
}
/*if (digitalRead(SW_PIN) == LOW)
{
// Button is pressed
// Reset threshold temperature to default value
motorThreshold = 20.0; // Example default value, change as needed
Serial.println("Threshold temperature reset to default.");
thresholdAdjusted = true; // Set flag to indicate threshold adjustment
delay(200); // Debouncing delay
} */
}
void printTemperature()
{
sensors.requestTemperatures();
float measuredTemperature = sensors.getTempCByIndex(0);
Serial.print("Current temperature: ");
Serial.print(measuredTemperature);
Serial.println("°C");
//Display current temperature
display.showNumberDec(measuredTemperature, false, 2, 0);
display.setSegments(celsiusSymbol, 2, 2);
// Check if temperature exceeds threshold
if (measuredTemperature >= motorThreshold) {
// Activate SSR
digitalWrite(SSR_PIN, HIGH);
} else {
// Deactivate SSR
digitalWrite(SSR_PIN, LOW);
}
if (thresholdAdjusted)
{
unsigned long startTime = millis();
while (millis() - startTime < 3000)
{
// Flash the display by toggling brightness every 500 milliseconds
if ((millis() - startTime) % 500 < 250)
{
display.showNumberDec(motorThreshold, false, 2, 0);
display.setSegments(celsiusSymbol, 2, 2);
}
else
{
display.clear();
}
}
thresholdAdjusted = false; // Reset threshold adjustment flag after 3 seconds
}
}
boolean stateContactA() {
/*
Two integrators are used to suppress contact bounce.
The first integrator to reach MaxCount wins
*/
// ----- Locals
int Closed = 0; // Integrator
int Open = 0; // Integrator
int MaxCount = 250; // Increase this value if you see contact bounce
// ----- Debounce Contact A
while (1) {
// ----- Check ContactA
if (digitalRead(ContactA)) {
// ----- ContactA is Open
Closed = 0; // Empty opposite integrator
Open++; // Integrate
if (Open > MaxCount) return HIGH;
} else {
// ----- ContactA is Closed
Open = 0; // Empty opposite integrator
Closed++; // Integrate
if (Closed > MaxCount) return LOW;
}
}
}
// loop()
// ==================================
void loop() {
adjustThresholdTemperature();
printTemperature();
}
您的
printTemperature()
会阻塞 3 秒,而为了捕捉旋钮的方向,您必须至少每隔 50 毫秒或更短时间测试 ContactA
和 ContactB
一次,才能做出响应。
如果您尝试删除闪烁的显示部分,请再次更新它是如何进行的,也许我们可以更清楚地看到那里发生了什么,当旋钮逆时针转动时,阻止阈值降低