hello guys,
i managed to make a program, using the codes from the tutorial here on the website of the IC 4021 Shift In, that lights some LEDs when a certain push button is pressed so i thought about sharing it if anyone is in need
(for the circuit use the same one in this tutorial https://www.arduino.cc/en/Tutorial/ShiftIn )
so here it is :
(Note: If you have a better way please post it i would be glad to go by it
)
//4021 Shift In controlling LEDs
//define where your pins are
int latchPin = 8;
int dataPin = 9;
int clockPin = 7;
//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 72; //01001000
//define an array that corresponds to values for each
//of the shift register's pins
boolean lastPushButton1 = LOW;
boolean currentPushButton1 = LOW;
boolean lastPushButton2 = LOW;
boolean currentPushButton2 = LOW;
boolean lastPushButton3 = LOW;
boolean currentPushButton3 = LOW;
boolean lastPushButton4 = LOW;
boolean currentPushButton4 = LOW;
void setup() {
//start serial
Serial.begin(9600);
//define pin modes
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
//the arduino Pins that the LEDs are connected to
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
//Pulse the latch pin:
//set it to 1 or HIGH to collect parallel data
digitalWrite(latchPin,1);
//wait
delayMicroseconds(0.5);
//set it to 0 or LOW to transmit data serially
digitalWrite(latchPin,0);
//while the shift register is in serial mode
//collect each shift register into a byte
//the register attached to the chip comes in first
switchVar1 = shiftIn(dataPin, clockPin);
//Switch Statement to check which push button is pressed to light up the RIGHT LED !
switch (switchVar1) {
case B00000001:
currentPushButton1 = !currentPushButton1;
if(lastPushButton1 != currentPushButton1 == HIGH){
digitalWrite(2, currentPushButton1);
}
lastPushButton1 = currentPushButton1;
break;
case B00000010:
currentPushButton2 = !currentPushButton2;
if(lastPushButton2 != currentPushButton2 == HIGH){
digitalWrite(3, currentPushButton2);
}
lastPushButton2 = currentPushButton2;
break;
case B00000100:
currentPushButton3 = !currentPushButton3;
if(lastPushButton3 != currentPushButton3 == HIGH){
digitalWrite(4, currentPushButton3);
}
lastPushButton3 = currentPushButton3;
break;
case B00001000:
currentPushButton4 = !currentPushButton4;
if(lastPushButton4 != currentPushButton4 == HIGH){
digitalWrite(5, currentPushButton4);
}
lastPushButton4 = currentPushButton4;
break;
}// ------end of switch Statement
}
//------------------------------------------------end main loop
byte shiftIn(int myDataPin, int myClockPin) {
int i;
int DigitalReadDataPin = 0;
int pinState;
byte myDataIn = 0;
for (i=7; i>=0; i--)
{
digitalWrite(myClockPin, 0);
delayMicroseconds(0.2);
DigitalReadDataPin = digitalRead(myDataPin);
if (DigitalReadDataPin) { //this means if DigitalReadDataPin == HIGH
pinState = 1;
//set the bit to 0 no matter what
myDataIn = myDataIn | (1 << i);
}
else {
pinState = 0;
}
digitalWrite(myClockPin, 1);
}
return myDataIn;
}