Dear community,
Please help,
I'm trying to write a smal program to make the arduino drive a shift register.
I would like the program to read the the values stored in myArray and store them in the variable "code"
I can't manage to do so, the values are translated automatically to decemal values.
I've introduced an extra step to translate them back to a byte, but this makes the the property a string which also isn't working.
[code]
// Pin mapping:
int DATA = 13; // Pin 13 is DATA output
int SRCK = 12; // Pin 12 is Shift Register Clock (on the rising edge of this clock the data is shifted into the register)
int SRCLR_ = 11; // Pin 11 is Shift Register Clear (make this pin low to clear the shift register)
int RCK = 10; // Pin 10 = Register Clock (on the rising edge of this clock the data is moved to the output register)
int G_ = 9; // Pin 9 is Output Enable (make this pin high to enable the output)
void setup() {
// put your setup code here, to run once:
pinMode(DATA, OUTPUT);
pinMode(SRCK, OUTPUT);
pinMode(SRCLR_, OUTPUT);
pinMode(RCK, OUTPUT);
pinMode(G_, OUTPUT);
Serial.begin(9600); // Open a serial port
// Clear all data and switch of all output
digitalWrite(G_, LOW); // Switch off the register output
digitalWrite(SRCLR_, LOW); // Clear the shift register
}
byte code = B00000000;
byte precode = B00000000;
int speed = 100;
byte mask = 1; //our bitmask
byte bitDelay = 100;
int loopcount = 8;
int arraycount = 0;
byte myArray[] = {B00000000, B10000000, B11100000, B01100000, B01000000, B01110000, B00110000, B00100000, B00111000, B00011000, B00010000, B00011100, B00001100, B00001000, B00001110, B00000110, B00000100, B00000111, B00000011, B00000010, B1000011, B10000001, B00000001, B11000001, B11000000};
void loop() {
// Switch on the shift register
precode = myArray[arraycount];
if (arraycount > 24) {
arraycount = 0;
}
Serial.print(" Variable value = ");
Serial.print(myArray[4]);
{
int zeros = 8 - String(precode, BIN).length();
String code;
for (int i = 0; i < zeros; i++) {
code = code + "0";
}
}
digitalWrite(G_, HIGH); // Switch on the register output
digitalWrite(SRCLR_, HIGH); // Disable the clear funtion
while (loopcount > 0) {
for (mask = 00000001; mask > 0; mask <<= 1) { //iterate through bit mask
if (code & mask) { // if bitwise AND resolves to true
digitalWrite(DATA, HIGH); // send 1
digitalWrite(SRCK, HIGH); // Clock = 1 (shift data in)
digitalWrite(SRCK, LOW); // Clock = 0
digitalWrite(DATA, LOW); // Set data pin to 0
}
else { //if bitwise AND resolves to false
digitalWrite(DATA, LOW); // send 0
digitalWrite(SRCK, HIGH); // Clock = 1 (shift data in)
digitalWrite(SRCK, LOW); // Clock = 0
}
delay(bitDelay);
loopcount--;
}
switch_on(); // Move register content to output
delay(1000);
}
arraycount++;
}
// Functions:
void switch_on() {
digitalWrite(RCK, HIGH); // Move shift register to output
digitalWrite(RCK, LOW); // Reset shift register output
}
[/code]