Skip to content

Instantly share code, notes, and snippets.

@JelleReith
Last active March 13, 2017 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JelleReith/638b9ddb6fad8fe564adbdc5d83b8154 to your computer and use it in GitHub Desktop.
Save JelleReith/638b9ddb6fad8fe564adbdc5d83b8154 to your computer and use it in GitHub Desktop.
int latchPin = 3;
int clockPin = 4;
int dataPin = 2;
int numOfRegisters = 2;
byte registerState[2];
int pause = 2000;
void setup(void)
{
Serial.begin(9600);
//while( !Serial ) {} // wait for serial init
//byte registerState = new byte[numOfRegisters];
for (size_t i = 0; i < numOfRegisters; i++) {
registerState[i] = 0;
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop(void)
{
Serial.println("PING");
coil_write(0,2);
delay(pause);
Serial.println("PONG");
coil_write(0,1);
delay(pause);
}
void coil_write(int coil_id,int state){
int dir_0_pin;
int dir_1_pin;
int onoff_pin;
int dir;
int onoff;
if(coil_id%2 == 0) {
int shifter_id = coil_id/2;
dir_0_pin = (shifter_id*8)+2;
dir_1_pin = (shifter_id*8)+4;
onoff_pin = (shifter_id*8)+3;
}else if(coil_id%2 == 1){
float shifter_id_float = (coil_id/2)-0.5;
int shifter_id = int(shifter_id_float);
dir_0_pin = (shifter_id*8)+5;
dir_1_pin = (shifter_id*8)+7;
onoff_pin = (shifter_id*8)+6;
}
if(state == 0) {
onoff = 0;
}
else if(state == 1){
onoff = 1;
dir = 0;
}
else if(state == 2){
onoff = 1;
dir = 1;
}
regWrite(onoff_pin,onoff);
regWrite(dir_0_pin, dir);
regWrite(dir_1_pin, !dir);
}
void regWrite(int p, bool state) {
int reg = p / 8;
int actualPin = p - (8 * reg);
digitalWrite(latchPin, LOW);
for (int i = 0; i < numOfRegisters; i++) {
//byte states = registerState[i];
if (i == reg) {
bitWrite(registerState[i], actualPin, state);
}
shiftOut(dataPin, clockPin, MSBFIRST, registerState[i]);
}
digitalWrite(latchPin, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment