Go Down

Topic: Programming Depth sensor with Liner actuator  (Read 93 times) previous topic - next topic

Ryan_Baluta

Apr 17, 2019, 07:59 pm Last Edit: Apr 17, 2019, 08:01 pm by Ryan_Baluta
Im using a Bar30 High-Resolution 300m Depth/Pressure Sensor from Blue robotics to read the depth, pressure and temperature of water. I am trying to link the sensor to a linear actuator to move back and forth.

I am using the MS5837_Example code for the depth sensor and a L16-R series linear actuator from Actuonix.

I am trying to program it so that when the depth sensor picks up a certain value, it will tell the linear actuator retract back in, and then when the depth sensor reads a lower valueit will tell the linear actuator to stretch out.

But I am having trouble programming this. 

https://www.actuonix.com/L16-R-Miniature-Linear-Servo-For-RC-p/l16-r.htm

https://www.bluerobotics.com/learn/bar30-arduino-guide/

Here are the links for the parts I am using

dougp

#1
Apr 17, 2019, 09:00 pm Last Edit: Apr 17, 2019, 09:01 pm by dougp
Including links for the hardware is a good start but, you mention programming issues and don't include the code.  Kinda hard for anybody to help with that.

Don't forget:



Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

Ryan_Baluta


Code: [Select]
#include <Wire.h>
#include "MS5837.h"
#include <Servo.h>

Servo myservo;
MS5837 sensor;

int depth = 0;
int val;

void setup() {
 
  myservo.attach(9);
 
  Serial.begin(9600);
 
  Serial.println("Starting");
 
  Wire.begin();

  // Initialize pressure sensor
  // Returns true if initialization was successful
  // We can't continue with the rest of the program unless we can initialize the sensor
  while (!sensor.init()) {
    Serial.println("Init failed!");
    Serial.println("Are SDA/SCL connected correctly?");
    Serial.println("Blue Robotics Bar30: White=SDA, Green=SCL");
    Serial.println("\n\n\n");
    delay(5000);
  }
 
  sensor.setModel(MS5837::MS5837_30BA);
  sensor.setFluidDensity(997); // kg/m^3 (freshwater, 1029 for seawater)
}

void loop() {
  // Update pressure and temperature readings
  val = analogRead(depth);
  val = map(val, 0, 1023, 0, 180);
  myservo.write(val);
  sensor.read();

  Serial.print("Pressure: ");
  Serial.print(sensor.pressure());
  Serial.println(" mbar");
 
  Serial.print("Temperature: ");
  Serial.print(sensor.temperature());
  Serial.println(" deg C");
 
  Serial.print("Depth: ");
  Serial.print(sensor.depth());
  Serial.println(" m");
 
  Serial.print("Altitude: ");
  Serial.print(sensor.altitude());
  Serial.println(" m above mean sea level");

  if (sensor.read(sensor.depth() >= 1.2)){
    digitalWrite(val, 0);
  }

  if (sensor.read(sensor.depth() <= 0.5)){
    digitalWrite(val, 180);
  }
  delay(1000);
}


This the code that I have so far, my main issue coming up with some "if" statement for the actuator for when it reads the depth value. 

dougp

Have you determined you're getting reasonable data from the sensor?
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

AWOL

Code: [Select]
    digitalWrite(val, 180); Unusual, on several levels.
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

PaulS

Code: [Select]
Servo myservo;
Who else's servo would you be using? You're not really using a servo, are you? So, the instance name has TWO dumb parts. Start by fixing this.

Code: [Select]
  val = analogRead(depth);
That is NOT how to get a depth reading from an I2C device.

Code: [Select]
  sensor.read();
  if (sensor.read(sensor.depth() >= 1.2)){

Is the read() method really overloaded?

Does the second overload really take true or false?

Code: [Select]
  if (sensor.read(sensor.depth() >= 1.2)){
    digitalWrite(val, 0);
  }

  if (sensor.read(sensor.depth() <= 0.5)){
    digitalWrite(val, 180);
  }

You have defined val to be the value read from an analog pin, mapped to the range 0 to 180. It is extremely unlikely that you want to be able to write to all 180 pins on your Arduino. Which Arduino are you using, that has 180 pins? Why do you want to set the pin to state 180, anyway? They only have 2 states - on or off.
The art of getting good answers lies in asking good questions.

Go Up