Skip to content

Instantly share code, notes, and snippets.

@Lukelectro
Last active May 13, 2018 18:53
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 Lukelectro/f161c368e1bd4c3702d457648f17b016 to your computer and use it in GitHub Desktop.
Save Lukelectro/f161c368e1bd4c3702d457648f17b016 to your computer and use it in GitHub Desktop.
#define STEPSPERROT (200)
#define MICROSTEPS (16)
#define INTERVALS (512)
#define MAGNETIZETIME (200) // miliseconds the magnet is powered. Hint from Aart: short, high current pulses (dump a capacitor) work best.
#define LEDPIN 17 // not 13 on pro micro, but 17
//#define DEBUG
#define FULLROTSTEPS (STEPSPERROT*MICROSTEPS)
#define STEPSPERINTERVAL (FULLROTSTEPS/INTERVALS)
#define REMAINDER_S (FULLROTSTEPS%INTERVALS)
void steps(int num, int dly=300){
for(int i=0;i<num;i++){
digitalWrite(2,HIGH);
delayMicroseconds(dly/2);
digitalWrite(2,LOW);
delayMicroseconds(dly/2);
}
}
void magnetize(){
digitalWrite(5,HIGH);
delay(MAGNETIZETIME);
digitalWrite(5,LOW);
delay(200); // zodat relais ook weer af valt...
}
void setup() {
// put your setup code here, to run once:
pinMode(16, INPUT); // jumper
pinMode(2,OUTPUT); // step
pinMode(3,OUTPUT); // dir
pinMode(5, OUTPUT); // magnet
pinMode(LEDPIN, OUTPUT);
// en 10 is de potmeter
digitalWrite(3,LOW);
#if defined(DEBUG)
digitalWrite(LEDPIN,LOW);
// draai 1 rondje zodat ik weet of ik het aantal stappen goed heb (met LED aan)
steps(FULLROTSTEPS,200);
digitalWrite(LEDPIN,HIGH);
#endif
}
void loop() {
int schr=0;// voor de "schrikkelsteps"
for(int i=0;i<INTERVALS;i++){
steps(STEPSPERINTERVAL);
schr+=REMAINDER_S;
if(schr>STEPSPERINTERVAL){ // schrikkelstep doen?
schr-=STEPSPERINTERVAL;
steps(1);
}
magnetize();
}
// Serial.Write("DONE!"); // TODO: Serial UI, so you can tell it the variables that now are defines and it tells you what it' s doing and when it' s done
digitalWrite(LEDPIN,LOW); // done (LOW = LED AAN)
while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment