Förr sa ni "Jag förstår inte så mycket av den kod du skrev". Så då postade jag ett typexempel, men inte ens det räckte.
Kod: Markera allt
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 12
// pin 7 - Serial clock out (SCLK)
// pin 8 - Serial data out (DIN)
// pin 13 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 2 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 8, 13, 4, 2);
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Deceleration for LED, relays, speaker
const int pin_led_green = 10; // green
const int pin_led_red = 9; // red
const int pin_relay1 = 11; // relay 1
const int pin_relay2 = 6; // relay 2
const int pin_tone = 5; // Speaker
int first; // first value at question
int second; // second value at question
int minvalue; // min value is the minimun range e.g 30 or 50.
String labels[5][5] = { {"*****TIME*****", "Dv0 time:", "Range 30-99:", String(first), String(second) },
{"*****TEMP*****", "Dv0 temp:", "Range 50-99:", String(first), String(second) },
{"*****TIME*****", "Dv1 time:", "Range 30-99:", String(first), String(second) },
{"*****TEMP*****", "Dv1 temp:", "Range 50-99:", String(first), String(second) },
{"***END TEMP***", "End temp:", "Range 50-99:", String(first), String(second) }};
void setup()
{
// Sett green and red LED output
pinMode(pin_led_green, OUTPUT);
pinMode(pin_led_red, OUTPUT);
pinMode(pin_relay1, OUTPUT);
pinMode(pin_relay2, OUTPUT);
digitalWrite(pin_relay1, HIGH);
digitalWrite(pin_relay2, HIGH);
// Sett a tone at start
tone(pin_tone, 400, 400);
// Flash with LED at start
digitalWrite(pin_led_green, HIGH);
digitalWrite(pin_led_red, HIGH);
delay(400);
digitalWrite(pin_led_green, LOW);
digitalWrite(pin_led_red, LOW);
display.begin(); // Begin to use the display
display.setContrast(50); // set contrast
display.clearDisplay(); // Remove the adafruit logo and clear!
sensors.begin(); // Start up the temperature library
}
void loop()
{
int choose = start();
const int maxi = choose;
int i = 0;
int BrewList[choose];
while (i < maxi)
{
BrewList[i] = brewFunc(i);
// 100 = back
// 200 = Accept
// 300 = ask the same question again
if (BrewList[i] == 100 ) // Just back one question. i cannot be negative number.
{
if (i == 0)
{
i = i;
}
else // back but ask the question again.
{
i = i - 1;
}
}
else if (BrewList[i] == 300) // ask the same question again.
{
i = i;
}
else // Just keep forward and ask a new question
{
i = i + 1;
}
}
// now for the real temperature loop
display.clearDisplay();
for (i = 0; i < maxi; i++)
{
// Display the values
display.print(labels[i][1]);
display.println(BrewList[i]);
display.display();
}
display.print("Keep valueB/E?");
display.display();
while (choose != 70) // 70 is just random number
{
choose = tangent_key();
delay(500);
if (choose == 200) // 200 is E for accept/exicute
{
the_loop(BrewList, maxi);
while(1) // THE END!
{
tone(pin_tone, 600, 400);
delay(1000);
}
}
else if (choose == 100) // 100 is B for back.
{
display.clearDisplay();
display.print("Back to menu");
display.display();
delay(2000);
choose = 70;
}
else
{
display.clearDisplay();
display.println("Invalid value");
display.print("Enter B or E");
display.display();
}
}
// end of loop. Let's begin whith a new menu!
}
int start()
{
display.clearDisplay();
display.println("*****MENU*****");
display.print("Press 1 or 2:");
display.display();
int temponaryKey = 1023;
while(temponaryKey != 1 || 2)
{
temponaryKey = tangent_key();
delay(500);
if (temponaryKey == 1)
{
display.clearDisplay();
display.print("You press: 1");
display.display();
delay(2000);
return 5;
}
else if (temponaryKey == 2)
{
display.clearDisplay();
display.print("You press: 2");
display.display();
delay(2000);
return 2;
}
else
{
display.clearDisplay();
display.println("Invalid number");
display.print("Try again");
display.display();
delay(2000);
tone(pin_tone, 800, 400);
display.clearDisplay();
}
}
}
int tangent_key() // press any key to choose metod
{
int tangent = 1023;
while(tangent == 1023)
{
// This is a voltage devider
while(tangent >= 1015)
{
tangent = analogRead(A5); // Analog pin 5
}
if (tangent > 585 && tangent < 595) // number 1
{
tone(pin_tone, 400, 400);
return 1;
}
else if (tangent > 50 && tangent < 60) // number 2
{
tone(pin_tone, 400, 400);
return 2;
}
else if (tangent > 370 && tangent < 390) // number 3
{
tone(pin_tone, 400, 400);
return 3;
}
else if (tangent > 999 && tangent < 1004) // number 4
{
tone(pin_tone, 400, 400);
return 4;
}
else if (tangent > 35 && tangent < 47) // number 5
{
tone(pin_tone, 400, 400);
return 5;
}
else if (tangent > 10 && tangent < 19) // number 6
{
tone(pin_tone, 400, 400);
return 6;
}
else if (tangent > 800 && tangent < 815) // number 7
{
tone(pin_tone, 400, 400);
return 7;
}
else if (tangent > 4 && tangent < 10) // number 8
{
tone(pin_tone, 400, 400);
return 8;
}
else if (tangent > 170 && tangent < 185) // number 9
{
tone(pin_tone, 400, 400);
return 9;
}
else if (tangent >= 0 && tangent < 1) // number 0
{
tone(pin_tone, 400, 400);
return 0;
}
else if (tangent > 855 && tangent < 875) // number 100 or B for back
{
tone(pin_tone, 400, 400);
display.clearDisplay();
return 100;
}
else if (tangent > 1006 && tangent < 1012) // number 200 or E for enter
{
tone(pin_tone, 400, 400);
display.clearDisplay();
return 200;
}
else
{
display.clearDisplay();
display.println("****ERROR****");
display.println("Try again");
display.print("Enter number");
display.display();
delay(3000);
tangent = 1023; // reset tangent to value 1023 and star over again
}
}
}
int brewFunc(int i)
{
// Display Menu, regulator and range
display.clearDisplay();
display.print(labels[i][0]); // Menu
display.println(labels[i][1]); // Device
display.println(labels[i][2]); // Range
display.display();
// Add first number
first = tangent_key();
delay(1000); // just to slow down
if (first == 100) // if press button B as furst number. Button B is value 100.
{
display.clearDisplay();
display.print("*****BACK*****");
display.print("Going back....");
display.display();
delay(1000);
return 100; // Just step back one question
}
else if (first == 200) // if press button E as furst number. Button E is value 200.
{
display.clearDisplay();
display.print("*****ERROR!***");
display.print("Not now!");
display.print("Returning.....");
display.display();
delay(1000);
return 300; // Just ask the same question again.
}
else
{
display.clearDisplay();
display.print(labels[i][0]); // Menu
display.println(labels[i][1]); // Device
display.println(labels[i][2]); // Range
display.print(first); // display the first value
display.display();
}
// Add second number
second = tangent_key();
delay(1000); // just to slow down
if (second == 100) // if press button B as furst number. Button B is value 100.
{
display.clearDisplay();
display.print("*****BACK*****");
display.print("Going back....");
display.display();
delay(1000);
return 100; // Just step back one question
}
else if (second == 200) // if press button E as second number. Button E is value 200.
{
while (second == 200)
{
display.clearDisplay();
display.print("*****ERROR!***");
display.print("Not E now!");
display.print("Enter number");
display.display();
delay(1000);
// Add a new second number
second = tangent_key();
delay(1000); // just to slow down
if (second == 100)
{
display.clearDisplay();
display.print("*****BACK*****");
display.print("Going back....");
display.display();
delay(1000);
return 100; // Just step back one question
}
else
{
// Nothing
}
}
// End of while-loop
display.clearDisplay();
display.print(labels[i][0]); // Menu
display.println(labels[i][1]); // Device
display.println(labels[i][2]); // Range
display.print(first); // display the first value
display.print(second); // display the second value
display.display();
delay(1000);
display.clearDisplay();
display.print("Keep value:");
display.print(String(String(first) += String(second) += "?")); // display first_second value.
display.display();
int Kepp_value = tangent_key();
delay(1000);
if (Kepp_value == 100) // Back = NO - Just go back to the same question again
{
display.clearDisplay();
display.print("*****BACK*****");
display.print("Going back....");
display.display();
delay(1000);
return 100; // 100 = Just step back one question
}
else if(Kepp_value == 200) // E = YES - Keep value
{
if (i == 0 || i == 2)
{
minvalue = 30; // min time value. Max temp and time is allways 99.
}
else
{
minvalue = 50; // min temp value. Max temp and time is allways 99.
}
if ((String(String(first) += String(second))).toInt() >= minvalue && (String(String(first) += String(second))).toInt() <= 99)
{
display.clearDisplay();
display.print("*****DONE*****");
display.print("Done....");
display.print(String(String(first) += String(second)));
display.display();
delay(1000);
return (String(String(first) += String(second))).toInt(); // Return the first_second value
}
else
{
display.clearDisplay();
display.print("*****BACK*****");
display.print("Not in range.");
display.print("Going back....");
display.display();
delay(1000);
return 300; // 300 is back only to the same question again.
}
}
}
else
{
display.clearDisplay();
display.print(labels[i][0]); // Menu
display.println(labels[i][1]); // Device
display.println(labels[i][2]); // Range
display.print(first); // display the first value
display.print(second); // display the second value
display.display();
delay(1000);
display.clearDisplay();
display.print("Keep value:");
display.print(String(String(first) += String(second) += "?")); // display first_second value.
display.display();
int Kepp_value = tangent_key();
delay(1000);
if (Kepp_value == 100) // Back = NO - Just go back to the same question again
{
display.clearDisplay();
display.print("*****BACK*****");
display.print("Going back....");
display.display();
delay(1000);
return 300; // 300 is back only to the same question again.
// Just return.
}
else if(Kepp_value == 200) // E = YES - Keep value
{
if (i == 0 || i == 2)
{
minvalue = 1;
}
else
{
minvalue = 1;
}
if ((String(String(first) += String(second))).toInt() >= minvalue && (String(String(first) += String(second))).toInt() <= 99)
{
display.clearDisplay();
display.print("*****DONE*****");
display.print("Done....");
display.print(String(String(first) += String(second)));
display.display();
delay(1000);
return (String(String(first) += String(second))).toInt(); // Return the first_second value
}
else
{
display.clearDisplay();
display.print("*****BACK*****");
display.println("Not in range.");
display.print("Going back....");
display.display();
delay(1000);
return 300; // 300 is back only to the same question again.
}
}
}
}
void the_loop(int BrewList[], int way)
{
// way can be 2 or 5. 2 is halfbrew and 5 is full brew.
// BrewList[] contains values from the questions.
digitalWrite(pin_led_red, HIGH); // start red led.
if (way == 5)
{
int i = 0;
int time = 0;
while(1)
{
sensors.requestTemperatures(); // request sensors temperature
// time clock
if (i == 60)
{
time++;
i = 0;
}
else
{
i++;
}
display.display();
display.clearDisplay();
display.print("Temp dv0:");
display.print(sensors.getTempCByIndex(0));
display.print("Temp dv1:");
display.print(sensors.getTempCByIndex(1));
display.print("End temp:");
display.println(BrewList[4]);
display.print("Time dv0:");
if (BrewList[0] - time <= 0) // brewlist[0] is time limit for Dv0 time
{
if (BrewList[0] - time == -1) // End time!
{
digitalWrite(pin_relay2, HIGH); // Turn OFF the relay 2 for dv0.
while(1) // BrewList[4] is the integear end temp value.
{
sensors.requestTemperatures(); // request sensors temperature
if(sensors.getTempCByIndex(0) >= BrewList[4])
{
display.clearDisplay();
display.display();
break;
}
else
{
display.clearDisplay();
display.print("***END TEMP***");
display.print("Temp dv0:");
display.print(sensors.getTempCByIndex(0));
display.print("End temp:");
display.print(BrewList[4]);
digitalWrite(pin_relay1, LOW); // Turn ON relay for dv0.
display.display();
}
}
break; // end loop.
}
else
{
display.println("0");
}
}
else
{
display.println(BrewList[0] - time);
}
display.print("Time dv1:");
if (BrewList[2] >= time) // brewlist[2] is time limit for Dv1 time
{
display.println(-BrewList[2] + time); // show before_start_time who is minus.
}
else if (BrewList[2] < time)
{
display.println(BrewList[0] - time); // count down to end time.
}
else
{
display.println("0");
}
// dv0 is first tank
if (sensors.getTempCByIndex(0) < BrewList[1]) // if sensor dv0 lower that choosen value ->
{
digitalWrite(pin_relay1, LOW); // Turn ON
}
else
{
digitalWrite(pin_relay1, HIGH); // Turn OFF
}
// dv1 is second tank.
if (time > BrewList[2])
{
if (sensors.getTempCByIndex(1) < BrewList[3]) // if sensor dv1 lower that choosen value ->
{
digitalWrite(pin_relay2, LOW); // Turn ON
}
else
{
digitalWrite(pin_relay2, HIGH); // Turn OFF
}
}
else
{
// Nothing
}
//delay(1000);
}
digitalWrite(pin_relay1, HIGH); // Turn OFF relay for dv0.
digitalWrite(pin_led_red, LOW); // Turn OFF the red led.
digitalWrite(pin_led_green, HIGH); // Turn ON the green led.
}
else
{
// HÄR SKA ANDRA DELEN AV KODEN KOMMA.
}
}