Single button Multiple functions

xawos_newbie: yes, i did. One wire now goes to pin 9, the other to vcc

So no, you didn't. The internal pullup that you enabled will pull the input HIGH. Pressing the button will also pull the input HIGH. The other pin should be connected to GND.

Arrch:
So no, you didn’t. The internal pullup that you enabled will pull the input HIGH. Pressing the button will also pull the input HIGH. The other pin should be connected to GND.

ROTFLMAO Thanks a lot, that solves the problem =)
I’ll post the schematics corrected and the code if someone wants to look at it.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int inbutton = 9;
int buttonstate = LOW;
int lastbuttonstate = HIGH;
int actual;
int trigpin = A4;
int echopin = A5;
int aPinIn = A0;
int val = 0; 
float Vout = 0.0;
float Vin = 4.66; 
float Rknown = 10000.0; 
float Runknown = 0.0;
float U = 0.0;
float I = 0.0; 
int backlight = 13;
int analogPin = 1; 
int chargePin = 6;      
int dischargePin = 7;  
float resistorValue = 1000.0;   
unsigned long startTime;
unsigned long elapsedTime;
float microFarads;              
float nanoFarads;
float picoFarads;

void setup(){
 pinMode(inbutton, INPUT);
 digitalWrite(inbutton, HIGH);
 lcd.begin(16, 2);
 pinMode(backlight, OUTPUT);
 pinMode(chargePin, OUTPUT);   
 digitalWrite(chargePin, LOW); 
 pinMode(trigpin, OUTPUT);
 pinMode(echopin, INPUT);
 digitalWrite(backlight, HIGH);
} 



void loop()
{
    buttonstate = digitalRead(inbutton);
if (buttonstate == HIGH && buttonstate != lastbuttonstate)
    {
      if(actual == 3) actual = 0;
      else actual++;
    }
  lastbuttonstate = buttonstate;
 
  switch(actual)
  {
    case 0:
    range();
    break;

    case 1:
    ohm();
    break;

    case 2:
    farad();
    break;

   }
}

void range() {
  float duration; 
  float cm;
  pinMode(trigpin, OUTPUT);
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);

  pinMode(echopin, INPUT);
  duration = pulseIn(echopin, HIGH);
 
  cm = microsecondsToCentimeters(duration);
  
  if (cm > 500){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Fuori raggio");
    lcd.setCursor(0,1);
    lcd.print("d'azione");
    delay(500);
  }
  
  else {

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("L'oggetto dista");
  lcd.setCursor(0, 1);
  lcd.print(cm);
  lcd.print(" cm");
  delay(500);
  }
 }
float microsecondsToCentimeters(float microseconds){
return microseconds / 29 / 2;
}

 

void ohm(){
lcd.clear();
 val = analogRead(aPinIn);
 Vout = (Vin/1024.0) * val;
 U = (Vin - Vout);
 I = (U/Rknown);
 Runknown = (Vout/I);
 lcd.print("Vout: ");
 lcd.print(Vout);
lcd.setCursor(0, 1); 
 lcd.print("R: ");
 lcd.print(Runknown);
 delay(1000);
}


 void farad(){
   digitalWrite(chargePin, HIGH);  
   startTime = millis();
   while(analogRead(analogPin) < 648){ 
   }
   elapsedTime= millis() - startTime; 
   microFarads = ((float)elapsedTime / resistorValue) * 1000;
    
   if(elapsedTime == 0){
     lcd.clear();
     lcd.print("Inserire un");
     lcd.setCursor(0, 1);
     lcd.print("Capacitore");     
     delay(1000);
   }
   if (microFarads >= 1){
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print((long)microFarads);   
     lcd.print("  microFarad");    
     lcd.setCursor(0,0);
   }
   else if (microFarads <= 1){

     nanoFarads = microFarads * 1000.0; 
     lcd.clear();
     lcd.setCursor(0,0);                                  
     lcd.print((long)nanoFarads); 
     lcd.print("  nanoFarad");  
     lcd.setCursor(0,0);
   }
   if (nanoFarads >= 1){
     picoFarads = nanoFarads * 1000.0; 
     lcd.clear();
     lcd.setCursor(0,0);                                  
     lcd.print((long)picoFarads);
     lcd.print("  picoFarad");  
     lcd.setCursor(0,0);
   }
 
   digitalWrite(chargePin, LOW);   
   pinMode(dischargePin, OUTPUT);   
   digitalWrite(dischargePin, LOW); 
   while(analogRead(analogPin) > 0){
                                    
   }
   pinMode(dischargePin, INPUT);   

 }

Thanks again Arcch, i should have thought about that =)

There is no patch for human stupidty =)

Problem solved, this thread can be closed (always wanted to post it XD )

An unusual solution I wouldn’t submit for grading unless you understand how it works and can make it your own.

With some research you might find it useful in the future.

//#define NDEBUG

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

#if !defined(NDEBUG)

#define DebugStr(STR)           Serial.print(STR)
#define DebugStrln(STR)         Serial.println(STR)
#define DebugDelay(MILLIS)      delay(MILLIS)

#else

#define DebugStr(STR)
#define DebugStrln(STR)
#define DebugDelay(MILLIS)

#endif

class push_button_t
{
    const uint8_t   _pin;
    const uint8_t   _transition_level;
    const uint8_t   _number_of_states;

    uint8_t         _press_counter;
    uint8_t         _state;

public:
    push_button_t(uint8_t pin, uint8_t transition_level = LOW, uint8_t number_of_states = 2)
        : _pin(pin)
        , _transition_level(transition_level)
        , _number_of_states(number_of_states)
        
        , _press_counter(0)
        , _state(0)
    {
        DebugStrln(__PRETTY_FUNCTION__);
    }

    void begin()
    {
        DebugStrln(__PRETTY_FUNCTION__);
    
        pinMode(_pin, INPUT);
    }


    // --- conversion operator, returns _press_counter

    operator uint8_t()
    {
        DebugStrln(__PRETTY_FUNCTION__);
    
        uint8_t     state = digitalRead(_pin);
        if ( state != _state )
        {
            if ( state == _transition_level )
            {
                _press_counter++;
                _press_counter %= _number_of_states;
            }
        }
        
       _state = state;

       return _press_counter;
    }
};


typedef void (*handler_ptr)(void);


const uint8_t   pinBUTTON = 4;


// switch on 'pinBUTTON', transition on HIGH and has 4 states

push_button_t   button(pinBUTTON, HIGH, 4);


void farad() { DebugStrln(__PRETTY_FUNCTION__); }
void ohm()   { DebugStrln(__PRETTY_FUNCTION__); }
void range() { DebugStrln(__PRETTY_FUNCTION__); }
void off()   { DebugStrln(__PRETTY_FUNCTION__); }

void loop()
{
    // array of handler function pointers to the desired functions handler
    // routine

    handler_ptr handlers[] = { off, range, ohm, farad };


    // invokes 'button's conversion operator using result as index to which
    // handler function to call

    (*handlers[button])();
}

void setup()
{
#if !defined(NDEBUG)
    Serial.begin(9600);
#endif

    button.begin();
}