#include <LiquidCrystal.h>
#include "OneWire.h"
#include "DallasTemperature.h"
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
// for encodeer
#define outputA 5
#define outputB 4
int counter = 0;
int aState;
int aLastState;
// for ds18B20
// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 7
// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);
//
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
lcd.begin(16,2);
// Start up the library:
sensors.begin();
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
// Send the command for all devices on the bus to perform a temperature conversion:
sensors.requestTemperatures();
// Fetch the temperature in degrees Celsius for device index:
float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
// Fetch the temperature in degrees Fahrenheit for device index:
float tempF = sensors.getTempFByIndex(0);
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
//// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
// lcd.clear();
} else {
counter --;
// lcd.clear();
}
Serial.print("Position: ");
Serial.println(counter);
lcd.setCursor(0, 0);
lcd.print("Position: ");
lcd.setCursor(10, 0);
lcd.print(counter);
// Print the temperature in Celsius in the Serial Monitor:
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.print("C | ");
// Print the temperature in Fahrenheit
Serial.print(tempF);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.println("F");
// Wait 1 second:
delay(1000);
//lcd.print("Eng / Nada");
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}
Loading
ds18b20
ds18b20