Hi,
For a school project i'm working on menu that only uses slide potmeters. This is a requirement and we are not allowed to use any other electronics.
The project is a calculator with multiple formula's, if you want to take a look follow to link.
My school project.
For the selection of the menu i made the following code
if ((potM0 <= 0)&&(potM1 <= 0)&&(potM2 <= 0)) {
lcd.clear();
menu--;
tekst1 = 0;
delay(1000);
}
I made multiple IF statements that are linked to the menu variable.
Now my problem is that when all the potmeters are on zero the menu goes back, but i don't want that to happen because then i can never calculate the lowest number. What i want is to add a layer so the menu only changes when the following happens:
-all sliders on the lowest
-timer start
-slider 1 down and up
-Did this happen between an x amount of time?
-yes, menu changes
-no, menu stays the same
I am told to do this with millis and i tried but i can't seem to figure it out.
With kind regards,
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
int menu = 0;
int potM0 =A0;
int potM1 =A1;
int potM2 =A2;
int tekst1 =0;
unsigned long startMillis;
byte Vierkant[8] = {
0b00000,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b00000,
0b00000
};
byte Cirkel[8] = {
0b00000,
0b01110,
0b11111,
0b11111,
0b11111,
0b01110,
0b00000,
0b00000
};
byte Driehoek[8] = {
0b00000,
0b00100,
0b00100,
0b01110,
0b01110,
0b11111,
0b11111,
0b00000
};
//.......................Formule 1............................................................
float a, b, c, r, gr, bo, A, B, C;
float calculatie(float X, float Y, float Z)
{
float R;
gr = X/2;
bo = Y/2;
R = (PI*Z*(sq(gr)+sq(bo)+(gr)*(bo)))/3;
return R;
}
//.......................Formule 2............................................................
float ab, hb, ob, stb, Ab;
float calculatieHoogte(float Xb)
{
float Hb;
stb=Xb/2;
Hb = 1000000/(PI*sq(stb));
return Hb;
}
float calculatieOppervlakte(float Xb)
{
float Ob;
stb=Xb/2;
Ob = 2*PI*stb*(stb+(1000000/(PI*sq(stb))));
return Ob;
}
//.......................Formule 3............................................................
float ac, rc, dc, Ac;
float calculatie(float Xc)
{
float Rc;
dc = ((-7172.00*sq(Xc)+717200.00*Xc+80371225.00)/2209.00);
Rc = (((-143440.00-640.00*Xc)/8836.00)-sqrt(dc))/(-4386.00/2209.00);
return Rc;
}
//.......................Formule 4......................................
float ad, wd, kd, afd, Ad;
float maximaleWrijving(float Xd)
{
float Wd;
afd=Xd/100.00;
Wd = 313.92-(533.664/sqrt(16.00+sq(afd)));
return Wd;
}
float horizontaleKracht(float Xd)
{
float Kd;
afd=Xd/100.00;
Kd = (333.54*afd)/sqrt(16.00+sq(afd));
return Kd;
}
//...............Formule 5..........................................
float ae, be, ce, re, Ae, Be, Ce;
float calculatiee(float Xe, float Ye, float Ze)
{
float Re;
float dte = 0.1;
float ge = 9810;
while (Ze>0) {
Ze=Ze-(sq(Xe)/sq(Ye))*dte*sqrt(2*ge*Ze);
Re = Re+dte;
}
return Re;
}
//Welkom tekst
void scrolling_loop1()
{
String text = "Schuif alle sliders naar omhoog om naar de volgende formule te gaan";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,1);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,1);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
void scrolling_loop2()
{
String text = "Schuif alle sliders naar omlaag om terug naar de vorige formule te gaan";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,1);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,1);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
void scrolling_loop3()
{
String text = "Het volume van een afgeknotte kegel";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,2);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,2);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
void scrolling_loop4()
{
String text = "Cilindrisch volume verpakking";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,2);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,2);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
void scrolling_loop5()
{
String text = " Kabelbaan";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,2);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,2);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
void scrolling_loop6()
{
String text = " Trekkabel";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,2);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,2);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
void scrolling_loop7()
{
String text = " Leegstromend vat";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,2);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,2);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
void scrolling_loop8()
{
String text = "De kosten van 3D printen";
int i, j=0;
for (i =0; i<text.length(); i++)
{
if(i<19)
{
lcd.setCursor(i,2);
lcd.print(text.charAt(i));
delay(150);
}
else
{
for(i=20;i<text.length();i++)
{
j++;
lcd.setCursor(0,2);
lcd.print(text.substring(j,j+20));
delay(250);
}
}
}
}
//.......................Formule 5......................................
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(4,1);
lcd.print("Welkom in de");
lcd.setCursor(1,2);
lcd.print("Custom calculator!");
delay(2000);
lcd.clear();
//lcd.setCursor(7,0);
//lcd.print("Uitleg");
//scrolling_loop1();
// delay(1000);
//lcd.clear();
//lcd.setCursor(7,0);
//lcd.print("Uitleg");
//scrolling_loop2();
//delay(1000);
//lcd.clear();
}
void loop()
{
potM0 = map(analogRead(A0), 0, 1023, 0, 50);
potM1 = map(analogRead(A1), 0, 1023, 0, 50);
potM2 =map(analogRead(A2), 0, 1023, 0, 50);
//if ((potM0 >= 50)&&(potM1 >=50)&&(potM2>=50)) {
//lcd.clear();
// menu++;
// tekst1 = 0;
//delay(1000);
//}
if ((potM0 >= 50)&&(potM1 >=0)&&(potM2>=50)) {
lcd.clear();
menu = 10 ;
tekst1 = 0;
delay(1000);
}
if ((potM0 >= 50)&&(potM1 >=50)&&(potM2>=50)) {
millis();
if (((potM0 >= 50)&&(potM1 >=50)&&(potM2>=50)) && (millis <= 500)){
lcd.clear();
menu++;
tekst1 = 0;
//delay(1000);
}
}
if ((potM0 <= 0)&&(potM1 <= 0)&&(potM2 <= 0)) {
lcd.clear();
menu--;
tekst1 = 0;
delay(1000);
}
//.......................Formule 1............................................................
if(menu==-1){
menu =0;
}
if (menu==0){
if(tekst1==0){
lcd.setCursor(5,1);
lcd.print("Formule 1");
scrolling_loop3();
delay(2000);
lcd.clear();
tekst1++;
}
a = map(analogRead(A0), 0, 1023, 0, 50);
lcd.createChar(0, Vierkant);
lcd.setCursor(0,0);
lcd.write((byte)0);
lcd.setCursor(2,0);
lcd.print("Diameter1=");
lcd.print(a,0);
b = map(analogRead(A1), 0, 1023, 0, 25);
lcd.createChar(1, Cirkel);
lcd.setCursor(0,1);
lcd.write((byte)1);
lcd.setCursor(2,1);
lcd.print("Diameter2=");
lcd.print(b,0);
c = map(analogRead(A2), 0 , 1023, 0, 100);
lcd.createChar(2, Driehoek);
lcd.setCursor(0,2);
lcd.write((byte)2);
lcd.setCursor(2,2);
lcd.print("Hoogte=");
lcd.print(c,0);
r = calculatie(a,b,c);
lcd.setCursor(2,3);
lcd.print("Volume=");
lcd.print(r,2);
A = a;
B = b;
C = c;
a = map(analogRead(A0), 0, 1023, 0, 50);
if (a != A)
{
lcd.setCursor(12,0);
lcd.print(" ");
lcd.setCursor(12,0);
lcd.print(a,0);
r = calculatie(a,b,c);
lcd.setCursor(9,3);
lcd.print(" ");
lcd.setCursor(9,3);
lcd.print(r,2);
A = a;
}
b = map(analogRead(A1), 0, 1023, 0, 25);
if (b != B)
{
lcd.setCursor(12,1);
lcd.print(" ");
lcd.setCursor(12,1);
lcd.print(b,0);
r = calculatie(a,b,c);
lcd.setCursor(9,3);
lcd.print(" ");
lcd.setCursor(9,3);
lcd.print(r,2);
B = b;
}
c = map(analogRead(A2), 0, 1023, 0, 100);
if (c != C)
{
lcd.setCursor(9,2);
lcd.print(" ");
lcd.setCursor(9,2);
lcd.print(c,0);
r = calculatie(a,b,c);
lcd.setCursor(9,3);
lcd.print(" ");
lcd.setCursor(9,3);
lcd.print(r,2);
C = c;
}
}
//.......................Formule 2..........................................................
else if (menu==1){
if(tekst1==0){
lcd.setCursor(5,1);
lcd.print("Formule 2");
scrolling_loop4();
delay(2000);
lcd.clear();
tekst1++;
}
ab = map(analogRead(A0), 0, 1023, 75, 125);
lcd.createChar(0, Vierkant);
lcd.setCursor(0,0);
lcd.write((byte)0);
lcd.setCursor(2,0);
lcd.print("Diameter=");
lcd.print(ab,0);
hb = calculatieHoogte(ab);
lcd.setCursor(2,1);
lcd.print("Hoogte=");
lcd.print(hb,2);
ob = calculatieOppervlakte(ab);
lcd.setCursor(2,2);
lcd.print("Oppervlak=");
lcd.print(ob,2);
Ab = ab;
ab = map(analogRead(A0), 0, 1023, 75, 125);
if (ab != Ab)
{
lcd.setCursor(11,0);
lcd.print(" ");
lcd.setCursor(11,0);
lcd.print(ab,0);
hb = calculatieHoogte(ab);
lcd.setCursor(9,1);
lcd.print(" ");
lcd.setCursor(9,1);
lcd.print(hb,2);
ob = calculatieOppervlakte(ab);
lcd.setCursor(12,2);
lcd.print(" ");
lcd.setCursor(12,2);
lcd.print(ob,2);
Ab = ab;
}
}
//.......................Formule 3.......................................................
else if (menu==2){
if(tekst1==0){
lcd.setCursor(5,1);
lcd.print("Formule 3");
scrolling_loop5();
delay(2000);
lcd.clear();
tekst1++;
}
ac = map(analogRead(A0), 0.00, 1023.00, 0.00, 1000.00);
lcd.createChar(0, Vierkant);
lcd.setCursor(0,0);
lcd.write((byte)0);
lcd.setCursor(2,0);
lcd.print("X = ");
lcd.print(ac/10.00,1);
rc = calculatie(ac/10.00);
lcd.setCursor(2,1);
lcd.print("Y = ");
lcd.print(rc,7);
Ac = ac;
ac = map(analogRead(A1), 0.00, 1023.0, 0.00, 1000.00);
if (ac != Ac)
{
lcd.setCursor(6,0);
lcd.print(" ");
lcd.setCursor(6,0);
lcd.print(ac/10.00,1);
rc = calculatie(ac/10.00);
lcd.setCursor(6,1);
lcd.print(" ");
lcd.setCursor(6,1);
lcd.print(rc,7);
Ac = ac;
}
}
//.......................Formule 4.......................................................
else if (menu==3){
if(tekst1==0){
lcd.setCursor(5,1);
lcd.print("Formule 4");
scrolling_loop6();
delay(2000);
lcd.clear();
tekst1++;
}
ad = map(analogRead(A0), 0.00, 1023.00, 0.00, 500.00);
lcd.createChar(0, Vierkant);
lcd.setCursor(0,0);
lcd.write((byte)0);
lcd.setCursor(2,0);
lcd.print("x = ");
lcd.print(ad,0);
wd = maximaleWrijving(ad);
lcd.setCursor(2,1);
lcd.print("Fmax = ");
lcd.print(wd,2);
kd = horizontaleKracht(ad);
lcd.setCursor(2,2);
lcd.print("Fhor = ");
lcd.print(kd,2);
Ad = ad;
ad = map(analogRead(A0), 0.00, 1023.00, 0.00, 500.00);
if (ad != Ad)
{
lcd.setCursor(9,0);
lcd.print(" ");
lcd.setCursor(9,0);
lcd.print(ad,0);
wd = maximaleWrijving(ad);
lcd.setCursor(9,1);
lcd.print(" ");
lcd.setCursor(9,1);
lcd.print(wd,2);
kd = horizontaleKracht(ad);
lcd.setCursor(9,2);
lcd.print(" ");
lcd.setCursor(9,2);
lcd.print(kd,2);
Ad = ad;
}
}
//.......................Formule 5.......................................................
else if (menu==4){
if(tekst1==0){
lcd.setCursor(5,1);
lcd.print("Formule 5");
scrolling_loop7();
delay(2000);
lcd.clear();
tekst1++;
}
{
ae = map(analogRead(A0), 0, 1023, 5, 50);
lcd.createChar(0, Vierkant);
lcd.setCursor(0,0);
lcd.write((byte)0);
lcd.setCursor(2,0);
lcd.print("Diameter1=");
lcd.print(ae,0);
be = map(analogRead(A1), 0, 1023, 400, 800);
lcd.createChar(1, Cirkel);
lcd.setCursor(0,1);
lcd.write((byte)1);
lcd.setCursor(2,1);
lcd.print("Diameter2=");
lcd.print(be,0);
ce = map(analogRead(A2), 0 , 1023, 500, 1000);
lcd.createChar(2, Driehoek);
lcd.setCursor(0,2);
lcd.write((byte)2);
lcd.setCursor(2,2);
lcd.print("Hoogte=");
lcd.print(ce,0);
lcd.setCursor(2,3);
lcd.print("Tijd=");
re = calculatiee(ae,be,ce);
lcd.print(re,0);
lcd.setCursor(13,3);
lcd.print("sec.");
Ae = ae;
Be = be;
Ce = ce;
}
{
ae = map(analogRead(A0), 0, 1023, 5, 50);
if (ae != Ae)
{
lcd.setCursor(12,0);
lcd.print(" ");
lcd.setCursor(12,0);
lcd.print(ae,0);
re = calculatiee(ae,be,ce);
lcd.setCursor(7,3);
lcd.print(" ");
lcd.setCursor(7,3);
lcd.print(re,0);
Ae = ae;
}
be = map(analogRead(A1), 0, 1023, 400, 800);
if (be != Be)
{
lcd.setCursor(12,1);
lcd.print(" ");
lcd.setCursor(12,1);
lcd.print(be,0);
re = calculatiee(ae,be,ce);
lcd.setCursor(7,3);
lcd.print(" ");
lcd.setCursor(7,3);
lcd.print(re,0);
Be = be;
}
ce = map(analogRead(A2), 0, 1023, 500, 1000);
if (ce != Ce)
{
lcd.setCursor(9,2);
lcd.print(" ");
lcd.setCursor(9,2);
lcd.print(ce,0);
re = calculatiee(ae,be,ce);
lcd.setCursor(7,3);
lcd.print(" ");
lcd.setCursor(7,3);
lcd.print(re,0);
Ce = ce;
}
}
}
else if (menu==5){
if(tekst1==0){
lcd.setCursor(2,1);
lcd.print(" Eigen opdracht");
scrolling_loop8();
delay(2000);
lcd.clear();
tekst1++;
lcd.setCursor(0,1);
lcd.print("hier komt de opdracht");
}
}
else if (menu==10){
lcd.setCursor(3,0);
lcd.print("Gemaakt door:");
lcd.setCursor(4,1);
lcd.print("Stefan kars");
lcd.setCursor(0,2);
lcd.print("Camile Zaadnoordijk");
lcd.setCursor(4,3);
lcd.print("Abe Reinsma");
}
}