I am doing a project where I m using 8:1 MUX & trying to code Arduino in such a way that I can give input of LED no. & that Led will keep glowing until I give another input. Can any one help me regarding this?
Please provide more details.
What is the MUX? Do you have a schematic?
Do you try to write a code yourself? Please show your efforts.
Welcome to the forum
There is useful information here in how to post so that you get the best help.
CD4051 is the MUX i m using
//Constants
#define number_of_mux 1
#define numOfMuxPins number_of_mux * 8
#define enPin 2
#define channelA 4
#define channelB 7
#define channelC 8
//Parameters
const int comPin = 3;
void setup(){
//Init Serial USB
Serial.begin(9600);
Serial.println(F("Initialize System"));
//Init CD4051B
pinMode(channelA, OUTPUT);
pinMode(channelB, OUTPUT);
pinMode(channelC, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(channelA, LOW);
digitalWrite(channelB, LOW);
digitalWrite(channelC, LOW);
digitalWrite(enPin, LOW);
}
void loop(){
MuxLED();
}
void selectChannel(int chnl){/* function selectChannel */
//// Select channel of the multiplexer
int A = bitRead(chnl,0); //Take first bit from binary value of i channel.
int B = bitRead(chnl,1); //Take second bit from binary value of i channel.
int C = bitRead(chnl,2); //Take third bit from value of i channel.
digitalWrite(channelA, A);
digitalWrite(channelB, B);
digitalWrite(channelC, C);
Serial.print(F("channel "));Serial.print(chnl);Serial.print(F(" : "));
Serial.print(C);
Serial.print(F(","));
Serial.print(B);
Serial.print(F(","));
Serial.print(A);
Serial.println();
}
void MuxLED(){/* function MuxLED */
Serial.println("Select the Channel number from 0 to 7");
char channelno = Serial.read();
if(0<= channelno && channelno <= 7){
Serial.println("Channel: "+channelno+" is selected");
selectChannel(channelno);
Serial.print(F("LED "));Serial.print(i);Serial.println(F(" HIGH"));
digitalWrite(comPin, HIGH);
}else{
Serial.println("Channel invaild. Try Again");
}
}
Please read the link in post #3 and edit your post, inserting the code in the code tags.
Yes I tried to code. I don't have a schematic of my own. I read an article and taking its reference. I am attaching its circuit image
First of all - your leds are connected directly without of current limiting resistor - it may fry your mux's outputs.
if you doubt why you need a resistor - read this thread
Okay
What's problem with the code? Is it works as expected?
If no - please explain the difference between what you expected and what you get.
The 4051 isn't really designed to switch current: it's a signal multiplexer. If you are trying to minimize the number of Arduino pins you need, there are better ways, such as a shift register.
This is a problem. You can't concatenate string literals and characters that way. The easiest fix is just to print them separately:
Serial.print("Channel: ");
Serial.print(channelno);
Serial.println(" is selected");
This is a problem. You are reading a character from Serial and treating it as a single-digit number. Each character is a represented by a number but the numbers for the digits '0' through '9' is NOT the same as the numbers 0 though 9. You have to map them.
You could treat every character as a digit character and convert to a single digit number:
int channelno = Serial.read() - '0'; // Convert digit characters
if (0 <= channelno && channelno <= 7) {
You could use the function to read an integer from Serial. This will read multi-digit numbers.
int channelno = Serial.parseInt();
if (0 <= channelno && channelno <= 7) {
You could leave the value as a character for a while. You would either have to do the conversion later or the selectChannel() function would have to deal with getting a character instead of a number.
char channelno = Serial.read();
if ('0' <= channelno && channelno <= '7') {
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.