Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AccelStepper.h>
- #include <SoftwareSerial.h>
- #include <EEPROM.h>
- long number1, number2, address=0;
- /*
- aktualne komendy bt
- http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html
- doly|move0 #ustaw w pozycji 0
- doly|startslow #ruszaj powoli do stop / end
- doly|stop #zatrzumaj
- move|[-50 ~ 50] #przesuń wózek o x stopni
- doly|setEnd #ustaw pozycje konca trasy
- doly|set0 #ustaw pozycje początku trasy
- set|
- start|
- data|"
- */
- boolean n, debug, allow_move;
- String BluetoothData, val1, val2, ser;
- int pad_x, znak, speed_, current_pos, end_track, mov;
- SoftwareSerial mySerial(10, 11); // RX, TX
- int steps_per_rev = 4095;
- String what ;
- /*-----( Declare Constants and Pin Numbers )-----*/
- #define FULLSTEP 4
- #define HALFSTEP 8
- #define HALFSTEP2 16
- // motor pins
- #define motorPin1 4 // Blue - 28BYJ48 pin 1
- #define motorPin2 5 // Pink - 28BYJ48 pin 2
- #define motorPin3 6 // Yellow - 28BYJ48 pin 3
- #define motorPin4 7 // Orange - 28BYJ48 pin 4
- /*-----( Declare objects )-----*/
- // NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
- AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
- // #define IN1 4
- // #define IN2 5
- // #define IN3 6
- // #define IN4 7
- int Steps = 7;
- int currPos = 0;
- long time;
- void setup() {
- Serial.begin(9600);
- n = false;
- debug = !true;
- allow_move = true;
- stepper1.setMaxSpeed(1000.0);
- stepper1.setAcceleration(300.0);
- stepper1.setSpeed(700);
- //stepper1.moveTo(-2048*4); // 1 revolution
- //#####################TESTOWO
- current_pos = 0;
- end_track = EEPROMReadlong(0);
- Serial.println("goodnight moon!");
- mySerial.begin(9600);
- // mySerial.println("Hello, world?");
- mySerial.setTimeout(200);
- }
- void loop() {
- if (Serial.available() > 0) {
- Serial.println( BluetoothData);
- Serial.println( val1);
- Serial.println( end_track);
- Serial.println(stepper1.currentPosition());
- }
- BluetoothData = mySerial.readStringUntil(';');
- //int commaIndex = BluetoothData.indexOf(',');Serial.flush()
- if (BluetoothData != "") {
- String val1 = getValue(BluetoothData, '|', 0);
- String val2 = getValue(BluetoothData, '|', 1);
- mySerial.flush();
- if (debug) {
- Serial.println( BluetoothData);
- Serial.println( val1);
- Serial.println( end_track);
- Serial.println(stepper1.currentPosition());
- }
- if (val1 == "doly") {
- if (val2 == "move0") {
- stepper1.setAcceleration(300.0);
- stepper1.setSpeed(1000);
- if (stepper1.distanceToGo() == 0)
- stepper1.moveTo(0);
- //Serial.println(stepper1.currentPosition());
- // while (stepper1.currentPosition()>-1) {
- // Serial.println(stepper1.currentPosition());
- while(stepper1.run()){}
- //}
- stepper1.disableOutputs();
- Serial.println(" jestem na początku trasy");
- }
- if (val2 == "startslow") {
- stepper1.setAcceleration(300.0);
- stepper1.setSpeed(20);
- stepper1.moveTo(end_track);
- allow_move = true;
- while (allow_move) {
- if(stepper1.currentPosition() == end_track){ stepper1.disableOutputs();stepper1.stop();break;}
- check_stop();
- stepper1.run();
- }
- Serial.println(" jestem na koncu trasy");
- } /// end startslow
- if (val2 == "setEnd") {
- end_track = stepper1.currentPosition();
- EEPROMWritelong(address,stepper1.currentPosition());
- Serial.println(stepper1.currentPosition());
- }
- if (val2 == "stop") {
- allow_move = !allow_move;
- }
- if (val2 == "set0") {
- current_pos = 0;
- stepper1.setCurrentPosition(0);
- Serial.println(stepper1.currentPosition());
- }
- } // End val1 = dolly
- if (val1 == "move") {
- if (val2.toInt() < 0) {
- stepper1.move(-100);
- //stepper1.distanceToGo() != 0
- mov = stepper1.currentPosition() +100;
- stepper1.setPinsInverted(true,true,true);
- // Serial.println(stepper1.currentPosition());
- while (stepper1.currentPosition() != mov) {
- // Serial.println(stepper1.currentPosition());
- stepper1.setSpeed(1000);
- stepper1.runSpeed();
- }
- stepper1.setPinsInverted(false,false,false);
- pad_x = stepper1.currentPosition() - (2* 100);
- stepper1.setCurrentPosition(pad_x);
- stepper1.disableOutputs();
- }//End -pos
- if (val2.toInt() > 0) {
- stepper1.move(100);
- mov = stepper1.currentPosition() +100;
- while (stepper1.currentPosition() != mov) {
- stepper1.setSpeed(1000);
- stepper1.runSpeed();
- }
- stepper1.setCurrentPosition(stepper1.currentPosition());
- stepper1.disableOutputs();
- }//End +pos
- } //end of move
- //Val1 start
- if (val1 == "start") {
- if (val2 == "somevalue2") { }
- if (val2 == "set0") {
- stepper1.setAcceleration(300.0);
- stepper1.setSpeed(200);
- stepper1.moveTo(0);
- allow_move = true;
- while (allow_move) {
- if(stepper1.currentPosition() == 0){ stepper1.disableOutputs();stepper1.stop();break;}
- check_stop();
- stepper1.moveTo(0);
- stepper1.run();
- }
- } //end set0
- if (val2 == "test_ride") {
- //## test accel
- stepper1.setAcceleration(100.0);
- stepper1.setSpeed(20);
- stepper1.moveTo(end_track);
- allow_move = true;
- while (allow_move) {
- if (stepper1.currentPosition() == 0){
- stepper1.moveTo(end_track);}else if(stepper1.currentPosition() == end_track){
- stepper1.moveTo(0);}
- check_stop();
- stepper1.run();
- }
- }
- } // End val1 = start
- if (val1 == "data") {
- if (val2 == "whoareyou") { mySerial.write("slider:adam01;"); }
- if (val2 == "showsettings") { mySerial.write(""); }
- } // End val1 = start
- } // end of bluetooth data not empty
- stepper1.disableOutputs();
- BluetoothData = "";
- val1 = "";
- val2 = "";
- } // endo of loop
- void check_stop()
- {
- if (mySerial.available() > 0) {
- what = mySerial.readStringUntil(';');
- if(what == "doly|stop"){
- allow_move = false;
- stepper1.stop();
- //delay(500);
- stepper1.disableOutputs();
- //break;
- //Serial.println(what);
- }
- }
- // BluetoothData = mySerial.readStringUntil(';');
- // if (BluetoothData == "doly|stop") {
- // allow_move = !allow_move;
- // return 1 ;
- // } else {
- // allow_move = !allow_move;
- // return 0 ;
- // }
- }
- String getValue(String data, char separator, int index)
- {
- int found = 0;
- int strIndex[] = {
- 0, -1
- };
- int maxIndex = data.length() - 1;
- for (int i = 0; i <= maxIndex && found <= index; i++) {
- if (data.charAt(i) == separator || i == maxIndex) {
- found++;
- strIndex[0] = strIndex[1] + 1;
- strIndex[1] = (i == maxIndex) ? i + 1 : i;
- }
- }
- return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
- void EEPROMWritelong(int address, long value)
- {
- //Decomposition from a long to 4 bytes by using bitshift.
- //One = Most significant -> Four = Least significant byte
- byte four = (value & 0xFF);
- byte three = ((value >> 8) & 0xFF);
- byte two = ((value >> 16) & 0xFF);
- byte one = ((value >> 24) & 0xFF);
- //Write the 4 bytes into the eeprom memory.
- EEPROM.write(address, four);
- EEPROM.write(address + 1, three);
- EEPROM.write(address + 2, two);
- EEPROM.write(address + 3, one);
- }
- //This function will return a 4 byte (32bit) long from the eeprom
- //at the specified address to address + 3.
- long EEPROMReadlong(long address)
- {
- //Read the 4 bytes from the eeprom memory.
- long four = EEPROM.read(address);
- long three = EEPROM.read(address + 1);
- long two = EEPROM.read(address + 2);
- long one = EEPROM.read(address + 3);
- //Return the recomposed long by using bitshift.
- return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement