using two functions on Shift register 595

Hi,

I am a new comer to arduino with background in graphic design, like the criminals in detective movies, I have snapped together codes from good tutorials and info displayed here and on other sites (thanks to internet and good people)

I am facing a problem and after googling for some days I am not sure, what I want to do is possible or not, with 595 shift register. ( and now getting allergic to overcopied knightride sequence tutorials :P )

Here is the scenario:

out of 595's 8 pins: 1. I want to use pin 5 for Night Light (turns big led on in dark) 2. I want to be able to use first 5(Q0-Q4) for dancing leds (led blink patterns)

My code is working fine, with one problem, when the led dance starts it turns off the Night light,

Any way i can send data to first five pins without effecting last 3

I am turning LED like this

int dancedelay = 100; // dancing led delay

int patterns[14] = { B00000001, dancedelay, B00000010, dancedelay, B00000100, dancedelay, B00001000, dancedelay, B00010000, dancedelay, B00011111, dancedelay };

digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, patterns[index * 2]); digitalWrite(latchPin, HIGH); delay(patterns[(index * 2) + 1]); index++; if (index >= count) { index = 0; } delay(50); break;

Thank you

extra / less relevant info (with the help of DIYAudio i was able to make my own 5.1 amplifier (from scratch), hope to get help to make another good project.

I am making this arduino based project to assist regulating my pc temperature , Night Light, room temperature, vu meter and dancing led patterns (last one for my kid :) )

Any way i can send data to first five pins without effecting last 3

You would have to read the state of the first three pins, or keep track of what you wrote to them, and set the values in patterns[n], using bitWrite() or bitSet(), before shifting out the value.

To keep the last three pins on:

   bitSet(patterns[index * 2], 0);
   bitSet(patterns[index * 2], 1);
   bitSet(patterns[index * 2], 2);
// Then shift the patterns[index * 2] value out.

Thanks a lot!

It helped me a great deal.

Another small problem thats bugging me, The on/off button only seems to work once the led dance pattern ends.

I want to stop animation on shift register immediately as button is pressed, rather keep it pressed so it reads when led dance cycle ends. Any help or guidance towards path would be greatly appreciated. (I dont want to drop this feature as my 2yr old loves leds)

to further explain my problem,

Case 1: led 1 on led 2 on led 3 on .... 15 more patterns and so on break

Case 2 xyz,

I want to stop led dance before break at one, not keep button pressed until break is reached

Check for button press more often then.

Hi, thank you for response, I am checking it in short interval, but button press is only counted, once the led dance cycle ends (reaches break;).

So now its like press the button down wait for cycle to end and than it counts as button press. Anyway the function is cancelled in between

like

Case 1: led 1 on led 2 on ON BUTTON PRESS CANCEL REMAINING PATTERN AND GOTO CASE 2 (all caps to highlight :)) led 3 on led 4 on .... 15 more patterns and so on break

I figured it out :smiley:

Problem, button click was not recognized until Switch Case 1 was not fully complete = all leds finished blinking.

Solution: I removed switch case from void button and made a separate void for it and asked void loop to check the button. so now. When i press it immediately stops led blinkings and move to next function.

//************************* LED Mode Button ******************************//
void ledmodebutfunc() {
  PORTC = B00001010; // selecting input of leds mode (analog multiplexer)
  delay (5);

  // read the pushbutton input pin:
  LedModebuttonState = digitalRead(SensorsInput);

  // compare the LedModebuttonState to its previous state
  if (LedModebuttonState != LedlastModebuttonState) {
    // if the state has changed, increment the counter
    if (LedModebuttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      LedModebuttoncount++;
      if (LedModebuttoncount >= 3) {
        LedModebuttoncount = 0;
      }
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(LedModebuttoncount);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  LedlastModebuttonState = LedModebuttonState;

}// end of void

//************************ CALLING FUNCTIONS ******************//

void ledconditions() { // this will check the button counts and call appropriate functions
  
  if (LedModebuttoncount == 1) {
    Serial.print("inside void led condition");
    Serial.println(LedModebuttoncount);
    arifledpattern(); // the led pattern i placed in another void to avoid mess in this part
  }
  else if (LedModebuttoncount == 2) { // writting only first 4 pins of shift register 595
    digitalWrite(latchPin, LOW);
    bitWrite(pinState, 0, LOW);
    bitWrite(pinState, 1, LOW);
    bitWrite(pinState, 2, LOW);
    bitWrite(pinState, 3, LOW);
    bitWrite(pinState, 4, HIGH);
    shiftingarif();
  }
} // end

"void" is a parameter (or lack thereof).

You are talking about functions.

PaulS: You would have to read the state of the first three pins, or keep track of what you wrote to them, and set the values in patterns[n], using bitWrite() or bitSet(), before shifting out the value.

To keep the last three pins on:

   bitSet(patterns[index * 2], 0);
   bitSet(patterns[index * 2], 1);
   bitSet(patterns[index * 2], 2);
// Then shift the patterns[index * 2] value out.

I would not do it that way. That way is modifying (corrupting) the original data. Plus you have to not only set bits but also clear them depending on the state of the individual bits. I would simply ensure that the unused bits in the patterns table are always zero (which already they are) and then OR in the other bits for the byte right as you send the byte to the shift register.

So assuming you have the other bits set appropriately in a byte variable called otherbits

You would do something like:

shiftOut(dataPin, clockPin, MSBFIRST, patterns[index * 2]|otherbits);

That way you simply set your other bits in otherbits, and the bits will updated in the 595 on the next shiftout operation.

--- bill

Thank you for kind responses, they helped me understand.

Following worked for me. Made separate functions for both 595 shiftouts and than used where ever i called shiftout.

void shiftregisterGP() { // general purpose 595, for lights and all
  shiftOut(dataPin, clockPin, MSBFIRST, shiftregister2);
}
void shiftregisterLED() { // 595 specific for led lights
  shiftOut(dataPin, clockPin, MSBFIRST, shiftregister1);
}

used them on every call, so all 595s are told what to do.

if (CPUsensorread > Settempsensorread ) { // thermostat cpu and threshold comparision 
    digitalWrite(latchPin, LOW);
    bitSet(shiftregister2, thermostatfanout);
    shiftregisterLED();
    shiftregisterGP();
    digitalWrite(latchPin, HIGH);

  }
  else  {
    digitalWrite(latchPin, LOW);
    bitClear(shiftregister2, thermostatfanout);
    shiftregisterLED();
    shiftregisterGP();
    digitalWrite(latchPin, HIGH);


  }

same way for led button to call different led patterns

switch (LedModebuttoncount) {
    case 0:
      Serial.println (F("default led"));
      digitalWrite(latchPin, LOW);
      shiftregister1 = 0b00000000;
      shiftregisterLED();
      shiftregisterGP();
      digitalWrite(latchPin, HIGH);
      break;

    case 1:
      shiftregister1 = Array[nextLed];
      ledsequencer();
      break;

Thankyou! to everyone that helped me finish my first arduino / microcontroller project

meet Sparrow. my CPU fan controller / Digital Thermometer / Dancing leds / humidity sensor.

Thankyou again to everyone for helping everybody.