[half Solved, Still in doubt]button function return wrong

int buttonState[5]={0,0,0,0,0};
int lastButtonState[5]={0,1,1,1,1};

Low is high, because I use INPUT_PULLUP here.

int bPress(int bPin){                                                             //button function
buttonState[bPin]=digitalRead(buttonPin[bPin]);

if(buttonState[bPin]!=lastButtonState[bPin]){
  lastButtonState[bPin]=buttonState[bPin];
  if(buttonState[bPin]==LOW)return 1;
  else return 0;


}      
}

it won’t work properly. changed test to low, test it work half way. my testVal increases very fast. what is wrong here?

if(bPress(1)==0) {mode++; testVal++;}

if(bPress(2)==0)digitalWrite(ledPin[1], HIGH);
else digitalWrite(ledPin[1], LOW);

if(bPress(3)==0)digitalWrite(ledPin[2], HIGH);
else digitalWrite(ledPin[2], LOW);

if(bPress(4)==0)digitalWrite(ledPin[3], HIGH);
else digitalWrite(ledPin[3], LOW);

+++
testVal return 1 when button 2 is not pressed; return 0 when it it pressed. why???

testVal=bPress(2);

I don’t think this is a smart way to do that, but it works. still don’t understand why the previous code return 1 instead of 0.

//------------------------------------------------------------------>> Buttons

if(buttonPress[1]!=lastButtonPress[1]){
  if(buttonPress[1]==HIGH){
    digitalWrite(ledPin[1], HIGH);
    testVal++;
  }
  else digitalWrite(ledPin[1], LOW);
}
lastButtonPress[1]=buttonPress[1];



for(int x=1; x<5; x++){  
buttonState[x]=digitalRead(buttonPin[x]);

if(buttonState[x]!=lastButtonState[x]){
  lastButtonState[x]=buttonState[x];
  if(buttonState[x]==LOW)buttonPress[x]=1;
  else buttonPress[x]=0;
}

} 

//------------------------------------------------------------------<< Buttons

Please do us, and yourself, a favour and post a complete program that exhibits the problem rather than bits and pieces.

I don’t have the full code now. I have rewritten it already. still have problem. the lcd is not displaying correctly.

#include <LiquidCrystal.h>

LiquidCrystal lcd(2,3,4,5,6,7);

byte timeIcon[8] = {
	0b11111,
	0b10001,
	0b01010,
	0b00100,
	0b00100,
	0b01010,
	0b10001,
	0b11111};

byte tempIcon[8] = {
        0b00100,
	0b01011,
	0b01010,
	0b01011,
	0b01110,
	0b11111,
	0b11111,
	0b01110};

byte batteryIcon[8] = {
	0b01110,
	0b11011,
	0b10001,
	0b11111,
	0b10001,
	0b11111,
	0b10001,
	0b11111};

byte lightIcon[8] = {
	0b00000,
	0b10101,
	0b01110,
	0b11011,
	0b01010,
	0b01110,
	0b10101,
	0b00000};

byte arrowupIcon[8] = {
	0b00100,
	0b00100,
	0b01110,
	0b11111,
	0b00100,
	0b01110,
	0b01110,
	0b01010};

byte buttonIcon[8] = {
	0b00000,
	0b11111,
	0b10001,
	0b10101,
	0b10101,
	0b10001,
	0b11111,
	0b00000
};

byte alarmIcon[8] = {
	0b00100,
	0b01110,
	0b01110,
	0b01110,
	0b01110,
	0b01110,
	0b11111,
	0b00100
};

int timeI=0;
int tempI=1;
int batteryI=2;   
int lightI=3; 
int arrowupI=4;   
int buttonI=5;  
int alarmI=6;  


#define tempI char(0x01)
#define batteryI char(0x02)   
#define lightI char(0x03) 
#define arrowupI char(0x04)   
#define buttonI char(0x05)
#define alarmI char(0x06)
#define timeI char(0x07)
//====================================================================================================================================
//====================================================================================================================================

// Pinouts =====>
int buttonPin[6]={0,A4,A3,A2,A1}; // Button 1-4
int ledPin[7]={0,8,9,10,11,12};   // Led 1-5
int shuntPin=A5;               
int tempPin=A6; 
int lightPin=A7;
int beeperPin=A0;


// TimeValues  ======>
int DAY=0; //Day
int HR=0; //hour
int MIN=0; //minute
int SEC=0; //Second
int lastSec=0;
unsigned long previousSec=0;

//   ======> sensorValues
int shuntMa=0;
int shuntMah=0;
unsigned long shuntMatotal=0;
int tempF=0;
int tempC=0;
int lightLevel=0;

//   ======> menus
int mode=1;
//   ======> button
int buttonState[5]={0,0,0,0,0};
int lastButtonState[5]={0,1,1,1,1};
int buttonPress[5]={0,0,0,0,0};
//   ======>


int testVal=0;
//====================================================================================================================================
//====================================================================================================================================

void setup() {
  
  //===5 RED LEDs ===
for(int x=1; x<7; x++){
  pinMode(ledPin[x],OUTPUT);
}  
  //===4 Black buttons ===  
for(int x=1; x<5; x++){
  pinMode(buttonPin[x], INPUT_PULLUP);
}  
  //===Sensors ===
  pinMode(shuntPin, INPUT);// shunt voltage   
  pinMode(tempPin, INPUT);// Temp. Sensor  
  pinMode(lightPin, INPUT);// Light resistor   
  pinMode(beeperPin, OUTPUT); // Beeper  
  
  //===LCD SETUP ===  
  lcd.begin(16,2);
  lcd.createChar(7, timeIcon);
  lcd.createChar(1, tempIcon);
  lcd.createChar(2, batteryIcon);   
  lcd.createChar(3, lightIcon);  
  lcd.createChar(4, arrowupIcon);   
  lcd.createChar(5, buttonIcon);  
  lcd.createChar(6, alarmIcon);  
  
}

//====================================================================================================================================
//====================================================================================================================================
void loop() {

   
  
//------------------------------------------------------------------>>LCD 
if(mode==1){

  char x [16];
  sprintf(x, "%c%02d:%02d:%02d:%02d %02d%c", timeI,DAY,HR,MIN,SEC,tempC,tempI); 
  lcdPrintText(0,0,x);
    
  char y [16];
  sprintf(y, " %cM %c+ %c- %cY %003d", buttonI,  buttonI, buttonI, buttonI, testVal );
  lcdPrintText(0,0,y);
  
}

if(mode==2){
lcd.clear();   
}

if(mode==3)mode=1;

/*


  char tempRead[5];
  sprintf(tempRead, "%02d %02d", lightLevel,tempC);  
  lcd.setCursor(10, 0);    
  lcd.print(tempRead);    
  
  char shuntRead[14];
  sprintf(shuntRead,"%04dmA %04dMah",shuntMa, shuntMah);
   lcd.setCursor(1, 1); 
  lcd.print(shuntRead);   
  
  */
     
//------------------------------------------------------------------<<LCD


//------------------------------------------------------------------>>Time
unsigned long currentSec=millis();
if(currentSec-previousSec>1000) {
SEC++; 
previousSec=currentSec;
if(shuntMa>1){shuntMatotal=shuntMatotal+shuntMa;}
}

if(SEC==60){SEC=0; MIN++;}
if(MIN==60){MIN=0; HR++;}
if(HR==24) {HR=0;DAY++;}
//------------------------------------------------------------------<<Time




//------------------------------------------------------------------>> Sensors
// 8% difference from meter to arduino reading
int shuntVal= analogRead(shuntPin);
shuntMa=shuntVal*(5.0/1023.0*1000.0*0.92);              //shuntMa
shuntMah=shuntMatotal/3600;                            //shuntMah

//(21%) tempVal*0.21=F degree/ (F-32)X5/9=C degree 
int tempVal= analogRead(tempPin);
tempF=tempVal*0.21;
tempF= constrain (tempF, 0,99);                      // tempF
tempC=((tempVal*0.21)-32)*5/9;                       // tempC

//darkest 50-1010 lightest
int lightVal= analogRead(lightPin);
lightVal=map(lightVal, 50,1010,0,99);
lightLevel=constrain (lightVal, 0, 99);             // lightLevel

//------------------------------------------------------------------<< Sensors

//------------------------------------------------------------------>> Buttons
if(buttonPress[1]==1) {mode++; }

if(buttonPress[2]==1)digitalWrite(ledPin[1], HIGH);
else digitalWrite(ledPin[1], LOW);

testVal=buttonPress[2];

 
buttonState[1]=digitalRead(buttonPin[1]);

if(buttonState[1]!=lastButtonState[1]){
  if(buttonState[1]==LOW)buttonPress[1]=1;
  else buttonPress[1]=0;
}
  lastButtonState[1]=buttonState[1];
  
  
buttonState[2]=digitalRead(buttonPin[2]);

if(buttonState[2]!=lastButtonState[2]){
  if(buttonState[2]==LOW)buttonPress[2]=1;
  else buttonPress[2]=0;
}
  lastButtonState[2]=buttonState[2];  


//------------------------------------------------------------------<< Buttons



//------------------------------------------------------------------>>
//------------------------------------------------------------------<<
//------------------------------------------------------------------>>
//------------------------------------------------------------------<<
//------------------------------------------------------------------>>
//------------------------------------------------------------------<<

}


//========================================================================================================>>FUNCTIONS

int bPress(int bPin){                                                             //button function
     
}  
  

void lcdPrintText (int x, int y, char* text){
  
  lcd.setCursor(x,y);  
  lcd.print(text);
}  


//------------------------------------------------------------------<<

In the program that you just posted this is the bPress() function

int bPress(int bPin){                                                             //button function

}

Mind you, as it is never called the fact that it does nothing is not a problem.

Can you please describe the problem with your current program ?

By the way, it is a good idea to save versions of your program as it evolves/changes so that you can go back and see what you did and how/why.