Advertisement
dioxik

berdel na kiełkach

Mar 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.25 KB | None | 0 0
  1. #include <AccelStepper.h>
  2. #include <SoftwareSerial.h>
  3. #include <EEPROM.h>
  4.  
  5. long number1, number2, address=0;
  6. /*
  7. aktualne komendy bt
  8. http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html
  9. doly|move0       #ustaw w pozycji 0
  10. doly|startslow   #ruszaj powoli do  stop / end
  11. doly|stop        #zatrzumaj
  12. move|[-50 ~ 50]  #przesuń wózek o x stopni
  13. doly|setEnd      #ustaw pozycje konca trasy
  14. doly|set0        #ustaw pozycje początku trasy
  15. set|
  16. start|
  17. data|"
  18. */
  19. boolean n, debug, allow_move;
  20. String BluetoothData, val1, val2, ser;
  21. int pad_x, znak, speed_, current_pos, end_track, mov;
  22. SoftwareSerial mySerial(10, 11); // RX, TX
  23. int steps_per_rev = 4095;
  24. String what ;
  25. /*-----( Declare Constants and Pin Numbers )-----*/
  26. #define FULLSTEP 4
  27. #define HALFSTEP 8
  28. #define HALFSTEP2 16
  29. // motor pins
  30. #define motorPin1  4     // Blue   - 28BYJ48 pin 1
  31. #define motorPin2  5     // Pink   - 28BYJ48 pin 2
  32. #define motorPin3  6     // Yellow - 28BYJ48 pin 3
  33. #define motorPin4  7     // Orange - 28BYJ48 pin 4
  34.  
  35. /*-----( Declare objects )-----*/
  36. // NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
  37. AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
  38.  
  39.  
  40.  
  41. // #define IN1  4
  42. // #define IN2  5
  43. // #define IN3  6
  44. // #define IN4  7
  45. int Steps = 7;
  46. int currPos = 0;
  47. long time;
  48.  
  49.  
  50. void setup() {
  51.   Serial.begin(9600);
  52.   n = false;
  53.   debug = !true;
  54.   allow_move = true;
  55.  
  56.   stepper1.setMaxSpeed(1000.0);
  57.   stepper1.setAcceleration(300.0);
  58.   stepper1.setSpeed(700);
  59.   //stepper1.moveTo(-2048*4);  // 1 revolution
  60.   //#####################TESTOWO
  61.   current_pos = 0;
  62.   end_track = EEPROMReadlong(0);
  63.  
  64.   Serial.println("goodnight moon!");
  65.  
  66.   mySerial.begin(9600);
  67.  // mySerial.println("Hello, world?");
  68.   mySerial.setTimeout(200);
  69. }
  70.  
  71. void loop() {
  72.  
  73.  
  74.  
  75.  
  76.   if (Serial.available() > 0) {
  77.     Serial.println( BluetoothData);
  78.     Serial.println( val1);
  79.     Serial.println( end_track);
  80.     Serial.println(stepper1.currentPosition());
  81.   }
  82.  
  83.   BluetoothData = mySerial.readStringUntil(';');
  84.   //int commaIndex = BluetoothData.indexOf(',');Serial.flush()
  85.   if (BluetoothData != "") {
  86.     String val1 = getValue(BluetoothData, '|', 0);
  87.     String val2 = getValue(BluetoothData, '|', 1);
  88. mySerial.flush();
  89.     if (debug) {
  90.       Serial.println( BluetoothData);
  91.       Serial.println( val1);
  92.       Serial.println( end_track);
  93.       Serial.println(stepper1.currentPosition());
  94.     }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.     if (val1 == "doly") {
  106.  
  107.       if (val2 == "move0") {
  108.         stepper1.setAcceleration(300.0);
  109.         stepper1.setSpeed(1000);
  110.         if (stepper1.distanceToGo() == 0)
  111.         stepper1.moveTo(0);
  112.         //Serial.println(stepper1.currentPosition());
  113.        // while (stepper1.currentPosition()>-1) {
  114.        // Serial.println(stepper1.currentPosition());
  115.        while(stepper1.run()){}
  116.       //}
  117.  
  118.       stepper1.disableOutputs();
  119.       Serial.println(" jestem na początku trasy");
  120.     }
  121.  
  122.  
  123.     if (val2 == "startslow") {
  124.  
  125.      
  126.     stepper1.setAcceleration(300.0);
  127.     stepper1.setSpeed(20);
  128.     stepper1.moveTo(end_track);
  129.     allow_move = true;
  130.     while (allow_move) {
  131.       if(stepper1.currentPosition() == end_track){      stepper1.disableOutputs();stepper1.stop();break;}
  132.      check_stop();
  133.      stepper1.run();
  134.       }
  135.          Serial.println(" jestem na koncu trasy");
  136.  
  137.  
  138.       } /// end startslow
  139.  
  140.  
  141.  
  142.       if (val2 == "setEnd") {
  143.         end_track = stepper1.currentPosition();
  144.         EEPROMWritelong(address,stepper1.currentPosition());
  145.         Serial.println(stepper1.currentPosition());
  146.       }
  147.       if (val2 == "stop")  {
  148.         allow_move = !allow_move;
  149.       }
  150.       if (val2 == "set0")  {
  151.         current_pos = 0;
  152.         stepper1.setCurrentPosition(0);
  153.         Serial.println(stepper1.currentPosition());
  154.        
  155.       }
  156.  
  157.     } // End val1 = dolly
  158.  
  159.     if (val1 == "move") {
  160.       if (val2.toInt() < 0) {
  161.         stepper1.move(-100);
  162.         //stepper1.distanceToGo() != 0
  163.         mov = stepper1.currentPosition() +100;
  164.         stepper1.setPinsInverted(true,true,true);
  165.  
  166.          // Serial.println(stepper1.currentPosition());
  167.          while (stepper1.currentPosition() != mov) {
  168.          // Serial.println(stepper1.currentPosition());
  169.          stepper1.setSpeed(1000);
  170.          stepper1.runSpeed();
  171.        }
  172.        stepper1.setPinsInverted(false,false,false);
  173.        pad_x = stepper1.currentPosition() - (2* 100);
  174.        stepper1.setCurrentPosition(pad_x);
  175.        stepper1.disableOutputs();
  176.  
  177.       }//End -pos
  178.  
  179.       if (val2.toInt() > 0) {
  180.  
  181.         stepper1.move(100);
  182.         mov = stepper1.currentPosition() +100;
  183.         while (stepper1.currentPosition() != mov) {
  184.           stepper1.setSpeed(1000);
  185.           stepper1.runSpeed();
  186.         }
  187.         stepper1.setCurrentPosition(stepper1.currentPosition());
  188.         stepper1.disableOutputs();
  189.  
  190.  
  191.       }//End +pos
  192.  
  193.     }  //end of move
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. //Val1 start
  202.  
  203. if (val1 == "start") {
  204.  
  205.   if (val2 == "somevalue2")  {      }
  206.  
  207.   if (val2 == "set0")  {  
  208.     stepper1.setAcceleration(300.0);
  209.     stepper1.setSpeed(200);
  210.     stepper1.moveTo(0);  
  211.     allow_move = true;
  212.     while (allow_move) {
  213.       if(stepper1.currentPosition() == 0){      stepper1.disableOutputs();stepper1.stop();break;}
  214.      check_stop();
  215.      stepper1.moveTo(0);
  216.      stepper1.run();
  217.       }
  218.  
  219.  } //end set0
  220.  
  221.  
  222.  if (val2 == "test_ride")  {    
  223.       //## test accel
  224.  
  225.  
  226.  
  227.       stepper1.setAcceleration(100.0);
  228.       stepper1.setSpeed(20);
  229.       stepper1.moveTo(end_track);
  230.       allow_move = true;
  231.       while (allow_move) {
  232.         if (stepper1.currentPosition() == 0){
  233.           stepper1.moveTo(end_track);}else if(stepper1.currentPosition() == end_track){
  234.             stepper1.moveTo(0);}
  235.             check_stop();
  236.  
  237.             stepper1.run();
  238.           }
  239.  
  240.  }
  241.  
  242.     } // End val1 = start
  243.  
  244.  
  245.  
  246.     if (val1 == "data") {
  247.  
  248.       if (val2 == "whoareyou")  { mySerial.write("slider:adam01;");     }
  249.       if (val2 == "showsettings")  { mySerial.write("");     }
  250.  
  251.  
  252.     } // End val1 = start
  253.  
  254.  
  255.  
  256.   } // end of bluetooth data not empty
  257.  
  258.  
  259. stepper1.disableOutputs();
  260.   BluetoothData = "";
  261.   val1 = "";
  262.   val2 = "";
  263. } // endo of loop
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. void check_stop()
  271. {
  272.  
  273.   if (mySerial.available() > 0) {
  274.     what = mySerial.readStringUntil(';');
  275.     if(what == "doly|stop"){
  276.        allow_move = false;    
  277.       stepper1.stop();
  278. //delay(500);
  279. stepper1.disableOutputs();
  280.       //break;
  281. //Serial.println(what);
  282.     }
  283.  
  284.   }
  285.   // BluetoothData = mySerial.readStringUntil(';');
  286.   // if (BluetoothData == "doly|stop") {
  287.   //   allow_move = !allow_move;
  288.   //   return  1 ;
  289.  
  290.   // } else {
  291.   //   allow_move = !allow_move;
  292.   //   return  0 ;
  293.   // }
  294.  
  295. }
  296.  
  297. String getValue(String data, char separator, int index)
  298. {
  299.   int found = 0;
  300.   int strIndex[] = {
  301.     0, -1
  302.   };
  303.   int maxIndex = data.length() - 1;
  304.   for (int i = 0; i <= maxIndex && found <= index; i++) {
  305.     if (data.charAt(i) == separator || i == maxIndex) {
  306.       found++;
  307.       strIndex[0] = strIndex[1] + 1;
  308.       strIndex[1] = (i == maxIndex) ? i + 1 : i;
  309.     }
  310.   }
  311.   return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
  312. }
  313.  
  314. void EEPROMWritelong(int address, long value)
  315. {
  316.       //Decomposition from a long to 4 bytes by using bitshift.
  317.       //One = Most significant -> Four = Least significant byte
  318.       byte four = (value & 0xFF);
  319.       byte three = ((value >> 8) & 0xFF);
  320.       byte two = ((value >> 16) & 0xFF);
  321.       byte one = ((value >> 24) & 0xFF);
  322.  
  323.       //Write the 4 bytes into the eeprom memory.
  324.       EEPROM.write(address, four);
  325.       EEPROM.write(address + 1, three);
  326.       EEPROM.write(address + 2, two);
  327.       EEPROM.write(address + 3, one);
  328.     }
  329.  
  330. //This function will return a 4 byte (32bit) long from the eeprom
  331. //at the specified address to address + 3.
  332. long EEPROMReadlong(long address)
  333. {
  334.       //Read the 4 bytes from the eeprom memory.
  335.       long four = EEPROM.read(address);
  336.       long three = EEPROM.read(address + 1);
  337.       long two = EEPROM.read(address + 2);
  338.       long one = EEPROM.read(address + 3);
  339.  
  340.       //Return the recomposed long by using bitshift.
  341.       return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
  342.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement