Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 4D robotarm
- // v 0.1 Beta- Nano Poti
- #include <EEPROM.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <Servo.h>
- #include <Rotary.h>
- Servo servo1; // create servo object to control a servo
- Servo servo2;
- Servo servo3;
- Servo servo4;
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
- Rotary r = Rotary(2, 3); // define rotary encoder and pins
- #define Gomb 5 // Nyomógomb
- #define RGomb 4 // Enkóder gombja
- unsigned char result;
- uint8_t sbc=0, motor; // sbc - serial buffer counter
- uint8_t sbuffer[100]; // serial buffer
- long counter=0, count1=1;
- uint8_t M1dest=90, M2dest=90, M3dest=90, M4dest=90; // destination position
- uint8_t M1pos=90, M2pos=90, M3pos=90, M4pos=90; // actual pos
- uint8_t value, stepcounter=0, stepcounter1=0, maxstepcounter=0;
- bool RUN=false; // RUN status
- uint8_t steps[5][100] = {};
- uint8_t servoMM[5][2]={};
- byte value1, value2, value3, value4;
- byte sp=2; // Speed, 1-Slow, 2-Normal, 3-Fast
- String sps="Normal";
- bool menu=false;
- byte menupos=0;
- const char string_0[] PROGMEM = " Continue ";
- const char string_1[] PROGMEM = " Reset ";
- const char string_2[] PROGMEM = " Stop ";
- const char string_3[] PROGMEM = " RUN Manual ";
- const char string_4[] PROGMEM = " RUN Auto ";
- const char string_5[] PROGMEM = " Slow ";
- const char string_6[] PROGMEM = " Normal ";
- const char string_7[] PROGMEM = " Fast ";
- const char *const stable[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5, string_6, string_7};
- char buffer[17];
- void setup() {
- pinMode(Gomb,INPUT_PULLUP);
- pinMode(RGomb,INPUT_PULLUP);
- Serial.begin(115200);
- r.begin(true);
- servo1.attach(6);
- servo2.attach(11);
- servo3.attach(10);
- servo4.attach(9);
- lcd.begin(16,2); // lcd init
- servoMM[1][0]=EEPROM.read(1); // servo1 min
- servoMM[1][1]=EEPROM.read(2); // servo1 max
- servoMM[2][0]=EEPROM.read(3);
- servoMM[2][1]=EEPROM.read(4);
- servoMM[3][0]=EEPROM.read(5);
- servoMM[3][1]=EEPROM.read(6);
- servoMM[4][0]=EEPROM.read(7);
- servoMM[4][1]=EEPROM.read(8);
- if (digitalRead(Gomb)==LOW) {
- lcd.setCursor(0,0);
- lcd.print("Setting ");
- minMaxSet();
- }
- count1=1;
- if (sp==1) count1=20;
- if (sp==2) count1=10;
- kiir1();
- }
- void kiir1() {
- lcd.clear();
- lcd.print("Manual position");
- lcd.setCursor(0,1);
- lcd.print("Step: ");
- lcd.setCursor(6,1);
- lcd.print(stepcounter+1);
- sps="Normal";
- if (sp==1) sps="Slow ";
- if (sp==3) sps="Fast ";
- lcd.setCursor(10,1);
- lcd.print(sps);
- }
- void menukiir() {
- //long asd;
- lcd.clear();
- lcd.print(" Menu ");
- lcd.setCursor(0,1);
- strcpy_P(buffer, (char *)pgm_read_word(&(stable[menupos])));
- lcd.print(buffer);
- }
- void loop() {
- result = r.process();
- if (result == DIR_CW or result == DIR_CCW) { // enkoder mozgás, lcd LED bekapcsolása
- //lcd.noBacklight();
- }
- if (result==DIR_CW and RUN==false and menu==true) {
- menupos++;
- if (menupos>=8) menupos=7;
- menukiir();
- }
- if (result==DIR_CCW and RUN==false and menu==true) {
- menupos--;
- if (menupos==255) menupos=0;
- menukiir();
- }
- if (digitalRead(Gomb)==LOW and menu==false) { // Save position
- steps[1][stepcounter] = M1dest;
- steps[2][stepcounter] = M2dest;
- steps[3][stepcounter] = M3dest;
- steps[4][stepcounter] = M4dest;
- stepcounter++;
- maxstepcounter=stepcounter;
- if (stepcounter>=99) stepcounter=98;
- kiir1();
- delay(500);
- }
- if (digitalRead(RGomb)==LOW and menu==false) {
- RUN=false;
- menu=true;
- menukiir();
- }
- if (RUN==false) {
- M1dest=map(analogRead(A0),0,1024,servoMM[1][0],servoMM[1][1]);
- M2dest=map(analogRead(A1),0,1024,servoMM[2][0],servoMM[2][1]);
- M3dest=map(analogRead(A2),0,1024,servoMM[3][0],servoMM[3][1]);
- M4dest=map(analogRead(A3),0,1024,servoMM[4][0],servoMM[4][1]);
- }
- counter++;
- if (counter>=count1) {
- counter=0;
- if (M1dest>M1pos) {
- M1pos++;
- servo1.write(M1pos);
- }
- if (M1dest<M1pos) {
- M1pos--;
- servo1.write(M1pos);
- }
- if (M2dest>M2pos) {
- M2pos++;
- servo2.write(M2pos);
- }
- if (M2dest<M2pos) {
- M2pos--;
- servo2.write(M2pos);
- }
- if (M3dest>M3pos) {
- M3pos++;
- servo3.write(M3pos);
- }
- if (M3dest<M3pos) {
- M3pos--;
- servo3.write(M3pos);
- }
- if (M4dest>M4pos) {
- M4pos++;
- servo4.write(M4pos);
- }
- if (M4dest<M4pos) {
- M4pos--;
- servo4.write(M4pos);
- }
- if (M1dest==M1pos and M2dest==M2pos and M3dest==M3pos and M4dest==M4pos and RUN==true) { // Ha mind a 4 motor a pozícióján van
- if (stepcounter1<stepcounter) { // Ha a futás közbeni lépés számláló kissebb, mint a lépés tároló, akkor a következő lépésre lép
- M1dest = steps[1][stepcounter1];
- M2dest = steps[2][stepcounter1];
- M3dest = steps[3][stepcounter1];
- M4dest = steps[4][stepcounter1];
- lcd.clear();
- lcd.print(M1dest);
- lcd.print(" ");
- lcd.print(M2dest);
- lcd.print(" ");
- lcd.print(M3dest);
- lcd.print(" ");
- lcd.print(M4dest);
- lcd.setCursor(0,1);
- lcd.print(stepcounter1);
- delay(500);
- stepcounter1++; // Futás közbeni lépésszámláló növelése
- }
- else { // A futás közbeni lépés számláló nagyobb, mint a lépés tároló, nincs több lépés, futás vége
- RUN=false;
- }
- }
- }
- //delay(10);
- }
- void valuecalc() {
- uint8_t sz=0,i ;
- for ( i=1;i<=10;i++) { // Serial buffer check
- if (sbuffer[i]==120 or sbuffer[i]==46) { // If x or ., end the checking
- sz=i-1; // Serial buffel -1, chunk the end marker
- break;
- }
- }
- //for (i=2;i<=sz;i++) {
- //}
- if (sz==2) { // If the serial buffer size 3 char (exp. M18 - M - motor, 1 - motor number, 8 - position value), then the data size only 1 char
- value=sbuffer[2]-48;
- }
- if (sz==3) { // If the serial buffer size 4 char (exp. M247 - M - motor, 2 - motor number, 47 - position value), then the data size 2 char
- value=(sbuffer[2]-48)*10;
- value=value+sbuffer[3]-48;
- }
- if (sz==4) { // If the serial buffer size 5 char (exp. M4152 - M - motor, 4 - motor number, 152 - position value), then the data size 3 char
- value=(sbuffer[2]-48)*100;
- value=value+(sbuffer[3]-48)*10;
- value=value+sbuffer[4]-48;
- }
- if (motor==1) {
- M1dest=map(value,0,180,servoMM[1][0],servoMM[1][1]);
- }
- if (motor==2) {
- M2dest=map(value,0,180,servoMM[2][0],servoMM[2][1]);
- }
- if (motor==3) {
- M3dest=map(value,0,180,servoMM[3][0],servoMM[3][1]);
- }
- if (motor==4) {
- M4dest=map(value,0,180,servoMM[4][0],servoMM[4][1]);
- }
- }
- void minMaxSet () {
- delay(500);
- while (digitalRead(Gomb)==LOW);
- lcd.setCursor(0,0);
- lcd.print("Min: ");
- delay(500);
- while (digitalRead(Gomb)==HIGH) {
- value1=map(analogRead(A0),0,1024,0,180);
- value2=map(analogRead(A1),0,1024,0,180);
- value3=map(analogRead(A2),0,1024,0,180);
- value4=map(analogRead(A3),0,1024,0,180);
- servo1.write(value1);
- servo2.write(value2);
- servo3.write(value3);
- servo4.write(value4);
- lcd.setCursor(6,0);
- lcd.print(value1);
- lcd.print(" ");
- lcd.setCursor(12,0);
- lcd.print(value2);
- lcd.print(" ");
- lcd.setCursor(6,1);
- lcd.print(value3);
- lcd.print(" ");
- lcd.setCursor(12,1);
- lcd.print(value4);
- lcd.print(" ");
- }
- delay(500);
- while (digitalRead(Gomb)==LOW);
- servoMM[1][0]=value1;
- servoMM[2][0]=value2;
- servoMM[3][0]=value3;
- servoMM[4][0]=value4;
- EEPROM.write(1,value1); // servo1 min
- EEPROM.write(3,value2);
- EEPROM.write(5,value3);
- EEPROM.write(6,value4);
- lcd.clear();
- lcd.print("Max:");
- delay(500);
- while (digitalRead(Gomb)==HIGH) {
- value1=map(analogRead(A0),0,1024,0,180);
- value2=map(analogRead(A1),0,1024,0,180);
- value3=map(analogRead(A2),0,1024,0,180);
- value4=map(analogRead(A3),0,1024,0,180);
- servo1.write(value1);
- servo2.write(value2);
- servo3.write(value3);
- servo4.write(value4);
- lcd.setCursor(6,0);
- lcd.print(value1);
- lcd.print(" ");
- lcd.setCursor(12,0);
- lcd.print(value2);
- lcd.print(" ");
- lcd.setCursor(6,1);
- lcd.print(value3);
- lcd.print(" ");
- lcd.setCursor(12,1);
- lcd.print(value4);
- lcd.print(" ");
- }
- delay(500);
- while (digitalRead(Gomb)==LOW);
- servoMM[1][1]=value1;
- servoMM[2][1]=value2;
- servoMM[3][1]=value3;
- servoMM[4][1]=value4;
- EEPROM.write(2,value1); // servo1 max
- EEPROM.write(4,value2);
- EEPROM.write(6,value3);
- EEPROM.write(8,value4);
- lcd.clear();
- lcd.print("Test: ");
- delay(500);
- while (digitalRead(Gomb)==HIGH) {
- value1=map(analogRead(A0),0,1024,servoMM[1][0],servoMM[1][1]);
- value2=map(analogRead(A1),0,1024,servoMM[2][0],servoMM[2][1]);
- value3=map(analogRead(A2),0,1024,servoMM[3][0],servoMM[3][1]);
- value4=map(analogRead(A3),0,1024,servoMM[4][0],servoMM[4][1]);
- servo1.write(value1);
- servo2.write(value2);
- servo3.write(value3);
- servo4.write(value4);
- lcd.setCursor(6,0);
- lcd.print(value1);
- lcd.print(" ");
- lcd.setCursor(12,0);
- lcd.print(value2);
- lcd.print(" ");
- lcd.setCursor(6,1);
- lcd.print(value3);
- lcd.print(" ");
- lcd.setCursor(12,1);
- lcd.print(value4);
- lcd.print(" ");
- }
- delay(500);
- while (digitalRead(Gomb)==LOW);
- lcd.clear();
- lcd.print("Setting OK.");
- delay(500);
- }
- void BTserial () {
- byte dataInb;
- if (Serial.available() > 0) {
- dataInb = Serial.read(); // Read the data as string
- sbuffer[sbc++]=dataInb;
- if (dataInb==120) { // 120 - x, the end marker
- if (sbc==2) { // If serial data counter 2, check the first char. The second char is end marker
- if (sbuffer[0]==65) { // A - slow
- count1=8000;
- counter=0;
- lcd.setCursor(0,0);
- lcd.print("SLOW ");
- }
- if (sbuffer[0]==66) { // B - normal
- count1=2000;
- counter=0;
- lcd.setCursor(0,0);
- lcd.print("NORMAL ");
- }
- if (sbuffer[0]==67) { // C - fast
- count1=1;
- counter=0;
- lcd.setCursor(0,0);
- lcd.print("FAST ");
- }
- if (sbuffer[0]==83) { // S - Save
- lcd.clear();
- lcd.print(M1dest);
- lcd.print(" ");
- lcd.print(M2dest);
- lcd.print(" ");
- lcd.print(M3dest);
- lcd.print(" ");
- lcd.print(M4dest);
- lcd.setCursor(0,1);
- lcd.print(stepcounter);
- steps[1][stepcounter] = M1dest;
- steps[2][stepcounter] = M2dest;
- steps[3][stepcounter] = M3dest;
- steps[4][stepcounter] = M4dest;
- stepcounter++;
- if (stepcounter>=100) stepcounter=99;
- lcd.setCursor(0,0);
- lcd.print("SAVE: ");
- lcd.setCursor(0,7);
- lcd.print(stepcounter);
- lcd.print(" ");
- }
- if (sbuffer[0]==76) { // L - Run
- if (stepcounter1>=stepcounter) stepcounter1=0; // Ha végig ment a program, akkor a lépésszámláló nullázódik
- if (stepcounter>0) RUN=true; // Van-e valamilyen érték a lépéstárolóban
- lcd.setCursor(0,0);
- lcd.print("RUN ");
- }
- if (sbuffer[0]==75) { // K - Pause
- RUN=false;
- lcd.setCursor(0,0);
- lcd.print("PAUSE ");
- }
- if (sbuffer[0]==82) { // R - Reset
- RUN=false;
- stepcounter=0;
- stepcounter1=0;
- M1dest=M1pos;
- M2dest=M2pos;
- M3dest=M3pos;
- M4dest=M4pos;
- lcd.setCursor(0,0);
- lcd.print("RESET ");
- }
- }
- if (sbuffer[0]==77 and RUN==false) { // M - motor, csak akkor vesz figyelembe új értéket, ha áll
- motor=sbuffer[1]-48; // Selected motor number. second char the position value
- valuecalc();
- lcd.setCursor(0,0);
- lcd.print("Motor: ");
- lcd.setCursor(0,7);
- lcd.print(motor);
- lcd.print(" ");
- }
- sbc=0;
- }
- } // Serial END
- }
- //END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement