@@ -11,6 +11,16 @@ int latchPin = 4; //Pin connected to ST_CP of 74HC595
1111int myClockPin = 3 ; // Pin connected to SH_CP of 74HC595
1212int myDataPin = 2 ; // Pin connected to DS of 74HC595
1313
14+ unsigned int index = 0 ;
15+ unsigned int counter = 0 ;
16+
17+ uint8_t draw_state = 0 ;
18+
19+ char buffer[256 ];
20+ bool ready = false ;
21+
22+ int cnt = 0 ;
23+
1424void stepperRotate (float rotation, float rpm);
1525
1626enum
2131
2232enum ACCEL_STATE
2333{
24- RAMP_UP = 0 ,
34+ INITAL=0 ,
35+ RAMP_UP,
2536 CONSTANT,
2637 RAMP_DOWN,
2738};
3344};
3445// char modes[] = {MODE_CONSTANT,MODE_CONSTANT,MODE_CONSTANT,MODE_CONSTANT,MODE_CONSTANT,MODE_CONSTANT,MODE_CONSTANT,MODE_CONSTANT};
3546char modes[] = {MODE_SET_POSITION,MODE_SET_POSITION,MODE_SET_POSITION,MODE_SET_POSITION,MODE_SET_POSITION,MODE_SET_POSITION,MODE_SET_POSITION,MODE_SET_POSITION};
36- char accelState[] = {RAMP_UP,RAMP_UP,RAMP_UP,RAMP_UP,RAMP_UP,RAMP_UP,RAMP_UP,RAMP_UP };
47+ char accelState[] = {INITAL,INITAL,INITAL,INITAL,INITAL,INITAL,INITAL,INITAL };
3748char directions[] = {DIRECTION_FORWARD,DIRECTION_FORWARD,DIRECTION_FORWARD,DIRECTION_FORWARD,DIRECTION_FORWARD,DIRECTION_FORWARD,DIRECTION_FORWARD,DIRECTION_FORWARD};
3849long positions[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
3950long destPositions[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
4051bool changing[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
41- bool enabledState[] = {1 ,1 ,0 ,0 ,0 ,0 ,0 ,0 };
52+ bool enabledState[] = {1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
4253unsigned int times[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
4354unsigned long speeds[] = {1000 , 600 , 600 , 600 , 600 , 600 , 600 , 600 };
55+
56+ long initalRampSpeed = 10000 ;
57+
58+ long rampSpeeds[] = {initalRampSpeed,initalRampSpeed,initalRampSpeed,initalRampSpeed,initalRampSpeed,initalRampSpeed,initalRampSpeed,initalRampSpeed};
4459bool states[] = {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 };
4560bool stepModes[8 ][3 ] = {{0 ,0 ,0 },{0 ,0 ,0 },{0 ,0 ,0 },{0 ,0 ,0 },{0 ,0 ,0 },{0 ,0 ,0 },{0 ,0 ,0 },{0 ,0 ,0 }};
4661unsigned long startSpeeds[] = {2000 , 2000 , 2000 , 2000 , 2000 , 2000 , 2000 , 2000 };
@@ -121,16 +136,6 @@ void receiveEvent(int bytes)
121136
122137void timerIsr () { stepperRotate () ;}
123138
124- unsigned int index = 0 ;
125- unsigned int counter = 0 ;
126-
127- uint8_t draw_state = 0 ;
128-
129- char buffer[256 ];
130- bool ready = false ;
131-
132- int cnt = 0 ;
133-
134139void p (char * buf, char *fmt, ... )
135140{
136141 va_list args;
@@ -353,11 +358,57 @@ void stepperRotate()
353358 unsigned int diff = counter - times[i];
354359 if (!enabledState[i]) { continue ;}
355360 // if(diff > speeds[i] || states[i])
361+
362+ if (positions[i] == destPositions[i])
363+ {
364+ accelState[i] = INITAL;
365+
366+ rampSpeeds[i] = initalRampSpeed;
367+
368+ continue ;
369+ } // Breaks constant rotation
370+
371+ switch (accelState[i])
372+ {
373+ case INITAL:
374+
375+ accelState[i] = RAMP_UP;
376+
377+ goto doContinue;
378+
379+
380+ case RAMP_UP:
381+
382+ rampSpeeds[i] -= 10 ;
383+
384+ if (rampSpeeds[i] < 0 )
385+ {
386+ accelState[i] = CONSTANT;
387+
388+ goto doContinue;
389+ }
390+
391+ if (diff <= rampSpeeds[i])
392+ {
393+ continue ;
394+ }
395+
396+ break ;
397+
398+ case CONSTANT:
399+ break ;
400+
401+ case RAMP_DOWN:
402+ break ;
403+ }
404+
405+ doContinue:
406+
356407 if (diff <= speeds[i]) { continue ;}
357408
358409 stepperPosChanged[i] = true ;
359410 times[i] = counter;
360- switch (modes[i])
411+ switch (modes[i]) // Try to move this code above, or combine it
361412 {
362413 case MODE_CONSTANT:
363414 states[i] = !states[i];
@@ -370,8 +421,9 @@ void stepperRotate()
370421 {
371422 if (positions[i] == destPositions[i])
372423 {
373- states[i] = 0 ;
374- break ;
424+ states[i] = 0 ; // Does it get here?
425+
426+ break ;
375427 }
376428 if (positions[i] - destPositions[i] > 0 )
377429 {
0 commit comments