Use 1 Arduino Analog Pin to Read 4 Pushbuttons

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ย. 2024

ความคิดเห็น • 24

  • @Eman2000
    @Eman2000  5 ปีที่แล้ว +4

    I have put the code for this project on github.
    github.com/Eman2000/Arduino-4-Buttons-1-Pin

    • @gangrenn
      @gangrenn 4 ปีที่แล้ว

      Would this work for leds ? (connecting multiple leds to one arduino pin and control them individualy)

  • @omaraissani6255
    @omaraissani6255 4 ปีที่แล้ว +1

    Great hack! if you use only one digital input you can use it for interruption, this could be done by adding an op amp in comparator configuration, comparing Vout: the output of your circuit with Vo: the least voltage that can be provide when pressing a button, if Vout >= Vo the op amp gives us 5v this is the signal that triggers the interrupt routine which is just reading Vout i.e knowing which button was pressed.

  • @t3kn079
    @t3kn079 4 ปีที่แล้ว +1

    Hey man thanks i was needing like20 buttons on a project and had 12 inputs thankyou for this information

  • @hunkymcstudleypants7722
    @hunkymcstudleypants7722 2 ปีที่แล้ว +1

    You can push multiple buttons by choosing a different resistor value for each button and putting them in parallel rather than series

  • @Eman2000
    @Eman2000  6 ปีที่แล้ว

    //The code:
    // These constants won't change. They're used to give names to the pins used:
    const int analogInPin = A5;
    const int outPin = 13; // Analog output pin that the LED is attached to
    int sensorValue = 0; // value read from the buttons
    int state = 0; // used for storing what button was pressed
    void setup() {
    Serial.begin(9600); // initialize serial communications at 9600 bps
    pinMode(outPin, OUTPUT); // set pin 13 as an output
    }
    void loop() {
    // print the results to the Serial Monitor:
    readSwitch();
    Serial.print("Value = ");
    Serial.println(sensorValue);
    Serial.print("State = ");
    Serial.println(state);
    if (state == 0) { //turns the LED off if a certain button is pressed
    digitalWrite(outPin, LOW);
    readSwitch(); //contuines to poll the switch circuitry
    }
    if (state == 1) { //runs a program similar to the blink schetch if a certain button is pressed
    for (int i = 0; i < 2000; i++) { //set a for loop that counts up to 2000
    if (i < 1000) { //if the for loop value is less than 1000 turn on the LED
    digitalWrite(outPin, HIGH);
    readSwitch(); //contuines to poll the switch circuitry also provides about a 1 ms delay
    }
    if (i > 1000) {
    digitalWrite(outPin, LOW); //if the for loop value is more than 1000 turn off the LED
    readSwitch(); //contuines to poll the switch circuitry also provides about a 1 ms delay
    }
    }
    }
    if (state == 2) { //creates the 2 flash sequence of the LED and polls the switches between delays
    digitalWrite(outPin, HIGH);
    delay(100);
    readSwitch();
    digitalWrite(outPin, LOW);
    delay(100);
    readSwitch();
    digitalWrite(outPin, HIGH);
    delay(100);
    readSwitch();
    digitalWrite(outPin, LOW);
    delay(250);
    readSwitch();
    }
    if (state == 3) { //Creates the fast blink effect and polls the switches after one cycle of the blink effect
    digitalWrite(outPin, HIGH);
    delay(75);
    digitalWrite(outPin, LOW);
    delay(75);
    readSwitch();
    }
    }
    void readSwitch () { //creates the function to read the switch states
    sensorValue = analogRead(analogInPin); //read the analog pin the switches are connected to
    if (sensorValue < 800 && sensorValue > 750) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
    state = 0;
    delay(100); //delay for debounce purposes
    }
    if (sensorValue < 749 && sensorValue > 650) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
    state = 1;
    delay(100); //delay for debounce purposes
    }
    if (sensorValue < 649 && sensorValue > 425) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
    state = 2;
    delay(100); //delay for debounce purposes
    }
    if (sensorValue < 425 && sensorValue >= 0) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly
    state = 3;
    delay(100); //delay for debounce purposes
    }
    delay(1); //delay for timeing purposes
    }

    • @brockw5857
      @brockw5857 5 ปีที่แล้ว

      Keeps giving the error of "stray '\357' in program"
      It gives the error for the last line of code. Great video though!
      (Update) Fixed error but still doesn't work.

  • @abdullahtok2706
    @abdullahtok2706 2 ปีที่แล้ว

    Very very very clever.

  • @jamest.5001
    @jamest.5001 6 ปีที่แล้ว +3

    I wondered if this was possible. wasn't sure how to complete the task. I know nothing about programming!

  • @punjabilog
    @punjabilog 3 ปีที่แล้ว

    Its Great toutrial Thank you very much sir.

  • @lijosmedia
    @lijosmedia 2 ปีที่แล้ว

    How to use high digital input as a single pulse in Arduino

  • @eelipulkkinen4888
    @eelipulkkinen4888 5 ปีที่แล้ว

    Thank you! This video is usefull

  • @sucbsakulmehi4261
    @sucbsakulmehi4261 2 ปีที่แล้ว

    How to select the proper resistor for more buttons?

  • @Barnyardbuilds286
    @Barnyardbuilds286 2 ปีที่แล้ว

    will this work with a relay? i am trying to figure out how i can change the delay with a push button. to make it slower or faster.

  • @brockw5857
    @brockw5857 5 ปีที่แล้ว

    Doesn't work. Sorry for commenting so much but after fixing the error code nothing happens. I upload it and once everything is hooked up properly, it stays stuck on the blink command. The serial monitor just says, "
    state = 3
    value = 0
    "

    • @Eman2000
      @Eman2000  5 ปีที่แล้ว

      Sorry it took so long for me to respond. If you are still trying to make this work I put the code on github.
      github.com/Eman2000/Arduino-4-Buttons-1-Pin

  • @321fire9
    @321fire9 5 ปีที่แล้ว

    which you used resistance, 10k; 1/4W, 2W, 1W ?
    Please send me answer.

    • @RapidBoss08
      @RapidBoss08 4 ปีที่แล้ว

      Probably way too late, but 1/4 watt works just fine but since its such low current and voltage, basically any wattage works.

  • @kawanosman6748
    @kawanosman6748 4 ปีที่แล้ว

    very helpful than u 😘😘😘

  • @Jjjj119-r8c
    @Jjjj119-r8c 4 ปีที่แล้ว

    does this work with 9 buttons as well?

  • @LMSTUDI0
    @LMSTUDI0 3 ปีที่แล้ว

    have you try to make a code that will translate this as a push buttons in game ? how to code it with some joystick/keybord library to make it work as a game kontroler ? Im flying planes in Digital Combat Simulator and a lot of buttons are needed ;)

  • @shakeelrasheed4367
    @shakeelrasheed4367 5 ปีที่แล้ว +1

    need programming code please

    • @Eman2000
      @Eman2000  5 ปีที่แล้ว

      Check the pinned comment please.

    • @brockw5857
      @brockw5857 5 ปีที่แล้ว

      @Jens Tiefschneider But it's not