aso4200

Turntable.ino

May 10th, 2022
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.71 KB | None | 0 0
  1. //Program Turntable.ino
  2. //Made by Allan Olsen, SMJK
  3. //First release 20190616
  4. //Version 20211116
  5.  
  6. typedef unsigned char UBYTE;
  7. typedef signed char BYTE;
  8. typedef unsigned short int UWORD;
  9. typedef signed short int WORD;
  10. typedef unsigned long int ULONG;
  11. typedef signed long int LONG;
  12. typedef unsigned long TIME_T;
  13.  
  14. enum DIRECTION {NEUTRAL=0, REVERSE, FORWARD};
  15. enum STATE {OFF=0, ON};
  16.  
  17. void setup();
  18. void loop();
  19. void split_word(UWORD val, UBYTE *a, UBYTE *b);
  20. void join_word(UWORD *val, UBYTE a, UBYTE b);
  21. void save_eeprom(UWORD *v);
  22. void read_eeprom(UWORD *v);
  23. void stepper_move(UWORD steps, UBYTE dir);
  24. int chk_cancel_move();
  25. void calc_move(int new_pos, UBYTE direction);
  26. void chk_button_direction();
  27. void chk_button_move();
  28. void chk_button_halfturn();
  29. int chk_button_save_pos(int x);
  30. UBYTE reset_stepper();
  31. void alarm();
  32. void setup_routine();
  33. UBYTE calc_index(UWORD val);
  34. void calc_index_init();
  35. void Print(const char *txt);
  36. void Print(const char *txt, signed int var);
  37. void Print(const char *txt, unsigned int var);
  38. void Print(const char *txt, unsigned char var);
  39. void Println();
  40. void Println(const char *txt);
  41. void Println(const char *txt, signed int var);
  42. void Println(const char *txt, unsigned int var);
  43. void Println(const char *txt, unsigned char var);
  44. void Println(signed int var);
  45. void Println(unsigned int var);
  46. void Println(unsigned char var);
  47.  
  48. #define DEBUG  //Debug on, if you want text message on the serial terminal
  49.  
  50. #include <Stepper.h>
  51. #include <EEPROM.h>
  52.  
  53. //#define JERSLEV
  54. #define NYSKOV
  55. //#define NS_TEST
  56.  
  57. UWORD analog_track_data;
  58. bool turn_activated=false;
  59. bool direction_activated=false;
  60. bool half_turn_activated=false;
  61. bool setup_init_activated=false;
  62. bool setup_init_start=false;
  63. bool setup_save_activated=false;
  64. UBYTE new_track_pos;
  65. WORD stepper_position;
  66. UBYTE direction;
  67. UBYTE step_correction_direction=FORWARD;
  68. UWORD step_correction=0;
  69. TIME_T time_setup;
  70. bool setup_mode=false;
  71. bool motor_direction_reverse=false, motor_direction_forward=false;
  72. UBYTE stepstartspeed;
  73. TIME_T move_cancel_time;
  74. bool move_cancel=false;
  75.  
  76. #ifdef JERSLEV
  77. #include "jerslev.h"
  78. #endif
  79.  
  80. #ifdef NYSKOV
  81. #include "nyskov.h"
  82. #endif
  83.  
  84. #ifndef TRACK_NO
  85. #define TRACK_NO 5 //Default 5 tracks, if not defined in another place
  86. #endif
  87. #define MAX_V_VAL 1023 //Max value for analogRead at UNO, MEGA etc
  88. UWORD lo[TRACK_NO];
  89. UWORD hi[TRACK_NO];
  90.  
  91. void setup()
  92. {
  93. #ifdef DEBUG
  94.     Serial.begin(9600);
  95. #endif
  96.     myStepper.setSpeed(STEPPERSPEED);
  97.     calc_index_init();  
  98. #ifdef ONE_PUSH_DIRECTION
  99.     pinMode(pin_turn_direction,INPUT);
  100.     pinMode(pin_LED_clockwise,OUTPUT);
  101.     pinMode(pin_LED_counter_clockwise,OUTPUT);
  102. #endif
  103. #ifdef TWO_PUSH_DIRECTION
  104.     pinMode(pin_turn_direction_forw,INPUT);
  105.     pinMode(pin_turn_direction_backw,INPUT);
  106.     pinMode(pin_setup_mode,INPUT);
  107. #endif
  108. #ifdef DRIVING_CURRENT
  109.     pinMode(pin_dc_bit0,OUTPUT);
  110.     pinMode(pin_dc_bit1,OUTPUT);
  111.     pinMode(pin_dc_bit2,OUTPUT);
  112.     pinMode(pin_dc_bit3,OUTPUT);
  113.     pinMode(pin_dc_bit_active,OUTPUT);
  114.     digitalWrite(pin_dc_bit0,LOW); //turn off bit 1 for driving current
  115.     digitalWrite(pin_dc_bit1,LOW); //turn off bit 2 for driving current
  116.     digitalWrite(pin_dc_bit2,LOW); //turn off bit 3 for driving current
  117.     digitalWrite(pin_dc_bit3,LOW); //turn off bit 4 for driving current
  118.     digitalWrite(pin_dc_bit_active,LOW); //turn off driving current
  119. #endif
  120.     pinMode(pin_selector,INPUT);
  121.     pinMode(pin_turn_move,INPUT);
  122.     pinMode(pin_half_turn,INPUT);
  123.     pinMode(pin_zero_point,INPUT);
  124.     pinMode(pin_setup_save,INPUT);
  125.     pinMode(pin_setup_led,OUTPUT);
  126.     digitalWrite(pin_setup_led,LOW); //turn off setup LED
  127. #ifdef ONE_PUSH_DIRECTION
  128.     digitalWrite(pin_LED_clockwise,HIGH); //turn on LED FORWARD
  129.     digitalWrite(pin_LED_counter_clockwise,LOW); //turn off LED REVERSE
  130.     direction=FORWARD;
  131.     motor_direction_forward=true;
  132.     Println("Direction: FORWARD");
  133. #endif
  134. #ifdef TWO_PUSH_DIRECTION
  135.     direction=NEUTRAL;
  136.     if(digitalRead(pin_turn_direction_forw))
  137.     {
  138.         direction=FORWARD;
  139.         motor_direction_forward=true;
  140.         motor_direction_reverse=false;
  141.         Println("Direction: FORWARD");
  142.     }
  143.     if(digitalRead(pin_turn_direction_backw))
  144.     {
  145.         direction=REVERSE;
  146.         motor_direction_forward=false;
  147.         motor_direction_reverse=true;
  148.         Println("Direction: REVERSE");
  149.     }
  150.     if(direction==NEUTRAL)
  151.         Println("Direction: NEUTRAL");
  152. #endif
  153.  
  154.     new_track_pos=0;
  155.     stepper_position=0;
  156.  
  157.     setup_mode=true; //set in setup mode for full speed at init
  158.     read_eeprom(track_pos_steps);
  159.  
  160.     if(digitalRead(pin_zero_point)==0) //if sensor can't be seen at startup, try to find it ...
  161.     {
  162.         if(reset_stepper()==false) //if sensor haven't been found after one full turn
  163.         {
  164.         Println("Sensor not found! ");
  165.         alarm(); //go in alarm mode
  166.         }
  167.     }
  168.     else //but if sensor are seen at startup, we have to move a bit, until undetected
  169.     {
  170.         UWORD x=0;
  171.         do
  172.         {
  173.         stepper_move(1, REVERSE); //and sensor has to be detected forward moving, so go reverse
  174.         x++;
  175.         if(x>GearStepsPrTurn) //if max steps has been reached, something is wrong
  176.             alarm();        
  177.         }while(digitalRead(pin_zero_point)); // do until sensor report open
  178.         if(reset_stepper()==false) //after detected, we have to reset to hardware zero point
  179.         {
  180.         Println("Sensor not found! ");
  181.         alarm(); //and if sensor not found afterward, go in alarm mode
  182.         }
  183.     }
  184.     setup_mode=false; //now set mode back to normal after startup.
  185. }
  186.  
  187. void loop()
  188. {
  189. #ifdef TWO_PUSH_SETUP
  190.     if(digitalRead(pin_turn_direction)&&digitalRead(pin_turn_move))
  191. #endif
  192. #ifdef ONE_PUSH_SETUP
  193.     if(digitalRead(pin_setup_mode))
  194. #endif
  195.     {
  196.         if(setup_init_activated==false)
  197.         {
  198.         setup_init_activated=true;
  199.         setup_init_start=true;
  200.         time_setup=millis()+SETUPWAITTIME;
  201.         }
  202.         else
  203.         {
  204.         if(millis()>time_setup)
  205.         {
  206.                 setup_mode=true;
  207.                 setup_init_start=false;
  208.                 setup_routine();
  209.         }
  210.         }
  211.     }
  212.     else
  213.     {
  214.         setup_init_activated=false;
  215.     }
  216.    
  217.     chk_button_halfturn();
  218.     chk_button_direction();
  219.     chk_button_move();
  220. #ifdef DRIVING_CURRENT
  221.     chk_driving_current();
  222. #endif
  223. }
  224.  
  225. void split_word(UWORD val, UBYTE *a, UBYTE *b)
  226. {
  227.    UWORD c;
  228.    *a=val & 0xff;
  229.    c=val & 0xff00;
  230.    *b=c >> 8;
  231. }
  232.  
  233. void join_word(UWORD *val, UBYTE a, UBYTE b)
  234. {
  235.    *val=a | (b << 8);
  236. }
  237.  
  238. void save_eeprom(UWORD *v)
  239. {
  240.    UWORD addr;
  241.    UBYTE a, b;
  242.  
  243.    addr=0;
  244.    EEPROM.write(addr, MAGICFILENUMBER); //An 8 bit value saved for validating on read, that data exist
  245.    Print("Saving to EEprom address ", addr);
  246.    Println("value for Magicnumber: ",a);
  247.    
  248.    addr++;
  249.    split_word(step_correction, &a, &b);
  250.    EEPROM.write(addr, a); //Value for turntable zero from the sensor zero - low byte.
  251.    EEPROM.write(addr+1, b); //Value for turntable zero from the sensor zero - high byte..
  252.     Print("Saving corrections value ");
  253.    Print("to EEprom address ", addr);
  254.    Print("+ ", addr+1);
  255.    Print("the low + high value: ",a);
  256.    Print("+ (",b);
  257.    Println("* 256) : total: ", step_correction);
  258.  
  259.    addr+=2;
  260.     Print("Saving step corrections direction ");
  261.    Print("to EEprom address ", addr);
  262.     Println("(1=REVERSE, 2=FORWARD): ",step_correction_direction);
  263.    EEPROM.write(addr, step_correction_direction); //Value for turntable direction from sensor zero.
  264.  
  265.    addr++;
  266.    for(UBYTE x=0; x<TRACK_NO; x++)
  267.    {
  268.       split_word(v[x], &a, &b);
  269.       EEPROM.write(addr, a);
  270.       EEPROM.write(addr+1, b);
  271.         Print("For track pos ",x);
  272.       Print("saving to EEprom address ", addr);
  273.       Print("+ ", addr+1);
  274.       Print("the low + high value: ", a);
  275.       Print("+ (", b);
  276.       Println("* 256) : total: ", v[x]);
  277.       addr+=2;
  278.    }
  279. }
  280.  
  281. void read_eeprom(UWORD *v)
  282. {
  283.    UWORD addr;
  284.    UBYTE a, b;
  285.  
  286.     addr=0;
  287.    a=EEPROM.read(addr);
  288.    Print("Loading from EEprom address ", addr);
  289.    Println("value for Magicnumber: ",a);
  290.    Println("Expected value for Magicnumber: ",MAGICFILENUMBER);
  291.  
  292.    if(a==MAGICFILENUMBER) //An 8 bit value for validating that data exist and is correct
  293.    {
  294.       addr++;
  295.       a=EEPROM.read(addr); //Value for turntable zero from the sensor zero - low byte.
  296.       b=EEPROM.read(addr+1); //Value for turntable zero from the sensor zero - high byte.
  297.       join_word(&step_correction, a, b);
  298.       Print("Corrections value ");
  299.       Print("loading from EEprom address ", addr);
  300.       Print("+ ", addr+1);
  301.       Print("the low + high value: ", a);
  302.       Print("+ (",b);
  303.       Println("* 256) : total: ", step_correction);
  304.    
  305.       addr+=2;
  306.       step_correction_direction=EEPROM.read(addr); //Value for turntable direction from sensor zero.
  307.         Print("Step correction direction ");
  308.       Print("loading from EEprom address ", addr);
  309.       Println("(1=REVERSE, 2=FORWARD): ",step_correction_direction);
  310.    
  311.       addr++;
  312.       for(UBYTE x=0; x<TRACK_NO; x++)
  313.       {
  314.          a=EEPROM.read(addr);
  315.          b=EEPROM.read(addr+1);
  316.             join_word(&v[x], a, b);
  317.             Print("For track pos ",x);
  318.          Print("loading from EEprom address ", addr);
  319.          Print("+ ", addr+1);
  320.          Print("the low + high value: ", a);
  321.          Print("+ (", b);
  322.          Println("* 256) : total: ", v[x]);
  323.          addr+=2;
  324.       }
  325.    }
  326.    else
  327.    {
  328.       return;  //If magic number compare fails, return and use default values in array of data
  329.    }
  330. }
  331.  
  332. void stepper_move(UWORD steps, UBYTE dir)
  333. {
  334.    stepstartspeed=STEPPERSTARTSP;
  335.  
  336.    if(steps==0) return;
  337.    if(dir==NEUTRAL) return;
  338.  
  339.    if(setup_mode) myStepper.setSpeed(STEPPERSPEED);
  340.  
  341. #ifdef DRIVING_CURRENT
  342.    driving_current(OFF);
  343. #endif
  344.  
  345.    if(dir==FORWARD&&motor_direction_forward==false)
  346.    {
  347.       motor_direction_forward=true;
  348.       motor_direction_reverse=false;
  349.       myStepper.setSpeed(STEPPERSPEED);
  350.       for(UWORD x=0; x<GEAR_BLUR; x++) //Cheap steppers can have blur in gear when shift from on direction to the other direction
  351.       {
  352.          myStepper.step(1);
  353.       }
  354.    }
  355.    if(dir==REVERSE&&motor_direction_reverse==false)
  356.    {
  357.       motor_direction_reverse=true;
  358.       motor_direction_forward=false;
  359.       for(UWORD x=0; x<GEAR_BLUR; x++) //Cheap steppers can have blur in gear when shift from on direction to the other direction
  360.       {
  361.          myStepper.step(-1);
  362.       }
  363.    }
  364.  
  365.    if(dir==FORWARD)
  366.    {
  367.       for(UWORD x=0; x<steps; x++)
  368.       {
  369.          if(setup_mode==false)
  370.          {
  371.             if(stepstartspeed < STEPPERSPEED)
  372.             {
  373.                stepstartspeed=stepstartspeed + STEP_UP_VAL;
  374.                myStepper.setSpeed(stepstartspeed);
  375.             }
  376.          }
  377.          if(chk_cancel_move())
  378.             return; //Cancel move, so we return and skip moving
  379.          myStepper.step(1);
  380.          stepper_position++;
  381.          if(stepper_position==GearStepsPrTurn) //we have a roll over
  382.             stepper_position=0;  //so set to zero
  383.       }
  384.    }
  385.    if(dir==REVERSE)
  386.    {
  387.       for(UWORD x=0; x<steps; x++)
  388.       {
  389.          if(setup_mode==false)
  390.          {
  391.             if(stepstartspeed < STEPPERSPEED)
  392.             {
  393.                stepstartspeed=stepstartspeed + STEP_UP_VAL;
  394.                myStepper.setSpeed(stepstartspeed);
  395.             }
  396.          }
  397.          if(chk_cancel_move())
  398.             return; //Cancel move, so we return and skip moving
  399.          myStepper.step(-1);
  400.          stepper_position--;
  401.          if(stepper_position==-1)
  402.             stepper_position=(GearStepsPrTurn - 1);
  403.       }
  404.    }
  405. #ifdef DRIVING_CURRENT
  406.    if(direction&&setup_mode==false)
  407.       driving_current(ON);
  408. #endif
  409. }
  410.  
  411. int chk_cancel_move()
  412. {
  413.    if(digitalRead(pin_turn_move))
  414.    {
  415.       if(move_cancel==false)
  416.       {
  417.          move_cancel=true;
  418.          move_cancel_time=millis()+CANCELMOVETIME; //Wait/hold botton this amount of millis to cancel move
  419.       }
  420.       else
  421.       {
  422.          if(millis()>move_cancel_time) //And when the time is up
  423.          {
  424.             return true;   //return true to stop steppper move
  425.          }
  426.       }
  427.    }
  428.    else
  429.    {
  430.       if(move_cancel==true)
  431.          move_cancel=false;
  432.    }
  433.    return false;
  434. }
  435.  
  436. void calc_move(int new_pos, UBYTE direction)
  437. {
  438.    int count;
  439.  
  440.    if(direction==FORWARD) //if rotating is forward (clockwise)
  441.    {
  442.       if(stepper_position>track_pos_steps[new_pos]) //then if stepper pos is greater than new track pos
  443.          count=(GearStepsPrTurn-stepper_position)+track_pos_steps[new_pos]; //the count is max step minus stepper pos plus new track pos
  444.       else
  445.          count=track_pos_steps[new_pos]-stepper_position; //else count is new track pos minus stepper pos
  446.    }
  447.    if(direction==REVERSE) //if rotating is backward (counter-clockwise)
  448.    {
  449.       if(stepper_position<track_pos_steps[new_pos]) //then if stepper pos is lesser than new track pos
  450.          count=(GearStepsPrTurn-track_pos_steps[new_pos])+stepper_position; //the count is max step minus new track pos plus stepper pos
  451.       else
  452.          count=stepper_position-track_pos_steps[new_pos]; //else count is stepper pos minus new track pos
  453.    }
  454.    if(count >= (GearStepsPrTurn/2)) //if count is greater or equal to max step diveded by 2
  455.     count=count-(GearStepsPrTurn/2); //then count is cut down with half the steps max, thereby take the shortest turn
  456.  
  457.    Println("New track pos ", new_pos);
  458.    Println("Step count ", count);
  459.    Println("Stepper pos before ", stepper_position);
  460.    stepper_move(count, direction);
  461.    Println("Stepper pos after ", stepper_position);
  462.    Println();
  463. }
  464.  
  465. #ifdef TWO_PUSH_DIRECTION
  466. void chk_button_direction()
  467. {
  468.    UBYTE t=false, u=false;
  469.    if(setup_init_start)
  470.       return; //We try to come in prog mode, return
  471.  
  472.    if(digitalRead(pin_turn_direction_forw))
  473.    {
  474.       if(direction_activated==false)
  475.       {
  476.          direction_activated=true;
  477.          if(direction==NEUTRAL)
  478.          {
  479.             direction=FORWARD;
  480.             Println("Direction: FORWARD");
  481.          }
  482.       }
  483.    }
  484.    else
  485.       t=true;
  486.  
  487.    if(digitalRead(pin_turn_direction_backw))
  488.    {
  489.       if(direction_activated==false)
  490.       {
  491.          direction_activated=true;
  492.          if(direction==NEUTRAL)
  493.          {
  494.             direction=REVERSE;
  495.             Println("Direction: REVERSE");
  496.          }
  497.       }
  498.    }
  499.    else
  500.       u=true;
  501.  
  502.    if(t&&u)
  503.    {
  504.       if(direction_activated)
  505.       {
  506.          direction_activated=false;
  507.          if(direction)
  508.          {
  509.             direction=NEUTRAL;
  510.             Println("Direction: NEUTRAL");
  511.          }
  512.       }
  513.    }
  514. }
  515. #endif
  516.  
  517. #ifdef ONE_PUSH_DIRECTION
  518. void chk_button_direction()
  519. {
  520.    if(setup_init_start)
  521.       return; //We try to come in prog mode, return
  522.    if(digitalRead(pin_turn_direction))
  523.    {
  524.       if(direction_activated==false)
  525.       {
  526.          direction_activated=true;
  527.          if(direction==FORWARD)
  528.          {
  529.             direction=REVERSE;
  530.             digitalWrite(pin_LED_counter_clockwise, HIGH);
  531.             digitalWrite(pin_LED_clockwise, LOW);
  532.             Println("Direction: REVERSE");
  533.          }
  534.          else
  535.          {
  536.             direction=FORWARD;
  537.             digitalWrite(pin_LED_clockwise, HIGH);
  538.             digitalWrite(pin_LED_counter_clockwise, LOW);
  539.             Println("Direction: FORWARD");
  540.          }
  541.       }
  542.    }
  543.    else
  544.    {
  545.       direction_activated=false;
  546.    }
  547. }
  548. #endif
  549.  
  550. void chk_button_move()
  551. {
  552.    if(setup_init_start)
  553.       return; //We try to come in prog mode, return
  554.    
  555.    if(digitalRead(pin_turn_move))
  556.    {
  557.       if(turn_activated==false)
  558.       {
  559.          turn_activated=true;
  560.          if(setup_mode)
  561.          {
  562.             stepper_move(1, direction);
  563.          }
  564.          else
  565.          {
  566.             if(direction != NEUTRAL)
  567.             {
  568.                analog_track_data=analogRead(pin_selector);
  569.                new_track_pos=calc_index(analog_track_data);
  570.                calc_move(new_track_pos, direction);
  571.             }
  572.             else
  573.                Println("Direction neutral, can't move ");
  574.          }
  575.       }
  576.    }
  577.    else
  578.    {
  579.       if(turn_activated==true)
  580.       {
  581.          turn_activated=false;
  582.          move_cancel=false;
  583.       }
  584.    }
  585. }
  586.  
  587. void chk_button_halfturn()
  588. {
  589.    if(digitalRead(pin_half_turn))
  590.    {
  591.       if(half_turn_activated==false)
  592.       {
  593.          half_turn_activated=true;
  594.          if(setup_mode)
  595.          {
  596.             stepper_move(HIGHSTEP, direction);
  597.          }
  598.          else
  599.          {
  600.             Println("Turn 180 degrees");
  601.             stepper_move(GearStepsPrTurn / 2, direction);
  602.             Print("Stepper pos ", stepper_position);
  603.             Println();
  604.          }
  605.       }
  606.    }
  607.    else
  608.    {
  609.       half_turn_activated=false;
  610.    }
  611. }
  612.  
  613. int chk_button_save_pos(int x)
  614. {
  615. #ifdef PUSH_NORMALY_CLOSED
  616.    if(digitalRead(pin_setup_save)==false) //If button is a NC
  617. #endif
  618. #ifdef PUSH_NORMALY_OPEN
  619.    if(digitalRead(pin_setup_save)) //If button is a NO
  620. #endif
  621.    {
  622.       if(setup_save_activated==false)
  623.       {
  624.          setup_save_activated=true;
  625.          track_pos_steps[x]=stepper_position;
  626.          Println("Track steps saved for ", x);
  627.          Println("Step pos ", track_pos_steps[x]);
  628.          return false;
  629.       }
  630.    }
  631.    else
  632.    {
  633.       setup_save_activated=false;
  634.    }
  635.    return true;
  636. }
  637.  
  638. UBYTE read_save_pos_correction(UWORD x)
  639. {
  640. #ifdef PUSH_NORMALY_CLOSED
  641.    if(digitalRead(pin_setup_save)==false) //If button is a NC
  642. #endif
  643. #ifdef PUSH_NORMALY_OPEN
  644.    if(digitalRead(pin_setup_save)) //If button is a NO
  645. #endif
  646.    {
  647.       if(setup_save_activated==false)
  648.       {
  649.          setup_save_activated=true;
  650.          step_correction=x;
  651.          step_correction_direction=direction;
  652.          Println("Step pos correction ", x  );
  653.          Println("Stepper position ", stepper_position);
  654.          return false;
  655.       }
  656.    }
  657.    else
  658.    {
  659.       setup_save_activated=false;
  660.    }
  661.    return true;
  662. }
  663.  
  664. UBYTE reset_stepper()
  665. {
  666.    UBYTE tmp_direction;
  667.    myStepper.setSpeed(STEPPERSPEED);
  668.    bool found=false;
  669.    UWORD tmp_track_select;
  670.    tmp_direction=direction;
  671.    direction=FORWARD;
  672. #ifdef DRIVING_CURRENT
  673.    driving_current(OFF);
  674. #endif
  675.  
  676.     Println("Trying to find sensor ... ");
  677.    for(UWORD x=0; x<GearStepsPrTurn; x++)
  678.    {
  679.       stepper_move(1, direction);
  680.       if(digitalRead(pin_zero_point)) //Found sensor zero point
  681.       {
  682.          Println("Found it ");
  683.          found=true;
  684.          if(step_correction)
  685.          {
  686.             Println("Now sets the stepper correction ");
  687.             stepper_move(step_correction, step_correction_direction);  
  688.          }
  689.          stepper_position=0; //after step correction set, reset stepper position
  690.          break;
  691.       }
  692.    }
  693.    if(found)
  694.    {
  695.       tmp_track_select=analogRead(pin_selector);
  696.       new_track_pos=calc_index(tmp_track_select);
  697.       calc_move(new_track_pos, direction);
  698.       Println("New track pos after startup ", new_track_pos);
  699.       Println("Stepper pos after startup ", stepper_position);
  700.    }
  701.    direction=tmp_direction;
  702.    return (found);
  703. }
  704.  
  705. void alarm()
  706. {
  707.     Println("Shutting down ... ");
  708.    while (1)
  709.    {
  710. #ifdef TWO_LED_DIRECTION
  711.       digitalWrite(pin_LED_clockwise, HIGH);
  712.       digitalWrite(pin_LED_counter_clockwise, LOW);
  713.       delay(300);
  714.       digitalWrite(pin_LED_counter_clockwise, HIGH);
  715.       digitalWrite(pin_LED_clockwise, LOW);
  716.       delay(300);
  717. #else
  718.       digitalWrite(pin_setup_led, HIGH);
  719.       delay(300);
  720.       digitalWrite(pin_setup_led, LOW);
  721.       delay(300);
  722.  
  723. #endif
  724.    }
  725. }
  726.  
  727. void setup_routine()
  728. {
  729.    UBYTE y;
  730.  
  731.    Println("Now in programming mode ");
  732.    digitalWrite(pin_setup_led, HIGH);
  733.  
  734.    Println("Looking after the zero pos sensor ... ");
  735.    if(digitalRead(pin_zero_point)) //sensor detectet
  736.    {
  737.       UWORD x=0;
  738.       do
  739.       {
  740.          stepper_move(1, REVERSE);
  741.          x++;
  742.          if(x > GearStepsPrTurn)
  743.             alarm(); //If sensor reading dosn't break before max steps, set alarm
  744.       }while (digitalRead(pin_zero_point)); //move backward until sensor reading break the signal
  745.    }
  746.  
  747.    bool found=false;
  748.    for(int x=0; x<GearStepsPrTurn; x++)
  749.    {
  750.       if(digitalRead(pin_zero_point)) //we found sensor zero point
  751.       {
  752.          stepper_position=0; //so reset stepper value
  753.          found=true;
  754.          break;
  755.       }
  756.       else
  757.       {
  758.          stepper_move(1, FORWARD);
  759.       }
  760.    }
  761.    if(found)
  762.    {
  763.       Println("Zero sensor found ");
  764.       Println("Set turntable zero compared to sensor zero");
  765.    }
  766.    else
  767.       alarm();
  768.    y=0;
  769.    do //set and save turntable zero point from sensor zero point
  770.    {
  771.       chk_button_halfturn(); //step 10 steps per push
  772.       chk_button_direction();  //direction change
  773.       chk_button_move();   //step 1 step per push
  774.       y=read_save_pos_correction(stepper_position);    //save value of stepper_position in step_correction
  775.    }while (y);
  776.    Println("Zero point saved ");
  777.    stepper_position=0; //and reset stepper_position
  778.  
  779.    y=0;
  780.  
  781.    for(int x=0; x<TRACK_NO; x++)
  782.    {
  783.       Println("Now set steps for track ",x);
  784.       do
  785.       {
  786.          chk_button_halfturn(); //step 10 steps per push
  787.          chk_button_direction(); //direction change
  788.          chk_button_move();   //step 1 step per push
  789.          y=chk_button_save_pos(x); //save value of stepper_position in track pos
  790.       }while (y);                           //Continue loop until y gets false
  791.       Print("Steps saved for track ", x);
  792.       Println(" value for steps ",track_pos_steps[x] );
  793.    }
  794.  
  795.    save_eeprom(track_pos_steps); //now save all values in eeprom
  796.  
  797.    setup_mode=true; //set in setup mode to have full speed in setup_mode
  798.    read_eeprom(track_pos_steps);
  799.    if(digitalRead(pin_zero_point)==0) //if sensor can't be seen at startup, then find sensor pos by turning turntable
  800.    {
  801.       if(reset_stepper()==false) //and if sensor hasn't been found after one full turn
  802.       {
  803.          Println("Sensor not found! ");
  804.          alarm(); //go in alarm mode
  805.       }
  806.    }
  807.    else //but if sensor is active at startup, make a reverse move until no longer active
  808.    {
  809.       UWORD x=0;
  810.       do
  811.       {
  812.          stepper_move(1, REVERSE);
  813.          x++;
  814.          if(x>GearStepsPrTurn)
  815.             alarm();  
  816.       }while(digitalRead(pin_zero_point));
  817.       if(reset_stepper()==false) //and if sensor hasn't been found after one full turn
  818.       {
  819.          Println("Sensor not found! ");
  820.          alarm(); //go in alarm mode
  821.       }
  822.    }
  823.  
  824.    Println("Now out of programming mode");
  825.    digitalWrite(pin_setup_led, LOW);
  826.    setup_mode=false;
  827. }
  828.  
  829. UBYTE calc_index(UWORD val)
  830. {
  831.     UBYTE x,y=0;
  832.     for(x=0; x<TRACK_NO; x++)
  833.     {
  834.         if (val >= lo[x]&&val <= hi[x])
  835.         {
  836.         y=x;
  837.         break;
  838.         }
  839.     }
  840.     return (y);
  841. }
  842.  
  843. void calc_index_init()
  844. {
  845.     UBYTE x;
  846.     UBYTE max_index=TRACK_NO - 1; //number of track index minus one
  847.     UWORD offset_val=(MAX_V_VAL / max_index) / 2; //value in fluctuation from center value
  848.     UWORD range_val=MAX_V_VAL / max_index; //size a range covers
  849.  
  850.     lo[0]=0;
  851.     hi[0]=offset_val;
  852.  
  853. #ifdef DEBUG
  854.     Print("Max Index ", max_index);
  855.     Print(" - Offset Val ", offset_val);
  856.     Println(" - Range Val ", range_val);
  857. #endif
  858.  
  859.     for(x=1; x <= max_index; x++)
  860.     {
  861.         lo[x]=hi[x - 1] + 1;
  862.         hi[x]=lo[x] + range_val;
  863.     }
  864.     if (hi[max_index] > MAX_V_VAL) hi[max_index]=MAX_V_VAL;
  865.  
  866. #ifdef DEBUG
  867.     for(x=0; x<TRACK_NO; x++)
  868.     {
  869.         Print("Index ", x);
  870.         Print("low and high value: ", lo[x]);
  871.         Println("- ", hi[x]);
  872.     }
  873. #endif
  874. }
  875.  
  876. void Print(const char *txt)
  877. {
  878.    #ifdef DEBUG
  879.    String text=txt;
  880.    Serial.print(text);
  881.    #else
  882.    return;
  883.     #endif
  884. }
  885.  
  886. void Print(const char *txt, signed int var)
  887. {
  888.     #ifdef DEBUG
  889.     String text=txt;
  890.     Serial.print(text);
  891.     Serial.print(var);
  892.     Serial.print(" ");
  893.     #else
  894.     return;
  895.     #endif
  896. }
  897.  
  898. void Print(const char *txt, unsigned int var)
  899. {
  900.     Print(txt, (signed int) var);
  901. }
  902.  
  903. void Print(const char *txt, unsigned char var)
  904. {
  905.     Print(txt, (signed int) var);
  906. }
  907.  
  908. void Println()
  909. {
  910.     #ifdef DEBUG
  911.     Serial.println();
  912.     Serial.println();
  913.     #else
  914.     return;
  915.     #endif
  916. }
  917.  
  918. void Println(const char *txt)
  919. {
  920.     #ifdef DEBUG
  921.     String text=txt;
  922.     Serial.println(text);
  923.     #else
  924.     return;
  925.     #endif
  926. }
  927.  
  928. void Println(const char *txt, signed int var)
  929. {
  930.     #ifdef DEBUG
  931.     String text=txt;
  932.     Serial.print(text);
  933.     Serial.println(var);
  934.     #else
  935.     return;
  936.     #endif
  937. }
  938.  
  939. void Println(const char *txt, unsigned int var)
  940. {
  941.     Println(txt, (signed int) var);
  942. }
  943.  
  944. void Println(const char *txt, unsigned char var)
  945. {
  946.     Println(txt, (signed int) var);
  947. }
  948.  
  949. void Println(signed int var)
  950. {
  951.     #ifdef DEBUG
  952.     Serial.println(var);
  953.     #else
  954.     return;
  955.     #endif
  956. }
  957.  
  958. void Println(unsigned int var)
  959. {
  960.     Println((signed int) var);
  961. }
  962.  
  963. void Println(unsigned char var)
  964. {
  965.     Println((signed int) var);
  966. }
Advertisement
Add Comment
Please, Sign In to add comment