Is there a way to make the code smaller (less space on arduino)
it uses a lot of variables (Yes i made it)
I do not know how to make the same logic with less variables.
onbuttonReleased
buttonPressed
holdbutton
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
initalizeSetup();
}
void loop() {
// put your main code here, to run repeatedly:
if(buttonPressed(0)){
Serial.println("b1 down");
}
/* pin, repeat each 500 millis, if you hold it*/
if(buttonPressed(1,500)){
Serial.println("b2 down");
}
/* pin, repeat each 500 millis, after 5 seconds*/
if(buttonPressed(2,500,5000)){
Serial.println("b3 down");
}
if(buttonPressed(3)){
Serial.println(isbuttonDown(1)?"b1 is down":"b1 is up");
}
if(onbuttonReleased(0)){
Serial.println("b1 realeased");
}
if(onbuttonReleased(1)){
Serial.println("b2 realeased");
}
if(onbuttonReleased(2)){
Serial.println("b3 realeased");
}
if(onbuttonReleased(3)){
Serial.println("b4 realeased");
}
}
/*define pins for the buttons*/
/*do not forget to change number_of_buttons*/
const int buttonpins[] = {A0,A1,3,4};
const int number_of_buttons = 4;
/*remove if you dont need*/
bool btdown[number_of_buttons];
bool cycle[number_of_buttons];
bool registeredPress[number_of_buttons];
bool btreleased[number_of_buttons];
bool RepeatPressedActionifHolding[number_of_buttons];
int btnreleaseinterval[number_of_buttons];
int RepeatPressedActionInterval[number_of_buttons];
int RepeatPressedActionAfter_X_millis_Interval[number_of_buttons];
unsigned long previousMillis[number_of_buttons];
unsigned long FirstMillis[number_of_buttons];
unsigned long currentMillis=0;
void initalizeSetup(){
//int *myArray = (int *) malloc(numberOfEntries * sizeof(int));
for(int i=0;i<number_of_buttons;i++){
/*use pullup inside arduino*/
/*connect switch to ground not to vcc!*/
pinMode(buttonpins[i],INPUT_PULLUP);
btnreleaseinterval[i]=100;
btdown[i]=false;
previousMillis[i]=0;
registeredPress[i]=false;
btreleased[i]=false;
RepeatPressedActionifHolding[i]=false;
RepeatPressedActionAfter_X_millis_Interval[i]=0;
RepeatPressedActionInterval[i]=0;
FirstMillis[i]=0;
cycle[i]=false;
}
}
/*remove if you dont need*/
bool onbuttonReleased(int btn){
if(!registeredPress[btn]&&btreleased[btn]){
btreleased[btn]=false;
return true;
}
return false;
}
/*remove if you dont need*/
bool isbuttonDown(int btn){
return registeredPress[btn];
}
bool buttonPressed(int btn,int repeatInterval){
return buttonPressed(btn,repeatInterval,repeatInterval);
}
bool buttonPressed(int btn,int repeatInterval,int After_X_millis_Interval){
RepeatPressedActionifHolding[btn]=true;
RepeatPressedActionInterval[btn]=repeatInterval;
RepeatPressedActionAfter_X_millis_Interval[btn]=After_X_millis_Interval;
return buttonPressed(btn);
}
bool buttonPressed(int btn){
btdown[btn]=!digitalRead(buttonpins[btn]);
/*ask the time*/
currentMillis=millis();
if(btdown[btn])
{
previousMillis[btn] = currentMillis;
/*if you click the button, it will return a true*/
/*so your code will run at this point, It will only run once! except if you enable Holding*/
if(!registeredPress[btn])
{
FirstMillis[btn] = currentMillis;
registeredPress[btn]=true;
return true;
}
}
/*handle hold after first cycle*/
if(cycle[btn]&&btdown[btn]&& registeredPress[btn]&& (unsigned long)currentMillis - FirstMillis[btn] >= (unsigned long)(RepeatPressedActionInterval[btn]) && RepeatPressedActionifHolding[btn]){
FirstMillis[btn] = (unsigned long)currentMillis;
RepeatPressedActionifHolding[btn]=false;
return true;
}
/*handle hold*/
if(!cycle[btn]&&btdown[btn]&& registeredPress[btn]&& (unsigned long)currentMillis - FirstMillis[btn] >= (unsigned long)(RepeatPressedActionAfter_X_millis_Interval[btn]) && RepeatPressedActionifHolding[btn]){
FirstMillis[btn] = (unsigned long)currentMillis;
cycle[btn]=true;
RepeatPressedActionifHolding[btn]=false;
return true;
}
/*check if you are still holding the button (corrects a faulty button)*/
if(!btdown[btn]&®isteredPress[btn]&& (unsigned long)(currentMillis - previousMillis[btn]) >= btnreleaseinterval[btn])
{
cycle[btn]=false;
btreleased[btn]=true;
registeredPress[btn]=false;
}
return false;
}