HP4067 Mux convert Serial to DigitalRead

int s0 = 7;
int s1 = 8;
int s2 = 9;
int s3 = 16;

const int ledPin1 = 2;
const int ledPin2 = 3;
const int ledPin3 = 4;
const int ledPin4 = 5;
const int ledPin5 = 6;

int SIG_pin = 10;

void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(SIG_pin,INPUT);

digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);

digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);

Serial.begin(9600);
}

void loop(){

//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 16; i ++){Serial.print("value ");
Serial.print(i);
Serial.print(" is : ");
Serial.println(readMux(i));
delay(500);}

int buton1 = readMux(0); //(Problem is here) I can't take the value of readMux(0)
if(buton1==HIGH) // I want to add digitalRead stg.
{digitalWrite(ledPin1,LOW);}else{digitalWrite(ledPin1,HIGH);}
}

int readMux(int channel){
int controlPin = {s0, s1, s2, s3};
int muxChannel[16][4]={ {0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1}};//channel 15
//loop through the 4 sig
for(int i = 0; i < 4; i ++){digitalWrite(controlPin[i], muxChannel[channel][i]);}

//read the value at the SIG pin
int val = analogRead(SIG_pin);

//return the value
return val;
}

I want to use hp4067 multiplexer (for 16 button input) button box using Joystick.
I read 16 button value as serial (0 and 1023).
How can I read digitalread (covert serial to digital read)
pls help this subject.
Thk u.

Welcome to the forums. Please ready the sticky post at the top of the forum to learn how to properly post your code using code tags. It will help people help you.

As for your issue, analogRead() returns a value from 0-1023. The constant HIGH is the same as 1 so you can't do an analogRead() and compare it.

Another problem you have is that you are reading pin 10 which is NOT an analog pin. It seems like you just want to change your analogRead() to digitalRead().

It is not clear to me what you want to do.

putting four english words in a row is not sufficient in this case

how can 16 buttons read in as serial values 0 and 1023 at the same time?

If english is not your native language use google-translate.
Write a detailed description in you native language and let do google-translate do the translation

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

I think you are using the word "serial" when you mean "analog".

You are reading from Pin 10. You have to connect the output of the multiplexer to Pin 10. Then, in readMux() change this line:
int val = analogRead(SIG_pin);
to
int val = digitalRead(SIG_pin)

Hi, @fguler_78
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please post a circuit diagram, include pin names and power supply wiring?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

#include <Joystick.h>

Joystick_ Joystick;

int XEkseni = 0;
int YEkseni = 0;
int ZRudder = 0;
int Gazkolu = 0;

int buttonState = 0;
int x = 1;

const int ledPin1 = 16;
const int ledPin2 = 14;
const int ledPin3 = 15;
const int ledPin4 = 8;
const int ledPin5 = 7;

const bool initAutoSendState = true;
//boolean ledOn=false;


//const int pinToButtonMap = 2;
//int lastButtonState[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0};

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  //pinMode(7, INPUT_PULLUP);
  //pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  //pinMode(14, INPUT_PULLUP);
  //pinMode(15, INPUT_PULLUP);
  //pinMode(16, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);

  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);

  Joystick.begin();
}

void loop()
{
  XEkseni = analogRead(A2);
  XEkseni = map(XEkseni, 0, 1023, 0, 255);
  Joystick.setXAxis(XEkseni);

  YEkseni = analogRead(A0);
  YEkseni = map(YEkseni, 0, 1023, 0, 255);
  Joystick.setYAxis(YEkseni);

  ZRudder = analogRead(A1);
  ZRudder = map(ZRudder, 0, 1023, 0, 255);
  Joystick.setZAxis(ZRudder);

  Gazkolu = analogRead(A3);
  Gazkolu = map(Gazkolu, 0, 1023, 0, 255);
  Joystick.setThrottle(Gazkolu);

  if (digitalRead(2) == HIGH)
  {
    Joystick.pressButton(0);
  } else {
    Joystick.releaseButton(0);
  }
  if (digitalRead(2) == LOW)
  {
    Joystick.pressButton(10);
  } else {
    Joystick.releaseButton(10);
  }

  if (digitalRead(3) == HIGH)
  {
    Joystick.pressButton(1);
  } else {
    Joystick.releaseButton(1);
  }
  if (digitalRead(3) == LOW)
  {
    Joystick.pressButton(11);
  } else {
    Joystick.releaseButton(11);
  }

  if (digitalRead(4) == HIGH)
  {
    Joystick.pressButton(2);
  } else {
    Joystick.releaseButton(2);
  }
  if (digitalRead(4) == LOW)
  {
    Joystick.pressButton(12);
  } else {
    Joystick.releaseButton(12);
  }

  if (digitalRead(5) == HIGH)
  {
    Joystick.pressButton(3);
  } else {
    Joystick.releaseButton(3);
  }
  if (digitalRead(5) == LOW)
  {
    Joystick.pressButton(13);
  } else {
    Joystick.releaseButton(13);
  }

  if (digitalRead(6) == HIGH)
  {
    Joystick.pressButton(4);
  } else {
    Joystick.releaseButton(4);
  }
  if (digitalRead(6) == LOW)
  {
    Joystick.pressButton(14);
  } else {
    Joystick.releaseButton(14);
  }

  int value1 = analogRead(9);

  if (value1 > 450 && value1 < 550)   //okunan değer 510
  {
    Joystick.pressButton(5);
  } else {
    Joystick.releaseButton(5);
  }

  if (value1 > 550) //okunan değer 1023
  {
    Joystick.pressButton(6);
  } else {
    Joystick.releaseButton(6);
  }
  delay(10);

  int value = analogRead(10);

  if (value > 780 && value < 830) //okunan değer 819
  { Joystick.pressButton(7);   if (x == 1) {
      digitalWrite(ledPin2, HIGH);
      x = 0;
      delay (500);
    } else if (x == 0) {
      x = 1;
      digitalWrite(ledPin2, LOW);
      delay (500);
    }
  }
  else {
    Joystick.releaseButton(7);
  }

  if (value > 700 && value < 780) //okunan değer 767
  { Joystick.pressButton(8);  if (x == 1) {
      digitalWrite(ledPin3, HIGH);
      x = 0;
      delay (500);
    } else if (x == 0) {
      x = 1;
      digitalWrite(ledPin3, LOW);
      delay (500);
    }
  }
  else {
    Joystick.releaseButton(8);
  }

  if (value > 650 && value < 700) //okunan değer 682
  { Joystick.pressButton(8);  if (x == 1) {
      digitalWrite(ledPin1, HIGH);
      x = 0;
      delay (500);
    } else if (x == 0) {
      x = 1;
      digitalWrite(ledPin1, LOW);
      delay (500);
    }
  }
  else {
    Joystick.releaseButton(8);
  }

  if (value > 500 && value < 600) //okunan değer 511
  { Joystick.pressButton(15); if (x == 1) {
      digitalWrite(ledPin4, HIGH);
      x = 0;
      delay (500);
    } else if (x == 0) {
      x = 1;
      digitalWrite(ledPin4, LOW);
      delay (500);
    }
  }
  else {
    Joystick.releaseButton(15);
  }

  if (value < 400)                //okunan değer 0
  { Joystick.pressButton(16); if (x == 1) {
      digitalWrite(ledPin5, HIGH);
      x = 0;
      delay (500);
    } else if (x == 0) {
      x = 1;
      digitalWrite(ledPin5, LOW);
      delay (500);
    }
  }
  else {
    Joystick.releaseButton(16);
  }

  delay(10);

}

Hi fguler,

well done posting the code as a code-section.
Please add

best regards Stefan

these codes I wrote works. No problem. Later I added hp4067 multiplexer to use more buttons. Now I am using an analog output for 16 buttons. how do I use it as a Joystick.pressButton for each.

Hi fguler,

it is still not clear to me what you want to do
A button-press is a digital thing just two states:

state 1: button unpressed
or
state 2: button pressed

"Using an analog output for 16 buttons"

Analog means voltage continioulsy ranging from
0,0V
0,1V
0,2V
...
4,8V
4,9V
5,0V

How is your "analog output" connected to the 16 buttons??

here you really should follow this

If english is not your native language use google-translate.
In your native language write down very detailed words what you want to do and let do google-translate the translation

best regards Stefan

What Arduino model are you using? You are using analogRead(10) and the UNO only has A0 through A5. The NANO has A6 and A7.

pro micro and 16 channell hp4067 multiplexer





Actually I want to make a joystick.
The joystick buttons will be active when the s1 -s10 buttons are pressed once, and the joystick buttons will become passive when released.
For the s1-s5 buttons, when pressed once, L1-L5 will light on, when pressed again, it will turn off. s11-s15 toggle button 3 works as on-off-on. So it will work like ten buttons in total. In potentiometers, the axis would be x, y, z and slider. That was my entire goal. I was very tired of you. Sorry. I will be very happy if you can help. Best regards...

Can you check your tactile switch wiring.
Using a DMM measure the voltage on C0 as you press the corresponding button.
Note what the voltages are.

This is how your tactile button is wired internally.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

The circuit picture shows that you want to have 16 digital switches connected to the 4067 multiplexer.

You can try to safe time by coding it all and then start testing.

You will loose plenty of time on debuging because in a big untested code there are so many places the bug could be.

If you invest time in small testprograms that test a single function this will pay off in the long run because you will find the bugs much faster.

So my suggestion is to write a testprogram that dos nothing more than select on of the 16 mux-channels and display the state of the input.

in two stages: make a small testprogram that shows what the input-pint connected to the output of the mux-chip is reading

Test this with a single switch directly connected to the Arduino-Input.

safe the working-code with some kind of serial number -001

safe it again with serialnumber -002
this ensures that you have a copy of the working code -001

Add the binary logic that switches on of S0, S1, S2, S3 to select one of the sixteen channels.

Test this by printing to the serial monitor the selected channel-number and the state of the input-pin.

This strategy will ensure that demuxing will work.

then go on with the next part
best regards Stefan

I could not make it properly. Anybody can write code for Joystick?

If you want somebody to write the complete code you could offer to pay money in the gigs and collaboration-subforum.

I guess you won't pay 30 to 50 dollars per programming-hour.

So just post your latest attempt of trying to write the program. Post your code in this way:

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

I'm sure you will achieve a working code with the support of the forum

best regards Stefan