Edit: I have mistaken in the forum. I intended to post it in 'Programming Questions'
Hi,
I rewritten the following code which is heavily based on the code from this page by Adam Meyer.
For the moment my code uses a delay so it is not a non-blocking code. I would like to transfer it to be a non blocking code so it will constantly read the values from the 4067 inputs but will print out only when a value for pot x is changed from the previously reading.
here is the full code:
//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;
//Mux in "SIG" pin
int SIG_pin = 0;
//Mux inputs
#define NUM_INPUTS 3
unsigned long currentMillis = millis();
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(115200);
}
void loop(){
MuxValue();
}
int readMux(int channel){
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[NUM_INPUTS][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
};
//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;
}
int MuxValue(){
for(int i = 0; i < NUM_INPUTS; i ++){
Serial.print("Value at channel ");
Serial.print(i);
Serial.print("is : ");
Serial.println(readMux(i));
delay(1000);
}
}
and here is the function I think I should change:
int MuxValue(){
for(int i = 0; i < NUM_INPUTS; i ++){
Serial.print("Value at channel ");
Serial.print(i);
Serial.print("is : ");
Serial.println(readMux(i));
delay(1000);
}
}
how can I make it into a non blocking code printing only when a change in value has happened?
my first thought is to use millis() and read the 4067 inputs only at a certain interval but this will not be a good idea (?) because it might have been that a value is changed within that interval.
How should I impliment the above?
Thanks