Go Down

Topic: need help to blink simple pattern with shift register (Read 1 time) previous topic - next topic

jaspal23

Hi,

I recently purchased UNO R3. I simply created a Led pattern. i just want to turn off/on the pattern.
I am using 74HC595N Shift register. So please add the blink function in the code and also tell me how to use output enable pin (13) in the code. I am learning arduino programing and other things.
i will be very thankful to you.

here the code is .......

int latchPin = 5;
int ClockPin = 6;
int dataPin = 4;

int led[8] = {0,1,1,0,0,1,1,0};

void setup()

    {
     
      pinMode(latchPin,OUTPUT);             
      pinMode(ClockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);

    digitalWrite(latchPin, LOW);
     
      for(int i=0;i<8;i++)
      {
       
        digitalWrite(ClockPin,LOW);
        digitalWrite(dataPin,led);
        digitalWrite(ClockPin,HIGH);
       
      }     
    digitalWrite(latchPin, HIGH);   
    }

void loop() {

}

Grumpy_Mike

#1
Apr 11, 2018, 11:21 am Last Edit: Apr 11, 2018, 11:22 am by Grumpy_Mike
Wire the output enable pin to ground.

As the setup function contains all the code for outputting the data to the shift register it needs moving into the loop function.
Then add at the start of the loop function the lines:-
Code: [Select]

led[0] ^= 1; // this will blink LED 0
delay(300); // so you can see it.

jaspal23

Wire the output enable pin to ground.

As the setup function contains all the code for outputting the data to the shift register it needs moving into the loop function.
Then add at the start of the loop function the lines:-
Code: [Select]

led[0] ^= 1; // this will blink LED 0
delay(300); // so you can see it.


it blinks only first led but my requirement is flashing of whole pattern {0,1,1,0,0,1,1,0}.

int latchPin = 5;
int ClockPin = 6;
int dataPin = 4;

int led[8] = {0,1,1,0,0,1,1,0};

void setup()

   {
     pinMode(latchPin,OUTPUT);            
     pinMode(ClockPin, OUTPUT);
     pinMode(dataPin, OUTPUT);
   }

void loop() {

led[0] ^= 1; // this will blink LED 0
delay(300); // so you can see it.
digitalWrite(latchPin, LOW);
   
     for(int i=0;i<8;i++)
     {
       
       digitalWrite(ClockPin,LOW);
       digitalWrite(dataPin,led);
       digitalWrite(ClockPin,HIGH);
       
     }      
   digitalWrite(latchPin, HIGH);    
}

Grumpy_Mike

#3
Apr 11, 2018, 02:46 pm Last Edit: Apr 11, 2018, 02:46 pm by Grumpy_Mike
Quote
my requirement is flashing of whole pattern
No you did not say that you just said
Quote
So please add the blink function in the code
That is what I added.
So exactly what do you want to blink and how?
The whole pattern or just the LEDs that are on?
If it is the whole pattern then use:-
Code: [Select]

for(int i = 0; i<8; i++){
led[i] ^= 1; // this will blink all the LEDs in the pattern
}
delay(300); // so you can see it.

jaspal23

No you did not say that you just saidThat is what I added.
So exactly what do you want to blink and how?
The whole pattern or just the LEDs that are on?
If it is the whole pattern then use:-
Code: [Select]

for(int i = 0; i<8; i++){
led[i] ^= 1; // this will blink all the LEDs in the pattern
}
delay(300); // so you can see it.


Thanks Grumpy_Mike for the giving code. It's doing fine. but if i want led flashing in {0,1,1,0,0,1,1,0} this pattern how i can achieve this.

Eight Leds are connected and i want only '1' HIGH will flash not '0' LOW and it will not get on. {0,1,1,0,0,1,1,0} Total 4 Leds get flashing and 4 Leds remain off . Sorry for my english.
This code doing inverse flashing. Its very useful. i leraned new thing. THANKS

Grumpy_Mike

#5
Apr 11, 2018, 07:26 pm Last Edit: Apr 11, 2018, 07:27 pm by Grumpy_Mike
You will need two arrays instead of just one, one to hold the pattern like you have now, and one to hold the current state of the LEDs.

Code: [Select]
for(int i = 0; i<8; i++){
if(led[i] == 1) ledState[i] ^= 1; // this will blink all the LEDs in the pattern
}
delay(300); // so you can see it.

And then send the ledState array out to the shift registers.

See how you have to be very careful when you specify something because woolly talk can be misinterpreted.

jaspal23

You will need two arrays instead of just one, one to hold the pattern like you have now, and one to hold the current state of the LEDs.

Code: [Select]
for(int i = 0; i<8; i++){
if(led[i] == 1) ledState[i] ^= 1; // this will blink all the LEDs in the pattern
}
delay(300); // so you can see it.

And then send the ledState array out to the shift registers.

See how you have to be very careful when you specify something because woolly talk can be misinterpreted.
Sir,

I tried to add blink code at diffrent positions but no success. Can you upload the whole code from starting to end point. i am unable to achieve this. Please correct my whole code.

Code: [Select]

int latchPin = 5;
int ClockPin = 6;
int dataPin = 4;

int led[8] = {0,1,1,0,0,1,1,0};
int ledState[8];
void setup()

    {
      pinMode(latchPin,OUTPUT);             
      pinMode(ClockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
    }

void loop() {

 digitalWrite(latchPin, LOW);
    for(int i = 0; i<8; i++)
    {
      if(led[i] == 1) ledState[i] ^= 1;
    }
      delay(300);
      for(int i=0;i<8;i++)
      {
        digitalWrite(ClockPin,LOW);
        digitalWrite(dataPin,led[i]);
        digitalWrite(ClockPin,HIGH);
     }     
      digitalWrite(latchPin, HIGH);   
}


Grumpy_Mike

Quote
Can you upload the whole code from starting to end point. i
No, I am trying to teach you, not do it for you. This is a lot harder for me than simply writing it for you but it is a lot better for you if you learn something.

Quote
I tried to add blink code at diffrent positions but no success.
But you said in reply #4 that you had the single LED blinking. Then you wanted the whole pattern and I told you how to do that, then you said that you wanted just the LEDs that were set in the pattern so I showed you how to do that. Now you want all the code written for you and you post code that doesn't even attempt to blink the LEDs.

Post your code that is your best attempt at blinking the LEDs like you want and we can discuss what is wrong. At the moment all I can say is to read my replies again and ask about what you don't understand.

jaspal23

here i uploaded the code. Please check the code and i am learning so i do not have enough skill to understand. i hope you understand. i am beginner.


Code: [Select]

int latchPin = 5;
int ClockPin = 6;
int dataPin = 4;

int led[8] = {0,1,1,0,0,1,1,0};
int ledState[8] ;
void setup()

    {
      pinMode(latchPin,OUTPUT);             
      pinMode(ClockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
    }

void loop() {

 digitalWrite(latchPin, LOW);
        for(int i=0;i<8;i++)
      {

        if(led[i] == 1) ledState[i] ^= 1;
        digitalWrite(ClockPin,LOW);
        digitalWrite(dataPin,led[i]);
    //    shiftOut(dataPin, ClockPin, MSBFIRST, ledState[i]);
        digitalWrite(ClockPin,HIGH);
     }     
      digitalWrite(latchPin, HIGH);   
delay(300);
}



This code works fine  with inverse flashing. But still please check once that position of variable,loop etc are ok.
Code: [Select]

int latchPin = 5;
int ClockPin = 6;
int dataPin = 4;

int led[8] = {0,1,1,0,0,1,1,0};
int ledState[8] ;
void setup()

    {
      pinMode(latchPin,OUTPUT);             
      pinMode(ClockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
    }

void loop() {

 digitalWrite(latchPin, LOW);
        for(int i=0;i<8;i++)
      {

        led[i] ^= 1;
        digitalWrite(ClockPin,LOW);
        digitalWrite(dataPin,led[i]);
//        shiftOut(dataPin, ClockPin, MSBFIRST, ledState[i]);
        digitalWrite(ClockPin,HIGH);
     }     
      digitalWrite(latchPin, HIGH);   
delay(300);
}


Grumpy_Mike

#9
Apr 12, 2018, 03:29 pm Last Edit: Apr 13, 2018, 03:49 pm by Grumpy_Mike
Quote
i am learning so i do not have enough skill to understand. i hope you understand. i am beginner.
Yes I understand, that is what I am trying to teach you, the relationship between what someone tells you to do and the code you write.

Quote
But still please check once that position of variable,loop etc are ok.
Well it is OK but it is not what I told you to do in any of my answers. I told you to insert the code I gave you at the start of the loop function. What you have done is to insert part of the code at the start, miss out some you your original and another part of the code at the end. While by luck it still functions it is not what I told you to do.

The code I gave you should be placed complete and as a whole after the first { in the loop function and before any code you originally had in the loop function.

The same goes for the code in reply #5. This code uses a new, extra array called ledState. it is used to hold the current state ( that is on or off ) of your LEDs. It must be declared exactly like you declare the array led, at the start of the code, I would suggest in the line below the led array declaration.
So like this:-
Code: [Select]
int led[] = {0,1,1,0,0,1,1,0};
int ledState[] = {0,1,1,0,0,1,1,0};



jaspal23

Thanks Grumpy_Mike for your cooperation. Only last time please check the code. placement of your code.

Code: [Select]

int latchPin = 5;
int ClockPin = 6;
int dataPin = 4;

int led[] = {0,1,1,0,0,1,1,0};
int ledState[] = {0,1,1,0,0,1,1,0};
void setup()

    {
      pinMode(latchPin,OUTPUT);             
      pinMode(ClockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
     }

void loop() {

 digitalWrite(latchPin, LOW);
      for(int i=0;i<8;i++)
      {
       if(led[i] == 1) ledState[i] ^= 1;
        digitalWrite(ClockPin,LOW);
        digitalWrite(dataPin,ledState[i]);
        digitalWrite(ClockPin,HIGH);
      }
    digitalWrite(latchPin, HIGH);   
  delay(300);
}


I get the error in ledState line when i am using the following declaration
Code: [Select]
int led[] = {0,1,1,0,0,1,1,0};
int ledState[] == {0,1,1,0,0,1,1,0};


Last thing i didn't understand following statements.

" Well it is OK but it is not what I told you to do in any of my answers. I told you to insert the code I gave you at the start of the loop function. What you have done is to insert part of the code at the start, miss out some you your original and another part of the code at the end. While by luck it still functions it is not what I told you to do.

The code I gave you should be placed complete and as a whole after the first { in the loop function and before any code you originally had in the loop function. "


Grumpy_Mike

#11
Apr 13, 2018, 03:48 pm Last Edit: Apr 13, 2018, 03:51 pm by Grumpy_Mike
Quote
I get the error in ledState line when i am using the following declaration
Then post the error message, and post the code that gave it you.

Never mind I spotted it, I had two = it should be just:-
Code: [Select]
int ledState[] = {0,1,1,0,0,1,1,0};

What is it about "The code I gave you should be placed complete and as a whole after the first { in the loop function and before any code you originally had in the loop function. "  that you can't understand?

Code: [Select]

void loop() {
// PLACE THE CODE I POSTED HERE
 digitalWrite(latchPin, LOW);
      for(int i=0;i<8;i++)


This is serious, what were you not able to understand about that, maybe if I knew this I could explain things so that you could understand.

jaspal23

sorry Grumpy_Mike. i was got confused with for loop. My mistake.
Now check the whole code. my last question is if your code place in inside the for loop(means above the digitalWrite(ClockPin,LOW);  ) line  then it works. If i place your code at the below " void loop() " then it also work fine.

Is it ok to create two loops. One for Xor operation and second for shifting data to register.

Code: [Select]

int latchPin = 5;
int ClockPin = 6;
int dataPin = 4;
int led[] = {0,1,1,0,0,1,1,0};
int ledState[] = {0,1,1,0,0,1,1,0};
void setup()
    {
      pinMode(latchPin,OUTPUT);             
      pinMode(ClockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
     }
void loop() {
          for(int i = 0; i<8; i++){
          if(led[i] == 1) ledState[i] ^= 1; // this will blink all the LEDs in the pattern
             }
delay(300);
 digitalWrite(latchPin, LOW);
      for(int i=0;i<8;i++)
      {
        digitalWrite(ClockPin,LOW);
        digitalWrite(dataPin,ledState[i]);
        digitalWrite(ClockPin,HIGH);
      }
    digitalWrite(latchPin, HIGH);   
}

Grumpy_Mike

Quote
Is it ok to create two loops.
You can have as many for loops as you want. You can only have one loop function, that is a function called "loop".

Quote
If i place your code at the below " void loop() " then it also work fine.
Which is what I intended. You had the changing of the LEDs and the shifting out to the shift register mixed up together. While this will work fine in this simple case, ultimately it is not what you want to do, because you want to separate functionality so that eventually you can reuse these lines of code by encapsulating them in a function.

Then you only need to call that function every time you want an output and not have to repeat code with a new LED manipulation all mixed in with it.   

jaspal23

ok i understand little bit. Is there any site or book which you want to refer for newbie like me with good explanation of things so i can start with new ideas.

Thanks for helping me  :) 

Go Up