I want to toggle 08 leds connected on output of 74HC595 through 08 button on arduino.
0~6 (7 button) working fine
But when #7 (8th button) is high led ON if I keep it high and try to toggle other 7 button all goes wrong leds randomly toggling.
If i will press #7 (8th button) again and all 07 button and leds working fine.
Where I am wrong.
My sketch and library attached.
#include <Shifter.h>
#define SER_Pin 2 //Data pin SER_IN
#define RCLK_Pin 3 //Latch Pin L_CLOCK
#define SRCLK_Pin 4 //Clock Pin CLOCK
#define NUM_REGISTERS 1 //how many registers are in the chain
//initaize shifter using the Shifter library
Shifter shifter(SER_Pin, RCLK_Pin, SRCLK_Pin, NUM_REGISTERS);
const uint32_t debounceTime = 20; // 5 mSec, enough for most switches
const uint8_t switchPin0 = A0; // with n.o. momentary pb switch to ground
const uint8_t switchPin1 = A1; // with n.o. momentary pb switch to ground
const uint8_t switchPin2 = A2; // with n.o. momentary pb switch to ground
const uint8_t switchPin3 = A3; // with n.o. momentary pb switch to ground
const uint8_t switchPin4 = A4; // with n.o. momentary pb switch to ground
const uint8_t switchPin5 = A5; // with n.o. momentary pb switch to ground
const uint8_t switchPin6 = 7; // with n.o. momentary pb switch to ground
const uint8_t switchPin7 = 6; // with n.o. momentary pb switch to ground
const bool switchOn = false; // using INPUT_PULLUP
const bool switchOff = true;
bool lastState0 = switchOff;
bool newState0 = switchOff;
bool toggleState0 = false;
bool lastState1 = switchOff;
bool newState1 = switchOff;
bool toggleState1 = false;
bool lastState2 = switchOff;
bool newState2 = switchOff;
bool toggleState2 = false;
bool lastState3 = switchOff;
bool newState3 = switchOff;
bool toggleState3 = false;
bool lastState4 = switchOff;
bool newState4 = switchOff;
bool toggleState4 = false;
bool lastState5 = switchOff;
bool newState5 = switchOff;
bool toggleState5 = false;
bool lastState6 = switchOff;
bool newState6 = switchOff;
bool toggleState6 = false;
bool lastState7 = switchOff;
bool newState7 = switchOff;
bool toggleState7 = false;
void setup() {
pinMode ( switchPin0, INPUT_PULLUP );
pinMode ( switchPin1, INPUT_PULLUP );
pinMode ( switchPin2, INPUT_PULLUP );
pinMode ( switchPin3, INPUT_PULLUP );
pinMode ( switchPin4, INPUT_PULLUP );
pinMode ( switchPin5, INPUT_PULLUP );
pinMode ( switchPin6, INPUT_PULLUP );
pinMode ( switchPin7, INPUT_PULLUP );
shifter.clear(); //set all pins on the shift register chain to LOW
shifter.write(); //send changes to the chain and display them
}
void loop() {
newState0 = digitalRead( switchPin0 );
newState1 = digitalRead( switchPin1 );
newState2 = digitalRead( switchPin2 );
newState3 = digitalRead( switchPin3 );
newState4 = digitalRead( switchPin4 );
newState5 = digitalRead( switchPin5 );
newState6 = digitalRead( switchPin6 );
newState7 = digitalRead( switchPin7 );
////////////////////////*********0**********///////////////////////////
if ( lastState0 != newState0 ) // state changed
{
delay( debounceTime );
lastState0 = newState0;
// push on, push off
if ( newState0 == switchOn && toggleState0 == false )
{
toggleState0 = true;
shifter.setPin(0, HIGH);
shifter.write();
}
else if ( newState0 == switchOn && toggleState0 == true )
{
toggleState0 = false;
shifter.setPin(0, LOW);
shifter.write();
}
}
////////////////////////////////////1//////////////////////////////////////////
if ( lastState1 != newState1 ) // state changed
{
delay( debounceTime );
lastState1 = newState1;
// push on, push off
if ( newState1 == switchOn && toggleState1 == false )
{
toggleState1 = true;
shifter.setPin(1, HIGH);
shifter.write();
}
else if ( newState1 == switchOn && toggleState1 == true )
{
toggleState1 = false;
shifter.setPin(1, LOW);
shifter.write();
}
}
////////////////////////*********2**********///////////////////////////
if ( lastState2 != newState2 ) // state changed
{
delay( debounceTime );
lastState2 = newState2;
// push on, push off
if ( newState2 == switchOn && toggleState2 == false )
{
toggleState2 = true;
shifter.setPin(2, HIGH);
shifter.write();
}
else if ( newState2 == switchOn && toggleState2 == true )
{
toggleState2 = false;
shifter.setPin(2, LOW);
shifter.write();
}
}
////////////////////////*********3**********///////////////////////////
if ( lastState3 != newState3 ) // state changed
{
delay( debounceTime );
lastState3 = newState3;
// push on, push off
if ( newState3 == switchOn && toggleState3 == false )
{
toggleState3 = true;
shifter.setPin(3, HIGH);
shifter.write();
}
else if ( newState3 == switchOn && toggleState3 == true )
{
toggleState3 = false;
shifter.setPin(3, LOW);
shifter.write();
}
}
////////////////////////*********4**********///////////////////////////
if ( lastState4 != newState4 ) // state changed
{
delay( debounceTime );
lastState4 = newState4;
// push on, push off
if ( newState4 == switchOn && toggleState4 == false )
{
toggleState4 = true;
shifter.setPin(4, HIGH);
shifter.write();
}
else if ( newState4 == switchOn && toggleState4 == true )
{
toggleState4 = false;
shifter.setPin(4, LOW);
shifter.write();
}
}
////////////////////////*********5**********///////////////////////////
if ( lastState5 != newState5 ) // state changed
{
delay( debounceTime );
lastState5 = newState5;
// push on, push off
if ( newState5 == switchOn && toggleState5 == false )
{
toggleState5 = true;
shifter.setPin(5, HIGH);
shifter.write();
}
else if ( newState5 == switchOn && toggleState5 == true )
{
toggleState5 = false;
shifter.setPin(5, LOW);
shifter.write();
}
}
////////////////////////*********6**********///////////////////////////
if ( lastState6 != newState6 ) // state changed
{
delay( debounceTime );
lastState6 = newState6;
// push on, push off
if ( newState6 == switchOn && toggleState6 == false )
{
toggleState6 = true;
shifter.setPin(6, HIGH);
shifter.write();
}
else if ( newState6 == switchOn && toggleState6 == true )
{
toggleState6 = false;
shifter.setPin(6, LOW);
shifter.write();
}
}
////////////////////////*********7**********///////////////////////////
if ( lastState7 != newState7 ) // state changed
{
delay( debounceTime );
lastState7 = newState7;
// push on, push off
if ( newState7 == switchOn && toggleState7 == false )
{
toggleState7 = true;
shifter.setPin(7, HIGH);
shifter.write();
}
else if ( newState7 == switchOn && toggleState7 == true )
{
toggleState7 = false;
shifter.setPin(7, LOW);
shifter.write();
}
}
////////////////////////////////////////////////////////////////////////////
}