edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
MOHAMED IDREES A SYogesh SNithiswaran MBenil jack
Published

RayBot

An AI-powered water-quality monitoring rover using biomimetic sine-wave propulsion for efficient, autonomous aquatic exploration.

AdvancedWork in progress41
RayBot

Things used in this project

Story

Read more

Schematics

Schematic diagram

Code

Coding

C/C++
#define PH_PIN A0
#define TURBIDITY_PIN A1
#define TDS_PIN A2

#define LEFT_PWM 5
#define LEFT_DIR 4
#define RIGHT_PWM 6
#define RIGHT_DIR 7

#define VREF 5.0

int motorSpeed = 150;

float phValues[] = {6.5, 7.0, 7.2, 7.5, 8.0};
float tempValues[] = {25.0, 26.0, 27.0, 28.0, 29.0, 30.0};

int phIndex = 0;
int tempIndex = 0;

unsigned long lastChange = 0;

void setup()
{
  Serial.begin(115200);

  pinMode(PH_PIN, INPUT);
  pinMode(TURBIDITY_PIN, INPUT);
  pinMode(TDS_PIN, INPUT);

  pinMode(LEFT_PWM, OUTPUT);
  pinMode(LEFT_DIR, OUTPUT);
  pinMode(RIGHT_PWM, OUTPUT);
  pinMode(RIGHT_DIR, OUTPUT);

  stopRobot();

  Serial.println("Water Quality Monitoring Robot");
  Serial.println("--------------------------------");
}

void loop()
{
  // Change simulated pH and temperature every 10 seconds
  if (millis() - lastChange >= 10000)
  {
    lastChange = millis();

    phIndex++;
    if (phIndex >= 5)
      phIndex = 0;

    tempIndex++;
    if (tempIndex >= 6)
      tempIndex = 0;
  }

  float pH = phValues[phIndex];
  float temperature = tempValues[tempIndex];

  // ---------------- TURBIDITY ----------------
  long turbiditySum = 0;

  for (int i = 0; i < 20; i++)
  {
    turbiditySum += analogRead(TURBIDITY_PIN);
    delay(2);
  }

  float turbidityADC = turbiditySum / 20.0;

  float turbidityVoltage =
      turbidityADC * VREF / 1023.0;


  // ---------------- TDS ----------------
  long tdsSum = 0;

  for (int i = 0; i < 30; i++)
  {
    tdsSum += analogRead(TDS_PIN);
    delay(2);
  }

  float tdsADC = tdsSum / 30.0;

  float tdsVoltage =
      tdsADC * VREF / 1023.0;


  // Temperature compensation
  float compensation =
      1.0 + 0.02 * (temperature - 25.0);

  float compensatedVoltage =
      tdsVoltage / compensation;


  // TDS calculation
  float tds =
      (133.42 * compensatedVoltage * compensatedVoltage * compensatedVoltage)
      - (255.86 * compensatedVoltage * compensatedVoltage)
      + (857.39 * compensatedVoltage);

  tds = tds * 0.5;

  if (tds < 0)
    tds = 0;


  // ---------------- WATER QUALITY ----------------

  bool safeWater = true;

  if (pH < 6.5 || pH > 8.5)
    safeWater = false;

  if (tds > 1000)
    safeWater = false;


  // ---------------- MOTOR CONTROL ----------------

  if (safeWater)
  {
    moveForward();
  }
  else
  {
    stopRobot();
  }


  // ---------------- SERIAL MONITOR ----------------

  Serial.print("Temperature: ");
  Serial.print(temperature, 1);
  Serial.print(" C");

  Serial.print(" | pH: ");
  Serial.print(pH, 2);

  Serial.print(" | Turbidity: ");
  Serial.print(turbidityVoltage, 3);
  Serial.print(" V");

  Serial.print(" | TDS: ");
  Serial.print(tds, 0);
  Serial.print(" ppm");

  Serial.print(" | Status: ");

  if (safeWater)
    Serial.println("SAFE - ROBOT MOVING");
  else
    Serial.println("UNSAFE - ROBOT STOPPED");

  delay(1000);
}


// ---------------- MOVE FORWARD ----------------

void moveForward()
{
  digitalWrite(LEFT_DIR, HIGH);
  digitalWrite(RIGHT_DIR, HIGH);

  analogWrite(LEFT_PWM, motorSpeed);
  analogWrite(RIGHT_PWM, motorSpeed);
}


// ---------------- STOP ROBOT ----------------

void stopRobot()
{
  analogWrite(LEFT_PWM, 0);
  analogWrite(RIGHT_PWM, 0);
}

Credits

MOHAMED IDREES A S
2 projects • 2 followers
Yogesh S
0 projects • 1 follower
Nithiswaran M
0 projects • 2 followers
Benil jack
0 projects • 0 followers
Thanks to HARIHARAN K.

Comments