Term 2 Project, drop mechanism update - A tired brain, rookie mistakes and revision

After successfully having made the retracting mechanism and testing it today, or rather thinking that it was successful, my brain just switched into gear and realized that a DC Motor won't hold its position to hold the camera up when it's hoisted. In desperation to not spend more time coding the same thing and my luck having turned, today I received the 360 degree SERVO motor (yes, servos or steppers is what you need for this kind of thing). Not wanting to change the prototyping for the mechanism on the breadboard so I can move on to other more pressing things, I simply modified it by adding the Arduino servo library and adding a pin for the signal. Result, with simply changing two values on the code I (or you) can forward or reverse either DC, or Servo, so it became kind of a universal motor code (for the DC  you don't need the servo commands but they won't hurt if you leave them).

 I have to admit to some frustration in the beginning as my servo was only rotating one way, but then my brain clicked once again and it occurred to me that some servos (if not all, I am not too sure) have a built-in H-bridge, which was the case of mine. The servo's H-bridge was conflicting with the H-bridge on the breadboard, so all I needed to do was to set both control pins to the same values and then tell it which way to rotate and for how long.

In short, now I can be certain that I have a motor that is strong enough to pull the camera from the water if it encounters some resistance.

See code attached below and video.

Until next time,

Luis

PS: I am aware that Blogger has issues playing some videos, I will update it with videos uploaded onto Youtube.


Code:

--------Water Sensor with Relay board connected to Continuous Servo, for drop mechanism

#include <Servo.h>
#define _relay 8
#define _motor 9
const int controlPin1 = 2;
const int controlPin2 = 3;
int previousState;
int motorDirection;

int timex = millis(); //----optional if controlling other things that require it
int timer = 0;        //----optional if controlling other things that require it

Servo _servo;


void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  previousState = 1;
  motorDirection = 1;


  _servo.attach(5);
  pinMode(_relay, INPUT);
  pinMode(_motor, OUTPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);


}

void loop() {
  // put your main code here, to run repeatedly:

  //--------------------Camera drop in contact with water & retracts when not -------------



  if (digitalRead(_relay) == HIGH && previousState ==!1) {


    Serial.print("  ON - Submerging Camera ");

    digitalWrite(controlPin1, HIGH);//--- interchange these (HIGH/LOW) if your motor does
    digitalWrite(controlPin2, LOW);//--- not have a built in H-Bridge

    _servo.attach(5);
 
    delay(5000);

    digitalWrite(_motor, HIGH);
    _servo.write(360);//--- line of code only relevant if you are using a servo
    delay(8000);
    digitalWrite(_motor, LOW);
   // _servo.detach();
    previousState = 1;



    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, LOW);

  }


  if (digitalRead(_relay) == LOW && previousState == 1) {

    motorDirection != 1;

    _servo.attach(5);
 
    Serial.print("  OFF  - Retracting Camera");
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);

    delay(5000);
    digitalWrite(_motor, HIGH);
    _servo.write(-360); //--- line of code only relevant if you are using a servo
    delay(8000);
    digitalWrite(_motor, LOW);

    //_servo.detach();
 
    previousState = !1;

    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, LOW);

  }


}

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

Comments

Popular Posts