Go Down

Topic: Newbie question.. (Read 2335 times) previous topic - next topic

owar

Hi, I am quite a newbie in Arduino programming and I have (for you sure simple) question.

I want to turn on or off  LEDs attached to HCF4094BE shift register. I have tried this http://senster.com/blog/wp-content/uploads/2009/02/expanding_outputs.pdf and it works.But I want to light a LED for 1s when the pushbutton is pressed and when it is pressed again it will light next led.I do not understand much the code so I need this help.

                                                                                                    Thanks for answers and please apologize my English if it is not good eneugh :)

johnwasser

Perhaps the code would be easier to follow if it was split up more.  I hope this helps:

Code: [Select]


void loop() {
  unsigned int outputpattern = 1; //stores the output pattern as an unsigned int, that is, two bytes

  for (int i=0 ; i< 16; i++) {  // Repeat 16 timees
    setOutputs(outputpattern);  // Display the pattern
    delay(100);  // Pause 0.1 seconds
    outputpattern = outputpattern << 1;  // Change the pattern
    }
}

void setOutputs(unsigned int pattern)
  {
  byte pattern_MSB = pattern >> 8; //extract the MSB by shifting 8 bits to the right
  byte pattern_LSB = pattern;

  // Shift the data out 8 bits at a time.
  shiftOut(dataPin, clockPin, MSBFIRST, pattern_MSB);
  shiftOut(dataPin, clockPin, MSBFIRST, pattern_LSB);

  digitalWrite(latchPin, 1); // flick the latch to put the data on the output pins
  delay(1);
  digitalWrite(latchPin, 0);
  }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

owar

So I made now this
Code: [Select]
// program to test using two 4094 shift registers
//
// if everything is correct, it will light up one LED at a time
//
int latchPin = 10;
int clockPin = 11;
int dataPin = 12;

int butt = 7;
byte but = LOW;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  digitalWrite(latchPin, 0);        //make sure data isn't latched
}
void loop() {
  unsigned int outputpattern = 1; //stores the output pattern as an unsigned int, that is, two bytes


but =  digitalRead(butt);
if (but == HIGH){
  outputpattern = outputpattern << 1;  // Change the pattern
   delay(500);
  } 
   
for (int i=0 ; i < 10; i++){

    setOutputs(outputpattern);  // Display the pattern
    delay(100);  // Pause 0.1 seconds
   
    }
   
}

void setOutputs(unsigned int pattern)
  {
  byte pattern_MSB = pattern >> 8; //extract the MSB by shifting 8 bits to the right
  byte pattern_LSB = pattern;

  // Shift the data out 8 bits at a time.
  shiftOut(dataPin, clockPin, MSBFIRST, pattern_MSB);
  shiftOut(dataPin, clockPin, MSBFIRST, pattern_LSB);

  digitalWrite(latchPin, 1); // flick the latch to put the data on the output pins
  delay(1);
  digitalWrite(latchPin, 0);
  }


but something is wrong.When i press the button, it will light the next led but when I press it again nothing happen and in a while the first led is on again...Do you now why?

Grumpy_Mike

Quote
Do you now why?

Yes, because that is the way you have written it.

I haven't looked at all the code but this bit:-
Code: [Select]
for (int i=0 ; i < 10; i++){

    setOutputs(outputpattern);  // Display the pattern
    delay(100);  // Pause 0.1 seconds
}

Just does the same thing 10 times because the variable outputpattern doesn't change during the loop.

owar

But even if I remove the for loop it does the same thing...

johnwasser

It does everything inside loop() repeatedly.

The first thing it does inside loop() is set the pattern to 0000000000000001.  That is why it is going back to the first light.

You have a delay of half a second after you change the pattern and before you display the pattern.

Then there is another 0.1 second delay before you repeat everything.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

owar

OK than how do I make the next led ON when the button is pressed?

johnwasser


OK than how do I make the next led ON when the button is pressed?


Move the initialization of 'outputpattern' to some place outside of any function.  You can put that line just above setup().
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

owar

Thank all off you.I have already done this before but my pullup resistor on the button was not properly connected.Thank you one more time :)

rossbr

Hello, we are working on an LED sign board, which is very similar to what you have running, using the HCF4094be.  The default code does make the sign light a 'column' of lights, but we would like to be able to do pixel by pixel so we can actually put letters or something besides a scrolling column.   :~  We have basically a 24 column by 7 row panel (there are 4 panels linked together), and we can either light up a big block of LEDs that scroll, or a single line that scrolls, but we cannot figure out how to get individual pixels to scroll.  I can provide pictures and more details if needed.  We are extremely new to arduino, LED, and HCF4094 stuff, so thank you in advance for all that you have helped us with so far (even though this is our first post :)).


Grumpy_Mike

Quote
but we cannot figure out how to get individual pixels to scroll.

Give it one clock pulse and then toggle the strobe line.

rossbr

Thank you for your amazingly fast reply!!  I'm not sure how to do that, however.  We have a Data/Clock/Strobe pin, but in the 'default' code from the PDF above, it uses a 'latch' pin.  So, we just changed the one line in code to say that the latch pin is the same as the strobe pin on the arduino.  I HATE being such a NOOB with this stuff.  I have gone through all the tutorials with my arduino with steppers and stuff, but this thing is way different.  Thanks again for your input!!!  I really appreciate it!!

Grumpy_Mike

I don't know how you have wired it up so I can't give precise pin numbers. You did not provide any link to a schematic nor did you post any code.

Give it one clock pulse  - put the clock pin high, then put it low using the digitalWrite() function
toggle the strobe line. - put the strobe pin high, then put it low using the digitalWrite() function

rossbr

Again, thank you for your time and input! 

Here are some pictures that we have posted on google:
http://picasaweb.google.com/rossb2645/LEDLightBoard?feat=email#

I tried playing with the line : pattern_LSB = outputpattern & B11111111;
thinking that each '1' was a row, but that had no effect on the output to the sign.

Thanks again for your time and input!!

We are running this code:
Code: [Select]

// program to test using two 4094 shift registers
//
// if everything is correct, it will light up one LED at a time
//
int latchPin = 11;
int clockPin = 10;
int dataPin = 9;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(latchPin, 0); //make sure data isn't latched
}
void loop() {
unsigned int outputpattern =1; //stores the output pattern as an unsigned int, that is, two bytes
int pattern_LSB; //the least significant byte (LSB) of the pattern
int pattern_MSB; //the most significant byte (MSB) of the pattern
for (int i=0 ; i< 16; i++){
pattern_MSB = outputpattern >>8; //extract the MSB of the pattern by shifting all the bits over by 8
pattern_LSB = outputpattern & B11111111; //extract the LSB of the pattern by bitwise AND
shiftOut(dataPin, clockPin, MSBFIRST, (byte) pattern_MSB);
shiftOut(dataPin, clockPin, MSBFIRST, (byte) pattern_LSB);
digitalWrite(latchPin, 1); // flick the latch to put the data on the output pins
delay(1);
digitalWrite(latchPin, 0);
delay(100);
outputpattern = outputpattern <<1; // shift the outputpattern left by one bit
}
}



Grumpy_Mike

Code: [Select]
digitalWrite(latchPin, 0); //make sure data isn't latched
should be
Code: [Select]
digitalWrite(latchPin, LOW); //make sure data isn't latched
To be strictly correct.

Quote
I tried playing with the line : pattern_LSB = outputpattern & B11111111;

Waste of time playing with the B11111111 all that does is to isolate the least significant byte fro the rest of the bit pattern outputpattern.
You should write this:-
Code: [Select]
pattern_LSB = outputpattern & 0xff
To make it easier for grown ups to read. Using a binary bit pattern is a bit pants.

However this code appears to do what you want to do. It outputs the patterns of on and off stored in outputpattern. Then at the end this line:-
Code: [Select]
outputpattern = outputpattern <<1; // shift the outputpattern left by one bit
Changes what you put out by one pixel and it outputs it again, thus causing it to scroll that bit pattern.

The line
Code: [Select]
unsigned int outputpattern =1;
Is where you set up the pattern you want to display, so for example:-
Code: [Select]
unsigned int outputpattern =0xaaaa;
will turn on every other LED, or
Code: [Select]
unsigned int outputpattern =0xff00;
will turn on the top 8 LEDs and leave off the bottom 8 ( or the other way round because a photo is NOT a schematic )

Go Up