Skip to content

Instantly share code, notes, and snippets.

@dropmeaword
Forked from JelleReith/coil.ino
Last active March 13, 2017 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dropmeaword/6673fd3fe5c47c3c89eeb3ca46898c81 to your computer and use it in GitHub Desktop.
Save dropmeaword/6673fd3fe5c47c3c89eeb3ca46898c81 to your computer and use it in GitHub Desktop.
// NOTE: no coil_write mapping yet
const int dataPin = 13;
const int latchPin = 14;
const int clockPin = 12;
int numOfRegisters = 4;
byte registerState[4];
int pause = 100;
void setup(void)
{
Serial.begin(115200);
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("PANG");
// coil_write(2, 0);
// delay(pause);
// Serial.println("PING");
// coil_write(2, 2);
// delay(pause);
// Serial.println("PONG");
// coil_write(2, 1);
// delay(pause);
//
// Serial.println("0 = HIGH");
// regWrite(0, HIGH); // testing
// regWrite(0, LOW); // testing
// delay(pause*2);
Serial.println("coil 1");
// one coil up
regWrite(3,HIGH); // on/off
regWrite(2, HIGH); // direction
regWrite(4,LOW); // direction
delay(pause);
// same coil down
regWrite(2,LOW);
regWrite(4,HIGH);
delay(pause);
// same coil (inactive)
regWrite(3,LOW);
regWrite(4,LOW);
delay(pause);
Serial.println("coil 2");
// one coil up
regWrite(6,HIGH); // on/off
regWrite(5, HIGH); // direction
regWrite(7,LOW); // direction
delay(pause);
// same coil down
regWrite(5,LOW);
regWrite(7,HIGH);
delay(pause);
// same coil (inactive)
regWrite(6,LOW);
regWrite(7,LOW);
delay(pause);
Serial.println("coil 3");
// one coil up
regWrite(11,HIGH); // on/off
regWrite(10, HIGH); // direction
regWrite(12,LOW); // direction
delay(pause);
// same coil down
regWrite(10,LOW);
regWrite(12,HIGH);
delay(pause);
// same coil (inactive)
regWrite(11,LOW);
regWrite(12,LOW);
delay(pause);
Serial.println("coil 4");
// one coil up
regWrite(19,HIGH); // on/off
regWrite(18, HIGH); // direction
regWrite(20,LOW); // direction
delay(pause);
// same coil down
regWrite(18,LOW);
regWrite(20,HIGH);
delay(pause);
// same coil (inactive)
regWrite(19,LOW);
regWrite(20,LOW);
delay(pause);
Serial.println("coil 5");
// one coil up
regWrite(22,HIGH); // on/off
regWrite(21, HIGH); // direction
regWrite(23,LOW); // direction
delay(pause);
// same coil down
regWrite(21,LOW);
regWrite(23,HIGH);
delay(pause);
// same coil (inactive)
regWrite(22,LOW);
regWrite(23,LOW);
delay(pause);
Serial.println("coil 6");
// one coil up
regWrite(27,HIGH); // on/off
regWrite(26, HIGH); // direction
regWrite(28,LOW); // direction
delay(pause);
// same coil down
regWrite(26,LOW);
regWrite(28,HIGH);
delay(pause);
// same coil (inactive)
regWrite(27,LOW);
regWrite(28,LOW);
delay(pause);
Serial.println("-------------");
Serial.println();
}
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) {
dir_0_pin = (coil_id*8)+2;
dir_1_pin = (coil_id*8)+4;
onoff_pin = (coil_id*8)+3;
}else if(coil_id%2 == 1){
dir_0_pin = (coil_id*8)+5;
dir_1_pin = (coil_id*8)+7;
onoff_pin = (coil_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 >> 3; /// 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