/* JogControllerTest Program * Author: Pete Holm * based on a similar CD4021B Shift Register example (shiftIn, by Carlyn Maw) */ #include SoftwareSerial mySerial(1, 0); // RX, TX //define where pins are - these are ardunio pins not ATtiny pins int latchPin = 3; int dataPin = 7; int clockPin = 8; //Define variables to hold the data //for shift register. //starting with a non-zero numbers can help //troubleshoot byte switchVar1 = 72; //01001000 void setup() { // Open serial communications and wait for port to open: // set the data rate for the SoftwareSerial port mySerial.begin(115200); //define pin modes pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, INPUT); } void loop() { // run over and over //Pulse the latch pin: //set it to 1 to collect parallel data digitalWrite(latchPin,1); //set it to 1 to collect parallel data, wait delayMicroseconds(20); //set it to 0 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); if ((switchVar1 != 0) && (switchVar1 != 1000000)) { mySerial.print(switchVar1, BIN); } // Place holder to print out the direction button pressed if (switchVar1 & (1 << 0) ){ //print the value of the array location mySerial.println(": stop"); } else if (switchVar1 & (1 << 1) ){ //print the value of the array location mySerial.println(": run"); } else if (switchVar1 & (1 << 2) ){ //print the value of the array location mySerial.println(": in"); } else if (switchVar1 & (1 << 3) ){ //print the value of the array location mySerial.println(": out"); } else if (switchVar1 & (1 << 4) ){ //print the value of the array location mySerial.println(": down"); } else if (switchVar1 & (1 << 5) ){ //print the value of the array location mySerial.println(": right"); } else if (switchVar1 & (1 << 6) ){ //print the value of the array location // mySerial.println(": left"); } else if (switchVar1 & (1 << 7) ){ //print the value of the array location mySerial.println(": up"); } delay(250); } ////// shiftIn function ///// just needs the location of the data pin and the clock pin ///// it returns a byte with each bit in the byte corresponding ///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0 byte shiftIn(int myDataPin, int myClockPin) { int i; int temp = 0; int pinState; byte myDataIn = 0; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, INPUT); //we will be holding the clock pin high 8 times (0,..,7) at the //end of each time through the for loop //at the begining of each loop when we set the clock low, it will //be doing the necessary low to high drop to cause the shift //register's DataPin to change state based on the value //of the next bit in its serial information flow. //The register transmits the information about the pins from pin 7 to pin 0 //so that is why our function counts down for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); delayMicroseconds(0.2); temp = digitalRead(myDataPin); if (temp) { pinState = 1; //set the bit to 0 no matter what myDataIn = myDataIn | (1 << i); } else { //turn it off -- only necessary for debuging //print statement since myDataIn starts as 0 pinState = 0; } digitalWrite(myClockPin, 1); } return myDataIn; }