Go Down

Topic: Code Check for ROV (Read 914 times) previous topic - next topic

mech_eng

Sorry was not trying to be rude or anything. I'll name the pins then re-post the code.

Capstone Design incorporates many disciplines, unfortunately my team has only Mechanical Engineers. We do have experience using MATLAB for mathematical/engineering problems. The projects are not provided by the school but by sponsors through the school. The sponsors give you the project scope and funding then you run with it. We did not have to use a micro controller, we could have used mechanical relays but the micro-controller is obviously superior. The Arduino makes this easier with lots of books and online help.

The point of the program is to use and engineering approach to develop a solution to a given problem. Some of the solutions may require, as is the case with this project, using knowledge outside of our discipline or seeking someone knowledgeable in said discipline, which is exactly what I am doing here.

mech_eng

Code with named output variables:
Code: [Select]

const char SOP = '<';
const char EOP = '>';

enum { Case, Speed };

int whichNumber = Case;

int  Casev, Speedv;
int M1A = 3;
int M1B = 4;
int M2A = 5;
int M2B = 6;
int MServ =11;



//-------------------------------------------------------------------


void setup ()
  {
  Serial.begin (9600);
  pinMode (M1A, OUTPUT);
  pinMode (M1B, OUTPUT);
  pinMode (M2A, OUTPUT);
  pinMode (M2B, OUTPUT);
  pinMode (MServ, OUTPUT);


  }

//-------------------------------------------------------------------

 
void processNumber (int n)
  {
  int x = n; 
 
  switch (whichNumber)
    {
     
    case Case:
      Casev = x;
      whichNumber = Speed;
      //Serial.print ("  Case = ");
      break;
     
    case Speed:
      Speedv = x;
      whichNumber = Case;
      //Serial.print (" Speed = ");
      break;
   
 
    }
    //Serial.print(x);
  } 
 
 
//-------------------------------------------------------------------
 
void processInput ()
  {
  static float receivedNumber = 0;
  static boolean False = false;
 
  byte c = Serial.read ();
 
  switch (c)
    {
     
    case EOP: 
      if (False)
        processNumber (- receivedNumber);
      else
        processNumber (receivedNumber);

    // fall through to start a new number
    case SOP:
      receivedNumber = 0;
      False = false;
      break;
     
    case '0' ... '9':
      receivedNumber *= 10;             // receivedNumber = recievedNumber*10
      receivedNumber += c - '0';       //receivedNumber = recievedNumber + (c - '0')
      break;
     
    case '-':
      False = true;
      break;
     
    }
  } 


//-------------------------------------------------------------------
 
void loop ()
  {
  if (Serial.available ())
  processInput ();
  int  var1 = Casev;
  int  var2 = Speedv;
   
    switch (var1)
    {
    //RIGHT 
    case 1:
    analogWrite(M1A,var2);
   
   
    digitalWrite(M1B, LOW);
    digitalWrite(M2A, LOW);
    digitalWrite(M2B, LOW);

    break;
   
    //LEFT
    case 2:
   
    analogWrite(M1A,var2);
   
    digitalWrite(M1B, HIGH);
    digitalWrite(M2A, LOW);
    digitalWrite(M2B, LOW);
    break;
   
    //FWD
    case 3:
    analogWrite(M1A,var2);
   
   
    digitalWrite(M1B, LOW);
    digitalWrite(M2A, HIGH);
    digitalWrite(M2B, LOW);
    break;
   
    //REV
    case 4:
    analogWrite(M1A,var2);
   
   
    digitalWrite(M1B, LOW);
    digitalWrite(M2A, LOW);
    digitalWrite(M2B, HIGH);
    break;
   
    //CW
    case 5:
   analogWrite(M1A,var2);
   
   
    digitalWrite(M1B, HIGH);
    digitalWrite(M2A, LOW);
    digitalWrite(M2B, HIGH);
   
    break;
   
    //CCW
    case 6:
    analogWrite(M1A,var2);
   
   
    digitalWrite(M1B, LOW);
    digitalWrite(M2A, HIGH);
    digitalWrite(M2B, HIGH);
    break;
   
    //SURFACE
    case 7:
    analogWrite(M1A,LOW);
   
   
    digitalWrite(M1B, LOW);
    digitalWrite(M2A, LOW);
    digitalWrite(M2B, LOW);
    break;
   
    //DIVE
    case 8:
   analogWrite(M1A,var2);
   
   
    digitalWrite(M1B, HIGH);
    digitalWrite(M2A, HIGH);
    digitalWrite(M2B, HIGH);
    break;
   
    //PITCH
    case 9:
     analogWrite(M1A,HIGH);
   
   
    digitalWrite(M1B, LOW);
    digitalWrite(M2A, LOW);
    digitalWrite(M2B, HIGH);
    break;
   
    //SERVO   
    case 10:
    digitalWrite (MServ,HIGH);
   
   
    //digitalWrite(3,LOW);
    //...digitalWrite(4, LOW);
    // digitalWrite(5, LOW);
    //digitalWrite(6, LOW);
    //digitalWrite(7, LOW);
    //digitalWrite(8, LOW);
    //digitalWrite(9, LOW);
    //digitalWrite(10, LOW);
    //digitalWrite(12, LOW);
    break;
   
    }
   
  }
  //-------------------------------------------------------------------



AWOL

Code: [Select]
void processNumber (int n)
  {
  int x = n;

Why the reassignment?

Code: [Select]
int M1A = 3;
int M1B = 4;
int M2A = 5;
int M2B = 6;

Are you planning on changing these pin assignments whilst the program is running?
If not, consider making them constants.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Create some functions, with meaningful names, like turnRight(), turnLeft(), dive(), rise(), etc. Call them in the appropriate place(s) in loop().

Which is easier to grasp:
Code: [Select]
    case 1:
    analogWrite(M1A,var2);
   
   
    digitalWrite(M1B, LOW);
    digitalWrite(M2A, LOW);
    digitalWrite(M2B, LOW);

    break;

or:
Code: [Select]
    case 1:
       turnRight();
       break;


The big advantage of functions is that you can debug them once. Comment out the whole loop() function. Create a new loop() function that does nothing but call turnRught(). Does the sub do what it is supposed to? If so, great. The function is working properly. If not, you can look at the logic of the function to see if there are errors, and you can look at the hardware. Is it connected to the right pins? If is powered correctly?

Divide and conquer. Don't try to do everything at once.

mech_eng

Thanks for the advice, when I have developed the final code I will post it. I'll clean it up and make logical names for what I want it to do. I have finals this week so it won't be until next week. It sounds like I am on the right track though. Thank you!

Go Up