Term 2 Project - Arduino and Pi Zero code



After the setback with the heatshrunk camera cable to the Pi, I have used the spare cable and applied a coat of clear nail varnish to it, as the cable will be partially submerged when the camera is in use. I have today enclosed the camera and sealed it, the design changed slightly but I am 99% sure that it's water tight but I still need to test it. I am finalising the boat, installing the winch and imaging unit as well as sorting out the cable. For now, here's the code I used for the Arduino (the timings for the winch are not final, I still need to test them and I am positively sure they will be greatly reduced) and the script for the Pi Zero that starts filming for 5 minutes as soon as it finishes booting.

Of course, if you have a pond in your backyard or a local network, you can VNC and do this yourself, at will and for as long as you want, but I decided to go down this route to guarantee that I have some footage to show if there are any other setbacks.



We are getting there. Winch/Light sensor being installed


Raspicam in its housing and Pi Zero in its box to be intalled 




The code and script below.



/////////////////////////////////Pi Zero script, films as it boots- my additions in red. Camera uses auto exposure, at ISO 500 saving a native h264 file on the desktop, called underwater.h264 for 5 minutes or 300000 milliseconds ////////////////////////////////////////////////////////

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

raspivid -ex auto -ISO 500 -o /home/pi/Desktop/underwater.h264 -t 300000

exit 0


////////////////////////--------------Arduino Ship Electronics Code (winch timings to be adjusted. Includes my previous "universal" code for Servos and DC Motors          )------------////////////////////////////////////

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

int ledPin1 = 11;
int ledPin2 = 12;
int LDRin = 0;


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);

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

}

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


  //--------------------light level sensor lights up LEDs in camera------------------------

  LDRin = (analogRead(0) / -4); // divides 1023 values by 4, to translate it to close to 255 values for LED
  analogWrite(ledPin1, LDRin);
  analogWrite(ledPin2, LDRin);

  //--------------------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);

    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);

  }


}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Until next time,

Luis

Comments

Popular Posts