I got the OneButton library working with 3 buttons with the "click" and "press" functions with great help from the forum. I have tried to apply what I have learned about the function of the OneButton library into my on going project but I just don't know where to put it or how to add the functions that I want. I have 3 buttons //Abttn,Bbttn,Cbttn//that I want to either read values from arrays//Amem,Bmem,Cmem// on a click or write new values to the same arrays on a press. I have the code working except for the dual functions of the 3 buttons. (there is some lcd formatting that is wrong because I need a 2X20 instead of a 2X16)
#include <LiquidCrystal.h>//Get the LCD library
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );//Assign the LCD pins
//Assign the input buttons
int upbttnF = 15;
int dwnbttnF = 16;
int upbttnR = 17;
int dwnbttnR = 18;
int Abttn = 11;
int Bbttn = 12;
int Cbttn = 13;
//initalize button states
int upbttnFState = 0;
int dwnbttnFState = 0;
int upbttnRState = 0;
int dwnbttnRState = 0;
int AbttnState = 0;
int BbttnState = 0;
int CbttnState = 0;
int bttnct = 0;
//initalize step counter varibles
int countF = 1;
int countR = 1;
int countFnew = 1;
int countRnew = 1;
//initalize memory value varible arrays
int myAVals[3] = {1,1};
int myBVals[3] = {14,14};
int myCVals[3] = {28,28};
//values for button cases, these are constants
#define nobutton 0
#define upbttn_F 1
#define dwnbttn_F 2
#define upbttn_R 3
#define dwnbttn_R 4
#define Amem 5
#define Bmem 6
#define Cmem 7
//Variables
byte buttonJustPressed = false;
byte buttonJustReleased = false;
byte buttonWas =nobutton;
void setup()
{//start setup function
//Assign buttons to inputs
Serial.begin(9600);
pinMode(upbttnF,INPUT);
pinMode(dwnbttnF,INPUT);
pinMode(upbttnR,INPUT);
pinMode(dwnbttnR,INPUT);
pinMode(Abttn,INPUT);
pinMode(Bbttn,INPUT);
pinMode(Cbttn,INPUT);
// Set up the LCD screen
//code omitted for post
void loop()
{//start void function
//Check button status
Serial.println("start void loop");
byte button;
button=ReadButtons();//run the ReadButtons function
if (buttonJustPressed || buttonJustReleased)
Serial.println(button);
//Change the value of the position counters
switch (button)
{ //start switch function
case nobutton:
{
break;
}
case upbttn_F:
{
Serial.println("up button F pushed");
countF = countF+1;
countF = constrain (countF,1,28);
lcd.setCursor(6,0);
lcd.print(" ");//clears previous data for justification
lcd.setCursor(6,0);
lcd.print (countF);//prints new value of countF
break;
}
case dwnbttn_F:
{
countF = countF-1;
countF = constrain (countF,1,28);
Serial.print("down button F pushed");
lcd.setCursor(6,0);
lcd.print(" ");
lcd.setCursor(6,0);
lcd.print(countF);
break;
}
case upbttn_R:
{
countR = countR+1;
countR = constrain (countR,1,28);
Serial.println("up button R pushed");
lcd.setCursor(14,0);
lcd.print(" ");
lcd.setCursor(14,0);
lcd.print(countR);
break;
}
case dwnbttn_R:
{
countR = countR-1;
countR = constrain (countR,1,28);
Serial.print("down button R pushed");
lcd.setCursor(14,0);
lcd.print(" ");
lcd.setCursor(14,0);
lcd.print(countR);
break;
}
case Amem:
{
myAVals [0] = countF;
myAVals [1] = countR;
Serial.println("Memory A pressed");
lcd.setCursor(1,1);
lcd.print(" ");
if (countF >9)
lcd.setCursor(1,1);
else
lcd.setCursor(2,1);
lcd.print(countF);
lcd.setCursor(4,1);
lcd.print(" ");
lcd.setCursor(4,1);
lcd.print(countR);
break;
}
case Bmem:
{
myBVals [0] = countF;
myBVals [1] = countR;
Serial.println("Memory B pressed");
lcd.setCursor(7,1);
lcd.print(" ");
if (countF>9)
lcd.setCursor(7,1);
else
lcd.setCursor(8,1);
lcd.print(countF);
lcd.setCursor(10,1);
lcd.print(" ");
lcd.setCursor(10,1);
lcd.print(countR);
break;
}
case Cmem:
{
myCVals [0] = countF;
myCVals [1] = countR;
Serial.println("Memory C pressed");
lcd.setCursor(13,1);
lcd.print(" ");
if (countF>9)
lcd.setCursor(13,1);
else
lcd.setCursor(14,1);
lcd.print(countF);
lcd.setCursor(14,1);
lcd.print(" ");
lcd.setCursor(14,1);
lcd.print(countR);
break;
}
default:
{
break;
}
}//end switch function
if(buttonJustPressed)
buttonJustPressed=false;
if(buttonJustReleased)
buttonJustReleased=false;
Serial.println("end void loop");
}//end of void loop
byte ReadButtons()
{//start ReadButtons function
Serial.println("Start ReadButtons");
byte button = nobutton;// assigns button value to 0
upbttnFState = digitalRead(upbttnF);
delay (50);
if (upbttnFState == HIGH)
{
button = upbttn_F;
}
dwnbttnFState = digitalRead(dwnbttnF);
delay (50);
if (dwnbttnFState == HIGH)
{
button = dwnbttn_F;
}
upbttnRState = digitalRead(upbttnR);
delay (50);
if (upbttnRState == HIGH)
{
button = upbttn_R;
}
dwnbttnRState = digitalRead(dwnbttnR);
delay (50);
if (dwnbttnRState == HIGH)
{
button = dwnbttn_R;
}
AbttnState = digitalRead(Abttn);
delay (50);
if (AbttnState == HIGH)
{
button = Amem;
}
BbttnState = digitalRead(Bbttn);
delay (50);
if (BbttnState == HIGH)
{
button = Bmem;
}
CbttnState = digitalRead(Cbttn);
delay (50);
if (CbttnState == HIGH)
{
button = Cmem;
}
if((buttonWas==nobutton)&&(button!=nobutton))
{
buttonJustPressed=true;
buttonJustReleased=false;
}
if((buttonWas!=nobutton)&&(button==nobutton))
{
buttonJustPressed=false;
buttonJustReleased=true;
}
buttonWas = button;
Serial.println("Button value from ReadButtons");
Serial.println(button);
return(button);
}//end ReadButtons function
part II to follow