#include <ezButton.h>
#define DEBOUNCE_TIME 50
//ezButton button1(6); // create ezButton object that attach to pin, top left button;
ezButton button2(7); // create ezButton object that attach to pin, bottom left button;
ezButton button3(8); // create ezButton object that attach to pin, top right;
ezButton button4(9); // create ezButton object that attach to pin, bottom right;
int ticker = 0;
int countbutton2;
int countbutton3;
int countbutton4;
void setup() {
Serial.begin(9600); //kick off serial
//button1.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds
button2.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds
button3.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds
button4.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds
/*
button2.setCountMode(COUNT_FALLING); //button counters
button3.setCountMode(COUNT_FALLING);
button4.setCountMode(COUNT_FALLING);
*/
}
//---------------------------------------------------
//----------------------------------------------------Main Loop----------------------
void loop(){
ticker ++;
if (ticker > 99999) {ticker = 0;}
[whichButton()];
//Serial.println(whichButton()); //put code herewhichButton();
if (countbutton2 == 1) {Button2CodeA();}
if (countbutton2 == 2) {Button2CodeB();}
if (countbutton3 == 1) {Button3CodeA();}
if (countbutton3 == 2) {Button3CodeB();}
if (countbutton3 == 3) {Button3CodeC();}
}
bool toggle(bool ¤tState) {
currentState = !currentState; // Toggle the state
return currentState; // Return the new state
}
//--------------------------button functions----------------------------------------
int whichButton(){
button2.loop(); // MUST call the loop() function first
button3.loop(); // MUST call the loop() function first
button4.loop(); // MUST call the loop() function first
if (button2.isPressed()) {
// countbutton2 = 0;
// countbutton3 = 0;
countbutton2 ++;
return 2;
}
if (button3.isPressed()) {
// countbutton2 = 0;
// countbutton4 = 0;
// countbutton3 ++;
return 3;
}
if (button4.isPressed()) {
// countbutton2 = 0;
// countbutton3 = 0;
// countbutton4 ++;
return 4;
}
}
void Button2CodeA(){
Serial.println("dipslay turns"); //put code here
resetAllButtonCounts();
countbutton2 = 1;
}
void Button2CodeB(){
Serial.println("displays layers"); //put code here
resetAllButtonCounts();
countbutton2 = 2;
}
void Button3CodeA(){
Serial.println("displays start setting"); //put code here
resetAllButtonCounts();
countbutton3 = 1;
}
void Button3CodeB(){
Serial.println("pot1 live"); //put code here
resetAllButtonCounts();
countbutton3 = 2;
}
void Button3CodeC(){
Serial.println("Traverse Settting"); //put code here
resetAllButtonCounts();
countbutton3 = 3;
}
void Button3CodeBlonghold(){
Serial.println("Start Postition Setteing");
}
void Button3CodeClonghold(){
Serial.println("Traverse toggle");
}
void Button4CodeA(){
Serial.println("JSON Turns");
}
void Button4CodeB(){
Serial.println("JSON Windings");
}
void Button4CodeC(){
Serial.println("JSON Layers");
}
void Button4CodeD(){
Serial.println("JSON wire Diameter");
}
void resetAllButtonCounts(){
button2.resetCount();
button3.resetCount();
button4.resetCount();
}
//--------------------------Function setting Traverse -----------------------------------
/*void SetStart(){
motorX.setCurrentPosition(0); //resets motor postitions
motorY.setCurrentPosition(0);
AutoTrav = false;
delay (400);
while (readpot1 == 1) {
button3.loop();
pot1 = (analogRead(A0));
long pot1pos = map(pot1,0, 1032, 0, 1000);
pot1pos = roundToNearest(pot1pos);
updateDisplay(myDispInt, myDispString);
if (motorX.currentPosition() != pot1pos) {
runAllSimultaneously(0, pot1pos);
myDispInt = pot1;
}
if (button3.isReleased()){
menu2sub = 0;
menu3sub = 0;
readpot1 = 0;
break;
}
}
}
*/
// ------------------Button 2 code------------------
/*
else if (button2.isPressed() && menu2sub == 0) {
menu3sub = 0;
myDispInt = TurnsCount;
myDispString = "t ";
menu2sub ++;
readpot1 = 0;
menulongString = 0;
}
else if (button2.isPressed() && menu2sub == 1) {
menu3sub = 0;
myDispInt = LayerCount;
myDispString = "L ";
menu2sub = 0;
readpot1 = 0;
menulongString = 0;
}
// Button 3 code
else if (button3.isReleased() && menu3sub == 0) {
myDispString = "Str";
myDispInt = TravStartSetpoint;
updateDisplay(myDispInt, myDispString);
menu3sub ++;
readpot1 = 0;
menulongString = 0;
}
else if (button3.isReleased() && menu3sub == 1) { // Set traverse postition
myDispString = "P1 ";
updateDisplay(myDispInt, myDispString);
readpot1 = 1;
menu3sub ++;
menulongString = 0;
SetStart();
}
else if (button3.isReleased() && menu3sub == 2) {
if(travEnabled == true){UpdateDisplayWithMessage("tRAV ENA");}
else {UpdateDisplayWithMessage("tRAV DIS");}
menulongString = 1;
readpot1 = 0;
m3 = 1;
menu3sub = 0;
}
// Button 4 code
else if (button4.isPressed() && menu4sub == 0) {
menu3sub = 0;
myDispInt = turns;
myDispString = "SEt";
menu4sub++;
readpot1 = 0;
menulongString = 0;
}
else if (button4.isPressed() && menu4sub == 1) {
menu3sub = 0;
myDispInt = wiLen;
myDispString = "LEN";
menu4sub++;
readpot1 = 0;
menulongString = 0;
}
else if (button4.isPressed() && menu4sub == 2) {
menu3sub = 0;
myDispInt = wiLay;
myDispString = "LAy";
menu4sub++;
readpot1 = 0;
menulongString = 0;
}
else if (button4.isPressed() && menu4sub == 3) {
menu3sub = 0;
myDispInt = wiDia;
myDispString = "DIA";
menu4sub = 0;
readpot1 = 0;
menulongString = 0;
}
/*
if (button3.getState() == LOW) {
if (!buttonPressed) {
buttonPressed = true;
buttonDownTime = millis();
} else if (millis() - buttonDownTime > 2000 && menu3sub == 2) {
TravStartSetpoint = analogRead(A0);
myDispInt = TravStartSetpoint;
Serial.println(TravStartSetpoint);
menulongString = 0;
updateDisplay(myDispInt, myDispString);
delay(250);
for (int i = 0; i < 8; i++) {
lc.clearSegment(i);
}
delay(250);
updateDisplay(myDispInt, myDispString);
delay(250);
for (int i = 0; i < 8; i++) {
lc.clearSegment(i);
}
delay(250);
updateDisplay(myDispInt, myDispString);
} else if (millis() - buttonDownTime > 2000 && m3 == 1) {
toggle(travEnabled);
// delay(200);
if(travEnabled == true){UpdateDisplayWithMessage("tRAV ENA");}
else {UpdateDisplayWithMessage("tRAV DIS");}
delay(250);
for (int i = 0; i < 8; i++) {
lc.clearSegment(i);
}
delay(250);
if(travEnabled == true){UpdateDisplayWithMessage("tRAV ENA");}
else {UpdateDisplayWithMessage("tRAV DIS");}
delay(250);
for (int i = 0; i < 8; i++) {
lc.clearSegment(i);
}
delay(250);
if(travEnabled == true){UpdateDisplayWithMessage("tRAV ENA");}
else {UpdateDisplayWithMessage("tRAV DIS");}
m3=0;
}
}
else {
buttonPressed = false;
}
*/
// read analogue pots at end if loop if in a menu mode
/*
if (readpot1 == 1) {
motorX.setCurrentPosition(0); //resets motor postitions
motorY.setCurrentPosition(0);
AutoTrav = false;
while (readpot1 == 1) {
button3.loop();
pot1 = (analogRead(A0));
long pot1pos = map(pot1,0, 1032, 0, 1000);
pot1pos = roundToNearest(pot1pos);
updateDisplay(myDispInt, myDispString);
if (motorX.currentPosition() != pot1pos) {
runAllSimultaneously(0, pot1pos);
myDispInt = pot1;
}
if (button3.isPressed()){
menu2sub = 0;
menu3sub = 0;
readpot1 = 0;
break;
}
}
*/
// end loop