But the problem is that don't work as i want, i would that if i press Btn1 does something, and if i click Btn2 does somethingelse. Here is the function "Ispressed":
bool Ispressed(int pin_button){
int stato = 0;
delay(50);
stato = digitalRead(pin_button);
delay(50);
if(stato == HIGH){
return true;
}
}
So, if you have a better idea you can help me remake this part of code. What i want is that with 2 buttons, if i click btn2, start function "imp2();", and if i click btn1, start function "imp1_1();", but i want that the code stop untill one of the two button is clicked, thanks.
In practice he reads the voltage that passes between the buttons and based on what is pressed Arduino reads the voltage and determines which button was pressed through a very simple IF:
void loop()
{
int value = analogRead(A0);
if (value > 800 && value < 1000) {
Serial.println("Button1");
} else if (value > 750 && value < 800) {
Serial.println("Button2");
} else if (value > 650 && value < 750) {
Serial.println("Button3");
} else if (value > 480 && value < 650) {
Serial.println("Button4");
} else if (value < 480) {
Serial.println("Button5");
}
delay(500);
}
Hope this is usefull for somebody trying do make a menu with submenu. So i created this:
@Tiziano_Faverzani A:1. You have a system with two buttons that are connected with external pull-down resistors. Correct? (The buttons can be connected with internal pull-up resistors enabled, Fig-1.)
2. Your system should not do anything until one of the buttons is activated/pressed. Correct?
3. If Button1 is pressed you execute the routine named: imp1_1(). Correct? 4. If Button2 is pressed you execute the routine named: imp2(). Correct?
B: Then why don't you proceed to wrtite the following sequential codes to realize your objectives? 1. Connect your buttons as per Fig-1 with internal pull-up resistors.
Figure-1:
2. Let us agree that L (Built-in LED of UNO) will blink for three times when Button-1 is closed. This is your function named: imp1_1().
3. Let us agree that LED1 will blink for five times when Button-2 is closed. This is your function named: imp2().
4. Upload the following sketch and check the functionality of the circuit by pressing Button1 and Button2.
#define L 13
#define LED2 6
void setup()
{
Serial.begin(9600);
pinMode(8, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(L, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.println(!Button1.read(), BIN);
}
void loop()
{
while((digitalRead(8) == HIGH) && (digitalRead(12) == HIGH))
{
; //wait until one of the buttons is closed
}
if(digitalRead(8) == LOW)
{
imp(L, 3); //Button1 is pressed, blink L 3 times
}
if(digitalRead(12)== LOW)
{
imp(LED2, 5); //Button2 is pressed, blink LED2 for 5 times
}
}
void imp(int m, int n)
{
for(int i=0; i<n; i++)
{
digitalWrite(m, HIGH);
delay(1000);
digitalWrite(m, LOW);
delay(1000);
}
}