Buttons?

Hy i have problem with implementing multiple positive edge buttons in my projevt and i have problem with buttons code for buttons is in attachments

alarm.txt (1.39 KB)

if(state1 && !previousState1)

Maybe you want:

if(state1 != previousState1)

etc.

It's not thing it this. I have try like you say but problem is in arduino hardware or compiler because code is right and when i write code from button() function in lloop() it works ok but when i call function button in loop its not work

Read these, if you still have problems ask for more help:
http://www.gammon.com.au/forum/?id=11955

http://www.thebox.myzen.co.uk/Tutorial/Inputs.html


.

when i call function button in loop its not work

What are you returning when no buttons have been pressed?

In picture is how i connect button… Im not a beginner in arduino and knows a lot but this keeps me crazy… …in serial monitor it should print only once when is button pressed. This works well with button1 button2 and button3 but when i press button4 it prints button4 and button1 constantly. When i implement button function logic in loop() it works ok but when i implement in button() all logic and call in loop() int a=button() and that print in dependence of a then i have problem ehich i deacribe above.

Added 1 line return 0; as default condition

int state1 = LOW;
int previousState1 = LOW;
int state2 = LOW;
int previousState2 = LOW;
int state3 = LOW;
int previousState3 = LOW;
int state4 = LOW;
int previousState4 = LOW;
int button1 = 30;
int button2 = 32;
int button3 = 34;
int button4 = 36;
void setup() {
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  int a = button();
  if ( a == 1)
  {
    Serial.println("BUTTON 1");
  }
  else if ( a == 2)
  {
    Serial.println("BUTTON 2");
  }
  else if ( a == 3)
  {
    Serial.println("BUTTON 3");
  }
  else if ( a == 4)
  {
    Serial.println("BUTTON 4");
  }
}
int button()
{
  state1 = digitalRead(button1);
  state2 = digitalRead(button2);
  state3 = digitalRead(button3);
  state4 = digitalRead(button4);
  if (state1 && !previousState1)
  {
    previousState1 = state1;
    return 1;
  }
  else if (state2 && !previousState2 )
  {
    previousState2 = state2;
    return 2;
  }
  else if (state3 && !previousState3)
  {
    previousState3 = state3;
    return 3;
  }
  else if (state4 && !previousState4)
  {
    previousState4 = state4;
    return 4;
  }
  else
  {
    previousState1 = state1;
    previousState2 = state2;
    previousState3 = state3;
    previousState4 = state4;
    return 0;
  }
}