Variable that changes due to a for loop. Use a particular one in an if statement

Hi,

--Given a 16-channel multiplexer, how can I retrieve the value at one channel to use in the if control structure?

For more context on this question please consider my other topic linked here:

For example using something like this

// int analogValue = readMux(i)

Where 'i' is changing due to a for loop. I just want to use the analog value at channel '0' for instance, with the if control structure.

Additionally I would like to have multiple of these so different channels control different if statements.

Here is the sketch as it stands, where I have tried to concert different elements and sketches from other authors. There a few parts to it but the comments I made are fairly thorough.


//An integer to read a certain channel
int readMux(int);

//Mux control pins
const byte s0 = 8;
const byte s1 = 9;
const byte s2 = 10;
const byte s3 = 11;

//Mux in "SIG" pin
int SIG_pin = 14;

//Max7219
const byte CS_PIN  = 3
const byte CLK_PIN = 4
const byte DIN_PIN = 5




//Holds a data type either true or false. bool var = val. Our variable is threshold.
boolean threshold = false;


//This is the number of devices adress starts counting at 0 even though there is only 1 device
#define MAX_SEG 1

//LED control Library
#include <LedControl.h>



LedControl matrix = LedControl (DIN_PIN, CLK_PIN, CS_PIN, MAX_SEG); // MAX7219

void setup ()
{
  //These lines are all setup for the Matrix

  
  //Switches from power saving to normal operation
  matrix.shutdown(0, false);
  
  //Controls the intensity  of the brightness takes value from 0-15 with setIntensity(int addr, int intensity)
  //Params:
  //  addr the address of the display to control
 //  intensity the brightness of the display.
//  void setIntensity(int addr, int intensity); 
  matrix.setIntensity(0, 7);
  
  //This shuts all the LEDs of
  //Params is adress
  matrix.clearDisplay(0);
  
  
  //These lines are all setup for the 16 Channel Multiplexer
  
 // This makes the digital pin an output
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  
  //It has an output of either GND or 5V. This sets the default as a pulldown.
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  
//Here is a test LED for good measure. Also the built in LED is pin 13.
//Pinmode for LED 13 
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  
  //This sets serial data transmission rate. Sets the data rate in bits per second.
  Serial.begin(9600);
}






//I dont know the mechanics of this, I guess its where we read the anaolog value at each (i). 
//I am not worried about these lines just know they work. 86-122 if anyone can explain in a good way. 
int readMux(int channel) {
  //array 
  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
  };

  //cycle through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }//close for loop

  //read the value at the SIG pin. //Read the value from the 16-channel multiplexor pin. This can only read one channel at a time.
  //and assign it to a variable.
  int analogValue = analogRead(SIG_pin);


  //Terminate a function and return a value from a function to the calling function, if desired.
  return analogValue;
}//close Adams super handy code part.


void loop() {
    //Loop through and read all 16 values
  //Reports back Value at channel i is: x
for (int i = 0; i < 16; i ++) {
  
    Serial.print("Value at channel ");
    Serial.print(i);
    Serial.print("is : ");
    Serial.println(readMux(i));
    Serial.print(" peeled state =  ");
    Serial.println(threshold);
	}
    


//Boolean holds a data type either true or false. 
//use this way : bool varible = true or false. My variable is threshold.
//boolean threshold = false; (already made this a global)

//My for loop at channel 0
int analogValue = readMux(0)//how do I get this inside the if control strucure

      
   if (analogValue > 500) {
      threshold = true;

      void setLed(int 0, int row, int col, true)

    }//close if
    else { 
      threshold = false;
      void setLed(int 0, int row, int col, false)

    }//close else
   
//another if/else for a different channel
 
  }//close loop




//Adams thing to retrieve value.
//switch mux to channel 15 and read the value.
// int analogValue = readMux(15);

PS i was on the web editor so I have not had a chance to auto-format.

you code is not C++ here, missing semi colons and proper call for functions

  int analogValue = readMux(0) //how do I get this inside the if control strucure
   if (analogValue > 500) {
      threshold = true;

      void setLed(int 0, int row, int col, true)

    }//close if
    else { 
      threshold = false;
      void setLed(int 0, int row, int col, false)

    }//close else

if should read

  int analogValue = readMux(0);
  if (analogValue > 500) {
    threshold = true;
    setLed(0,row,col, true);
  }  else {
    threshold = false;
    setLed(0, row, col, false);
  }//close else

then it's fine, you are actually using readMux(0) for the if condition

you could also get rid of analogValue if you don't use it for anything else:

  if (readMux(0) > 500) {
    threshold = true;
    setLed(0,row,col, true);
  }  else {
    threshold = false;
    setLed(0, row, col, false);
  }//close else
1 Like