#include <LiquidCrystal.h>
#include <SD.h>
#define CS_PIN 17
const int programButton = 2;
const int startButton = 3;
const int powerButton = 4;
const int stepPin = 20;
const int dirPin = 21;
const int buzzerPin = 14;
const float BETA = 3950;
int oldValueProgram = LOW;
int oldValueStart = LOW;
int oldValuePower = LOW;
int temp_timer = 0;
boolean powerOn = false;
String programs[] = {"ECO", "Fast", "Regular"};
int durations[] = {200, 100, 150};
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int pos = 0;
File root;
void setup() {
Serial.begin(115200);
delay(20);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.");
lcd.begin(20, 4);
lcd.createChar(1, new byte[8]{0x06, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00});
resetLcd();
lcd.noDisplay();
pinMode(programButton, INPUT);
pinMode(startButton, INPUT);
pinMode(powerButton, INPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(stepPin, LOW);
delay(20);
}
void buttonPressed() {
int numPrograms = 3;
pos++;
if (pos >= numPrograms) {
pos = 0;
}
if (pos < 0) {
pos = numPrograms - 1;
}
writeLcd(programs[pos], 0, 1);
writeLcd(String(durations[pos]) + " ms", 0, 2);
writeTemperature(programs[pos]);
}
void loop() {
if (powerOn){
listen2button(programButton, oldValueProgram, &buttonPressed);
listen2button(startButton, oldValueStart, &runProgram);
}
listen2button(powerButton, oldValuePower, &switch_power);
if(temp_timer >= 2000){
File textFile = SD.open("temp_log.txt", FILE_WRITE);
if (textFile) {
int analogValue = analogRead(28);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
textFile.println("time: " + String(millis()/1000) + "s temperatur: " + String(celsius));
textFile.close();
} else {
Serial.println("error opening temp_log.txt!");
}
temp_timer = 0;
} else{
temp_timer = temp_timer + 10;
}
delay(10); // Adding a delay() here speeds up the simulation
}
void listen2button(int pin, int& old_value, void (*func) ()){
int new_value = digitalRead(pin);
if(new_value != old_value)
{
if(new_value == HIGH)
{
func();
}
// Remember the value for the next time.
old_value = new_value;
}
}
void runProgram(){
int duration = durations[pos];
writeLcd("Running program:", 0, 0);
writeLcd(String(durations[pos]) + "/" + String(durations[pos]) + " ms", 0, 2);
runMotor(duration);
writeLcd("Finished program:", 0, 0);
delay(30);
for (int j = 0; j < 3; j++){
tone(buzzerPin, 1760, 20);
delay(200);
}
resetLcd();
}
void runMotor(int duration){
digitalWrite(dirPin, HIGH);
for (int i = 0; i < duration; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
int remainingTime = duration - (i + 1);
String padding = "";
if (remainingTime < 100){
padding += "0";
}
if (remainingTime < 10){
padding += "0";
}
lcd.setCursor(0, 2);
lcd.print(padding + String(remainingTime));
delay(5);
}
}
void writeLcd(String msg, int column, int row){
lcd.setCursor(column, row);
for(int n = column; n < 20; n++)
{
lcd.print(" ");
}
lcd.setCursor(column, row);
lcd.print(msg);
}
void resetLcd(){
writeLcd("Select program:", 0, 0);
writeLcd(programs[pos], 0, 1);
writeLcd(String(durations[pos]) + " ms", 0, 2);
writeTemperature(programs[pos]);
}
void writeTemperature(String program){
lcd.setCursor(0, 3);
for(int n = 0; n < 20; n++)
{
lcd.print(" ");
}
lcd.setCursor(0, 3);
if (program == "ECO"){
lcd.print("50");
}
else{
lcd.print("65");
}
lcd.write(1);
lcd.print("C");
}
void switch_power(){
powerOn = !powerOn;
if (powerOn){
lcd.display();
tone(buzzerPin, 1074, 20);
}
else{
lcd.noDisplay();
File textFile = SD.open("temp_log.txt");
if (textFile) {
Serial.println("temp_log.txt: ");
while (textFile.available()) {
Serial.write(textFile.read());
}
} else {
Serial.println("error opening temp_log.txt!");
}
}
}