Reading which Pin of cd4067 is "highest"

Hi,
I set up a arduino nano with a cd4067 multiplexer. (Analog input A0 is POSCV)
Now I want to output a number from 0-15 for the Pin that reads the highest number. (whenever one of them is higher than 250).
(eg. if address pin 13 on the 4067 has the highest value of 800 and all other pins are lower, set the "led" to 13)
For now, the code looks like this, but the int "k" only gives me 1.

#define POSCV A0
int temp = 0;
int led = 17;
int k = 0;
int m = 0;
int addressPins[] = {6, 7, 8, 9};

void setup ()
{

  pinMode (POSCV, INPUT);

  Serial.begin(9600);
  Wire.begin();


  for (int m = 0; m <= 3; m++) {
    pinMode(addressPins[m], OUTPUT);
  }
}


void loop ()
{  

  for (int k = 0; k <= 15; k++) {
    for (int m = 0; m <= 3; m++) {
      digitalWrite(addressPins[m], bitRead(k, m));
    }
    
  }

temp = analogRead(POSCV);
     
   if (temp < 250)
   
   {
//nothing
   }
    
   else if (temp >= 250)
{
bitRead(k,m);

   if (k == 15)
   {
led = 15;
   }

   else if(k == 14)
   
   {
led = 14;
   }

   else if(k == 13)
   
   {
led = 13;

//.... [continues until k == 0]

I guess there is another way to do this which i can't seem to find.
Can someone help me?

If you read the data into an array use the below to find the maximum value in the array and the location of the maximum value within the array.

int data[] = {0x02, 0x6A, 0xBB, 0xCF, 0x04, 0xAE, 0x6B, 0x9D, 0xC3, 0x9D, 0xF2, 0x53, 0x65, 0x45, 0xE5, 0x9B};
int dataMax = 0;
int maxIndex = 0;

void setup()
{
   Serial.begin(115200); \

   for(byte n = 0; n < sizeof(data) / sizeof(data[0]); n++)
   {
      if (data[n] > dataMax)
      {
         dataMax = data[n];
         maxIndex = n;
      }
   }
   
   Serial.print("array maximum = ");
   Serial.print(data[maxIndex], HEX);
   Serial.print("   at index  ");
   Serial.println(maxIndex);
}

void loop()
{

}

Which controller is used?
Using pin 0 and 1 is bad for some controllers. Use those pin for serial and debugging.
Read one channel and compare the reading with the previous maximum. If the new reading is bigger save the value and the channel number. read the other channels, one by one, compare and store new high readings.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.