#define pinb 8
#define pinr 9
#define v12pin 11
int dataPin = 2;
int dataPin2 = 5;
int latchPin = 3;
int clockPin = 4;
int LR = 0;
byte bitarray[7];
int bval = 1;
//int b1 = 1;
//int b2 = 256;
#define MaxShiftRegisters 1
#define SpeedCnst 66
#define SubSpeedCnst 66
char YourName[] = "";
void setup()
{
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
MyRider(true); //working
MyRider(false); //working
MyRider(true); //working
MyRider(false); //working
byte RP=random(MaxShiftRegisters)+1;
switch (RP)
{
case 1: for(int n=1; n<10; n++) Pattern1();
case 2: for(int n=1; n<10; n++) Pattern2();
case 3: for(int n=1; n<10; n++) Pattern3();
case 4: for(int n=1; n<10; n++) Pattern4();
}
}
void Pattern1()
{
LR=random(MaxShiftRegisters)+1;
byte bp[4] = {B10000001,B01000010,B00100100,B00011000};
for (int n=0; n<=5; n++)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
// bitWrite(bp,i2,0);
}
for (int n=4; n>=0; n--)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
}
}
void Pattern2()
{
LR=random(MaxShiftRegisters)+1;
byte bp[4] = {B11000000,B00110000,B00001100,B00000011};
for (int n=0; n<=5; n++)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
// bitWrite(bp,i2,0);
}
for (int n=4; n>=0; n--)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
}
}
void Pattern3()
{
LR=random(MaxShiftRegisters)+1;
byte bp[4] = {B11111111,B00000000,B11111111,B00000000};
for (int n=0; n<=5; n++)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
// bitWrite(bp,i2,0);
}
for (int n=4; n>=0; n--)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
}
}
void Pattern4()
{
LR=random(MaxShiftRegisters)+1;
byte bp[4] = {B01010101,B10101010,B01010101,B10101010};
for (int n=0; n<=5; n++)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
// bitWrite(bp,i2,0);
}
for (int n=4; n>=0; n--)
{
ShiftByteOut(LR,bp[n]);
delay(SubSpeedCnst);
}
}
void ShiftByteOut(byte chip, byte b)
{
byte bp = B00000000;
byte bp2 = B00000000;
digitalWrite(latchPin, LOW);
for(byte n2=0; n2<MaxShiftRegisters+1; n2++)
{
if (chip==n2)
shiftOut(dataPin, clockPin, MSBFIRST, b);
else
shiftOut(dataPin, clockPin, MSBFIRST, bp2);
}
digitalWrite(latchPin, HIGH);
}
void MyRider(boolean up)
{
byte bp = B00000000;
byte bp2 = B00000000;
// LR=random(3)+1; //random 74hc595 chip
LR++;
if (LR>3)
LR=1;
if (up==true)
for (int i2=0; i2<=7; i2++)
{
bitWrite(bp,i2,1);
ShiftByteOut(LR,bp);
delay(SpeedCnst);
bitWrite(bp,i2,0);
}
if (up==false)
for (int i2=7; i2<=0; i2++)
{
bitWrite(bp,i2,1);
ShiftByteOut(LR,bp);
delay(SpeedCnst);
bitWrite(bp,i2,0);
}
}
void BinaryKnightRider(boolean up)
{
byte wclear = 0;
int b1 = 1; //original
int b2 = 256; //original value 256
if (up==true)
{
while (b1<177)
{
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
// shiftOut(dataPin, clockPin, MSBFIRST, wclear);
shiftOut(dataPin, clockPin, MSBFIRST, b1);
shiftOut(dataPin, clockPin, MSBFIRST, b2);
// shiftOut(dataPin, clockPin, MSBFIRST, wclear);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
delay(SpeedCnst);
b2 = b2 >> 1;
b1 = b1 << 1;
// b2--;
// b1++;
}
}
if (up==false)
{
b2=256;
//b2=256; //original value 256
b1=1;
// b1=1; //original
while (b2>0)
{
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, MSBFIRST, b2);
// shiftOut(dataPin, clockPin, MSBFIRST, wclear);
shiftOut(dataPin, clockPin, MSBFIRST, b1);
// shiftOut(dataPin, clockPin, MSBFIRST, wclear);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
delay(SpeedCnst);
// b2--;
// b1++;
b2 = b2 >> 1;
b1 = b1 << 1;
}
}
}
void Clear()
{
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, MSBFIRST, 0);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
}
void DisplayName()
{
char c;
int i = 0;
c=YourName[i];
while(c!='\0')
{
// Disable the latch while we clock in data
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, MSBFIRST, c);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
delay(SpeedCnst);
i++;
c=YourName[i];
}
}
void BinaryCountUp()
{
int currentValue = 1;
while (currentValue<=255)
{
// Disable the latch while we clock in data
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, MSBFIRST, currentValue);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
delay(SpeedCnst);
currentValue = currentValue << 1 ;
}
}
void BinaryC3()
{
int currentValue = 1;
int currentValue2 = 255;
// int bval = B10101010;
// int bval2 = B11110000;
while (currentValue<=255)
{
// Disable the latch while we clock in data
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, MSBFIRST, currentValue2);
shiftOut(dataPin, clockPin, MSBFIRST, currentValue);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
// digitalWrite(latchPin, LOW);
// shiftOut(dataPin2, clockPin, MSBFIRST, currentValue2);
// digitalWrite(latchPin, HIGH);
delay(SpeedCnst);
currentValue++;
// currentValue2--;
}
}
void BinaryCR()
{
int currentValue = 255;
while (currentValue>=1)
{
// Disable the latch while we clock in data
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, MSBFIRST, currentValue);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
delay(SpeedCnst);
currentValue = currentValue >> 1 ;
}
}
void BinaryCR2()
{
int currentValue = 255;
while (currentValue>=1)
{
// Disable the latch while we clock in data
digitalWrite(latchPin, LOW);
// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, LSBFIRST, currentValue);
// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);
delay(SpeedCnst);
currentValue = currentValue >> 1 ;
}
}
this is my code playing with 3 74HC595N registers.
this is the way i got around the "bit" issue.