Pages: [1] 2   Go Down
Author Topic: Newbie question..  (Read 1976 times)
0 Members and 1 Guest are viewing this topic.
Czech republic
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8719
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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);
  }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Czech republic
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I made now this
Code:
// 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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33432
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Czech republic
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8719
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Czech republic
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8719
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Czech republic
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   smiley-confuse  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 smiley).

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33432
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but we cannot figure out how to get individual pixels to scroll.
Give it one clock pulse and then toggle the strobe line.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33432
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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
}
}

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33432
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
digitalWrite(latchPin, 0); //make sure data isn't latched
should be
Code:
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:
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:
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:
unsigned int outputpattern =1;
Is where you set up the pattern you want to display, so for example:-
Code:
unsigned int outputpattern =0xaaaa;
will turn on every other LED, or
Code:
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 )
 
Logged

Pages: [1] 2   Go Up
Jump to: