3D Printed Watercraft (Mini Ziphius)

Material Required

logo_beeverycreative

Schematic

Schematic: 3D Printed Watercraft

Code

Download from GitHub

  1. /**********************************************************************
  2. * © 2014 YD Ynvisible, S.A.
  3. *
  4. * FileName:        Mini_Ziphius.ino
  5. * Dependencies:    SoftwareSerial.h
  6. * Processor:       ATmega328
  7. * IDE:             Arduino 1.0.5
  8. *
  9. * Description:
  10. * Driving a 2 motors drone through a smartphone via Bluetooth 4.0
  11. * We have an App with a graphical interface to interact with the drone
  12. **********************************************************************/
  13.  
  14. #include <SoftwareSerial.h> //Software Serial Port
  15. #define RxD 6 //Bluetooth TX=Core RX
  16. #define TxD 7 //Bluetooth RX=Core TX
  17. #define FRONT 1
  18. #define BACK 0
  19. #define OFF 0
  20. #define ON 1
  21. SoftwareSerial blueToothSerial(RxD,TxD);
  22.  
  23. int motor1_pwm=9;
  24. int motor1_dir=8;
  25. int motor2_pwm=11;
  26. int motor2_dir=12;
  27. char X[3], Y[3], A, B;
  28. int X1_num;
  29. int i=1;
  30. int X_num,Y_num, A_num, B_num;
  31. int X_numf,Y_numf, A_numf, B_numf;
  32. int motor1_way,motor2_way;
  33. char recvChar;
  34.  
  35. void setup()
  36. {
  37.   pinMode(RxD, INPUT);
  38.   pinMode(TxD, OUTPUT);
  39.   pinMode(motor1_pwm, OUTPUT); //Motor1 PWM
  40.   pinMode(motor1_dir, OUTPUT); //Motor1
  41.   pinMode(motor2_pwm, OUTPUT); //Motor2 PWM
  42.   pinMode(motor2_dir, OUTPUT); //Motor3
  43.   digitalWrite(motor1_pwm,LOW);
  44.   digitalWrite(motor1_dir,LOW);
  45.   digitalWrite(motor2_pwm,LOW);
  46.   digitalWrite(motor2_dir,LOW);
  47.   setupBlueToothConnection();
  48. }
  49. void loop()
  50. {
  51.   if(blueToothSerial.available()){          //check if the pipe is free
  52.     recvChar = blueToothSerial.read();
  53.     if(recvChar!='\n'){                     //recognise end of line
  54.       if(i==2 && recvChar==','){            //treatment of data sent from the smartphone
  55.         i++;X[1]=NULL;
  56.       }
  57.       if((i==4 || i==5) && recvChar==','){  //treatment of data sent from the smartphone
  58.         i++;Y[1]=NULL;
  59.       }
  60.       switch(i){
  61.         case 1: X[0]=recvChar;break;
  62.         case 2: X[1]=recvChar;break;      
  63.         case 3: break;      //comma
  64.         case 4: Y[0]=recvChar;break;
  65.         case 5: Y[1]=recvChar;break;
  66.         case 6: break;      //comma    
  67.         case 7: A=recvChar;break;
  68.         case 8: B=recvChar;break;
  69.         default:break;    
  70.       }  
  71.       if(i==3 && recvChar!=','){            //treatment of data sent from the smartphone
  72.         X[0]='9';X[1]='9';i--;
  73.       }
  74.       if(i==6 && recvChar!=','){            //treatment of data sent from the smartphone
  75.         Y[0]='9';Y[1]='9';i--;
  76.       }
  77.       i++;
  78.      
  79.       if(i==9){                             //all data received?... start calculation convertion
  80.         X_num=atoi(X);
  81.         Y_num=atoi(Y);
  82.         A_num=A-'0';
  83.         B_num=B-'0';
  84.  
  85.         if(Y_num>=50){
  86.           Y_numf=Y_num-50;
  87.           Y_numf=Y_numf*255.0/50.0;
  88.           Y_numf=255-Y_numf;
  89.           motor1_way=FRONT;
  90.           motor2_way=FRONT;
  91.          
  92.         }
  93.         else{
  94.           Y_numf=50-Y_num;
  95.           Y_numf=Y_numf*255.0/50.0;
  96.           motor1_way=BACK;
  97.           motor2_way=BACK;
  98.      
  99.         }
  100.         Y_num=(int)Y_numf;
  101.        
  102.         if(X_num>=50){  
  103.           motor1_way=ON;
  104.         }
  105.         else{
  106.           motor2_way=ON;  
  107.         }
  108.         analogWrite(motor1_pwm,Y_num);        //Write the converted data sent from the smartphone to the motor driver
  109.         digitalWrite(motor1_dir,motor1_way);
  110.         analogWrite(motor2_pwm,255-Y_num);
  111.         digitalWrite(motor2_dir,!motor2_way);
  112.         i=1;
  113.       }
  114.     }
  115.   }
  116. }  
  117.  
  118. void setupBlueToothConnection(){
  119.   blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 9600
  120.   delay(10); // This delay is required.
  121.   blueToothSerial.print("AT+RENEW"); //Restore all setup value to factory setup
  122.   delay(10);  // This delay is required.
  123.   blueToothSerial.print("AT+ROLE0"); //make it a slave  
  124.   delay(10);  // This delay is required.
  125. }
  126. //END
/**********************************************************************
* © 2014 YD Ynvisible, S.A.
*
* FileName:        Mini_Ziphius.ino
* Dependencies:    SoftwareSerial.h
* Processor:       ATmega328
* IDE:             Arduino 1.0.5
*
* Description:
* Driving a 2 motors drone through a smartphone via Bluetooth 4.0
* We have an App with a graphical interface to interact with the drone
**********************************************************************/
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6 //Bluetooth TX=Core RX
#define TxD 7 //Bluetooth RX=Core TX
#define FRONT 1
#define BACK 0
#define OFF 0
#define ON 1
SoftwareSerial blueToothSerial(RxD,TxD);
int motor1_pwm=9;
int motor1_dir=8;
int motor2_pwm=11;
int motor2_dir=12;
char X[3], Y[3], A, B;
int X1_num;
int i=1;
int X_num,Y_num, A_num, B_num;
int X_numf,Y_numf, A_numf, B_numf;
int motor1_way,motor2_way;
char recvChar;
void setup()
{
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  pinMode(motor1_pwm, OUTPUT); //Motor1 PWM
  pinMode(motor1_dir, OUTPUT); //Motor1
  pinMode(motor2_pwm, OUTPUT); //Motor2 PWM
  pinMode(motor2_dir, OUTPUT); //Motor3
  digitalWrite(motor1_pwm,LOW);
  digitalWrite(motor1_dir,LOW);
  digitalWrite(motor2_pwm,LOW);
  digitalWrite(motor2_dir,LOW);
  setupBlueToothConnection();
}
void loop()
{
  if(blueToothSerial.available()){          //check if the pipe is free
    recvChar = blueToothSerial.read();
    if(recvChar!='\n'){                     //recognise end of line
      if(i==2 && recvChar==','){            //treatment of data sent from the smartphone
        i++;X[1]=NULL;
      }
      if((i==4 || i==5) && recvChar==','){  //treatment of data sent from the smartphone
        i++;Y[1]=NULL;
      }
      switch(i){
        case 1: X[0]=recvChar;break;
        case 2: X[1]=recvChar;break;      
        case 3: break;      //comma
        case 4: Y[0]=recvChar;break;
        case 5: Y[1]=recvChar;break;
        case 6: break;      //comma     
        case 7: A=recvChar;break;
        case 8: B=recvChar;break; 
        default:break;     
      }  
      if(i==3 && recvChar!=','){            //treatment of data sent from the smartphone
        X[0]='9';X[1]='9';i--;
      }
      if(i==6 && recvChar!=','){            //treatment of data sent from the smartphone
        Y[0]='9';Y[1]='9';i--;
      }
      i++;
      if(i==9){                             //all data received?... start calculation convertion
        X_num=atoi(X);
        Y_num=atoi(Y);
        A_num=A-'0';
        B_num=B-'0';
        if(Y_num>=50){
          Y_numf=Y_num-50;
          Y_numf=Y_numf*255.0/50.0;
          Y_numf=255-Y_numf;
          motor1_way=FRONT;
          motor2_way=FRONT;
        }
        else{
          Y_numf=50-Y_num;
          Y_numf=Y_numf*255.0/50.0;
          motor1_way=BACK;
          motor2_way=BACK; 
        }
        Y_num=(int)Y_numf;
        if(X_num>=50){   
          motor1_way=ON;
        }
        else{
          motor2_way=ON;   
        }
        analogWrite(motor1_pwm,Y_num);        //Write the converted data sent from the smartphone to the motor driver
        digitalWrite(motor1_dir,motor1_way);
        analogWrite(motor2_pwm,255-Y_num);
        digitalWrite(motor2_dir,!motor2_way);
        i=1; 
      }
    }
  } 
}  
void setupBlueToothConnection(){
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 9600
  delay(10); // This delay is required.
  blueToothSerial.print("AT+RENEW"); //Restore all setup value to factory setup
  delay(10);  // This delay is required.
  blueToothSerial.print("AT+ROLE0"); //make it a slave   
  delay(10);  // This delay is required.
}
//END