Go Down

Topic: buttons-74ls154-bcd problem (Read 300 times) previous topic - next topic

newbiedad

Nov 18, 2018, 03:49 am Last Edit: Nov 18, 2018, 08:11 am by newbiedad
hi,
i have a project i'm building and i have a program problem i can't solve.

i have a arduino uno with pins d2-d5 connected to a 74ls154 (working) blinking led's.
i am using pins d6-d13 connected to push button switchs.

the problem is that 74ls154 is BCD input and i want to push a button on arduino (d6 to d8) and display the push to one of 8 led's on the 74ls154. 

any ideas? thanks in advance!

newbiedad


/// Setup variables /// for set pins on arduino to 74ls154
const int A = 2;
const int B = 3;
const int C = 4;
const int D = 5;

int tim = 50; //the value of delay time

void setup() {
 
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);

}
 
void loop()
{

///--------Begin Code - LEDS -Forward ------------/

//0000 - all off -
  digitalWrite(A, HIGH);
  digitalWrite(B, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, HIGH);
delay(tim); //wait
 
  //0000 - 1
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
delay(tim); //wait
 
  //0001 - 2
  digitalWrite(A, HIGH);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
delay(tim); //wait
 
 //0010 - 3
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
delay(tim); //wait

 
 //0011 - 4
  digitalWrite(A, HIGH);
  digitalWrite(B, HIGH);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
delay(tim); //wait

//0100 - 5
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
delay(tim); //wait

//0101 - 6
  digitalWrite(A, HIGH);
  digitalWrite(B, LOW);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
delay(tim); //wait

//0110 - 7
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
delay(tim); //wait

//0111 - 8
  digitalWrite(A, HIGH);
  digitalWrite(B, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
}



PaulRB

#2
Nov 18, 2018, 08:33 am Last Edit: Nov 18, 2018, 08:39 am by PaulRB
Hi. Please read the forum guide in the sticky post, then modify your post above and correct it.

74ls154 is not BCD. It is a 4 to 16 line decoder.

Quote
i want to push a button on arduino (d6 to d8) and display the push to one of 8 led's on the 74ls154. 
Don't understand. You will have 3 pushbuttons and 8 LEDs. What exactly do you want to happen when you push these buttons. Give an example sequence of events.

newbiedad

The 74ls154 is a 4-LINE BCD TO 10-LINE DECIMAL DECODER, please read the specs.

i have 8 switchs connected to the arduino directly with pull-down resistors from pins d6 to d13.

i have the 74ls154 inputs connected to the arduino pins d2-d5. 8 led's are connected to the 74ls154 outputs 1-8... the other 8-15 outputs are connected to relays and motors.

when i push a button, i want an led to light on the outputs 1-8 of the 74ls154.

i can't figure out how to tie together the push buttons code to output to the 74ls154 to light the leds.   


GolamMostafa

#4
Nov 18, 2018, 01:41 pm Last Edit: Nov 18, 2018, 04:32 pm by GolamMostafa
@OP

1.  OK! The input pattern to your 74LS154 dcoder is 0000, 0001, ..., 1001 which you wish to consider as BCD input; this is fine.

2.  When 0000 is asserted at the input of 74LS154, the S0/-output will assume LL-state, and all other 15 outputs will assume LH-states.  When 0001 is asserted at the input of 74LS154, the S1/-output will assume LL-state, and all other 15 outputs will assume LH-states.  When 1001 is asserted at the input of 74LS154, the S9/-output will assume LL-state, and all other 15 outputs will assume LH-states. From this view point, you want to tell the 74LS154 as a BCD to 10-line decoder. This is also fine to me. However, the 74LS154 is usually termed as a 4-to-16 decoder.

3.  Let us designate your buttons connected with d6 - d13 as SW6 - SW13.

4.  Assume that SW6 is closed; now, you want the LED (say LED6) connected with S0/-pin of 74LS154 is ON. To do it, you can execute the following codes:
Code: [Select]
if(SW6 is found closed)
{
   ignite LED6
}


The Arduino Codes are:
Code: [Select]

while(digitalRead(6) !=HIGH)
{
   ;
}

PORTD = 0b00000000;   //bi-2 to bit-5 (0000) go to d2 - d5 of PORTD


If you want to include all the buttons, you can use case-switch structure or some other style you like. Modify your original codes in the light of codes of Step-4 to include all 8 buttons and post them. The Forum members will help you to correct your codes.

PaulRB

#5
Nov 18, 2018, 02:21 pm Last Edit: Nov 18, 2018, 03:07 pm by PaulRB
The 74ls154 is a 4-LINE BCD TO 10-LINE DECIMAL DECODER, please read the specs.
No, 74ls42 is what you're thinking of. 74ls154 is 4 to 16 decoder.

Quote
other 8-15 outputs are connected to relays and motors.
If it's a 10 line decoder, how come it has outputs 10 to 15?

Quote
i can't figure out how to tie together the push buttons code to output to the 74ls154 to light the leds.  
The way I would approach this would be to scan through the buttons in reverse order, using a while-loop until you find one that is pressed. Since the buttons are connected to consecutive Arduino pins, you can calculate the pin number by adding 5 to the loop variable. This will give you a number between 1 and 8, or 0 if no button is pressed. Then you can use bitRead() to give you the values you need to write to the A, B, C, D pins of the decoder.
Code: [Select]

byte i = 8;
while (i > 0 && digitalRead(i + 5) == LOW) i--;
for (byte j = 0; j < 4; j++) digitalWrite(2 + j, bitRead(i, j));

I'm assuming here that output 0 of the decoder is not connected to anything, so when you set A, B, C & D to LOW, no led or motor will be activated.

newbiedad

If it's a 10 line decoder, how come it has outputs 10 to 15?

thats called staying up all night without coffee. :-)

it's a 74ls154 demux 24pin 4-16 line...

this is a tinker test machine i'm building, 8 buttons select options, 8 led's light up led's to the button selected. i choose the 74ls154 because i needed the extra outputs. arduino handles input buttons outputs to 74ls154... that's it. nothing else... so when it's not doing anything it should just be scanning the buttons and waiting.

and now for coffee... :-)

thanks again for any help you guys can offer, much appreciated.

GolamMostafa

#7
Nov 18, 2018, 05:41 pm Last Edit: Nov 19, 2018, 05:47 am by GolamMostafa
@OP

1.  Hope that the following diagram depicts your setup.


2.  Upload the following sketch and report the results. (Tested using Serial Monitor.)
Code: [Select]
byte x;
byte y;
byte z;

void setup()
{
  Serial.begin(9600);

  for (int i = 2; i < 6; i++)
  {
    pinMode (i, OUTPUT); //2 - 5 outputs
  }

  for (int i = 6; i < 14; i++)
  {
    pinMode (i, INPUT); //6 - 13 inputs
  }
}

void loop()
{
  x = PIND; //reading d7 d6 d5 - d0 (pd7 pd6)
  y = PINB; //reading d13 - d8 = pb5 - pb0 ; digital pins
  y = y << 2;  //y7 - y2 = d13 - d8
  bitWrite(y, 1, bitRead(x, 7));
  bitWrite(y, 0, bitRead(x, 6)); //y=y7 - y0 = K7 = K0

  for (int i = 0; i < 8; i++)
  {
    if (bitRead(y, i) == HIGH)//checking if K0 or K1, ..., K7 is closed
    {
      z = i;  //i
      z = z << 2;
      PORTD = z;   //if K0 is closed, send 0000 = i to decoder; K1 is closed, send 0001=i to decoder, ...,
      Serial.println(z, BIN);//K0-K7 --> 0,1,10,11,100,101,110,111
    }
  }
}

newbiedad

GolamMostafa

THANK YOU!

AWESOME!!! It works perfectly!!!  :)  :)  :)

Yes, that's the circuit exactly... i need a program like that...


PAULRB
 
AWESOME CODING!
i tested your code, it worked... i was able to scan the buttons and get the serial monitor to display it.
THANK YOU!!!

I am more a hardware guy, i'm just starting to learn how to code but it's not that bad.

PaulRB

#9
Nov 18, 2018, 06:33 pm Last Edit: Nov 18, 2018, 07:28 pm by PaulRB
I'm not sure what a "tinker test machine" is.

But I am wondering why you chose 74ls154, unless it's because you have a box of them going to waste.

Firstly, the "ls" chip range is pretty old and defunct and had been replaced for a long time by the "hc" range. It's possible that no one has bothered to make the '154 in the hc range because it's not often used these days.

Secondly, decoders like 74ls42 and 74ls154 have a serious limitation when used to increase the number of available Arduino outputs. Only one of the 10 or 16 outputs can be active at any time, and one of them must always be active.

If I wanted to increase the number of available Arduino outputs, I would use something like 74hc595 or pcf8475 or pcf8575. With 74hc595, you can have as many outputs as you need, and have those outputs HIGH or LOW in any combination, and only 3 Arduino outputs are needed to control potentially hundreds of outputs. With the pcf chips, you get either 8 or 16 pins which can be inputs or outputs, each with individual control, and only 2 Arduino pins needed to control them and provide a large number of extra inputs or outputs.

newbiedad


I have lots and lots of old chips from way way back, i'm old school like 80's. i build pretty much everything with old ttl-ls-cmos logic if i can, personally i have made electronics fun again for myself. by integrating the old chips which i already know all about by heart and have worked with in the past i have found them to be very robust when integrating to today's processors and such. i believe i have some 74hc595 or pcf8475 or pcf8575 maybe? , but i really wanted to do some old time copper clad board work. besides i really don't like doing to much work with SMD stuff. this old stuff is easy to work with and if you already have lots of it, use it... although i must admit i really like IC2 protocol and devices, these are the cat's meow... the sensors and boards you can get are unreal and all working with IC2 and arduino... awesome!

as for the 74ls154, i choose it because i only wanted one event to occur at any one time. and it's one of my favourite chips  :-)

PaulRB

I'm with you on the SMD stuff. I don't have the eyesight or steady hands for that. So I generally stick to DIP chips, thru-hole components and so on. But things may have to change over the next few years as the old components become scarce. New devices are already becoming available and ever more affordable which can print conductive tracks onto bare boards, and print solder paste. All we will need to do is place the components in roughly the right place, and the device will then heat the board, pulling the components into the exact spot and "re-flow" all the smd components prefectly. Bring it on!

newbiedad

#12
Nov 18, 2018, 10:31 pm Last Edit: Nov 20, 2018, 02:22 am by newbiedad
ohh i like that idea... or...

print conductive tracks onto a non-conductive plastic sheet, place parts into position and heat with a hair dryer. done. to remove a bad part, reheat, replace, reheat.


i like it, as long as i can also have an expander port where i can bring in real wire devices and chips off board. (backwards compatibility)

to tackle the SMD problem, i have using smd sop adaptor boards, you can get them for any type of smd device cheap and fairly easy to solder to.

as a thank you for all your help guys... here is what i'm working on... it's a miniature drink serving machine that i came up with, just testing ideas...


*** Also if i could ask a favour, in the code by GolamMostafa, that works great by the way, could you please give me some simple examples of how you would control single outputs on the 74ls154 more efficiently or better then the way i have been doing it, i have some control relays on specific pins to control... it would be greatly appreciated!!!

I'm not a code guy, just a simple single output example would be great, i'm not sure where to put it or which value i'm supposed to use is it x,y, or z ??? Thanks!

Thanks!



GolamMostafa

#13
Nov 20, 2018, 02:23 pm Last Edit: Nov 20, 2018, 02:24 pm by GolamMostafa
*** Also if i could ask a favour, in the code by GolamMostafa, that works great by the way, could you please give me some simple examples of how you would control single outputs on the 74ls154 more efficiently or better then the way i have been doing it, i have some control relays on specific pins to control... it would be greatly appreciated!!!
Please, clarify what have you wanted to mean by saying -- "of how you would control single outputs on the 74ls154 more efficiently or better then the way i have been doing it, i have some control relays on specific pins to control..."?

You wanted to see that LED0 should be ON when you press K0, and so on... We have shown you of doing that. What else you want that we can do for you in terms of coding for a specific task like a relay control at S13/-pin of the decoder without disturbing the current LEDX status?

newbiedad

what i have now is a switch that when pushed triggers an led, that is connected to the 74154 output pins 1 through 7. this works perfect. thank you!

however, when a button is pushed led comes on, jump to subroutine: "should also" activate a relay on the other independent outputs of the 74154 pins 9-16. this controls the drip relays.

sequence
----------
push button, led comes on, relay activates and controls drip (by time delay). done. re-scan buttons again.

so, a simple example of (detect button), led on, output to pin 9 to control relay1, delay, return scan for push buttons again.
so, a simple example of (detect button), led on, output to pin 10 to control relay2, delay, return scan for push buttons again.
so, a simple example of (detect button), led on, output to pin 11 to control relay3, delay, return scan for push buttons again.

or simply output to pin 9 control relay1, delay.

or simply output to pin 10 control relay2, delay.

or simply output to pin 11 control relay3, delay.

return to rescan;

ect...

Thank you again for your help, it is much appreciated!




Go Up