Appreciate any help on a project involving ultrasonic sensors

Hello all, working on a project and as a beginner I am pretty lost on where to even get started.
So I want to use an ultrasonic sensor to measure a fish. From that measurement I want Arduino to check against values in an array and make sure the fish being measured is above or below the required legal size. The device will also have an LCD attached, so the idea is you select the fish species first (using a rotary encoder), measure the fish and if its above the legal size a green LED will turn on, if undersize a red LED will turn on.

So far I can get an ultrasonic sensor working with the LCD and displaying the measurement its reading but that's all I can get to. The rest of the requirements I'm not sure how to start. Even just pointing me in the right direction could help.

I think maybe for the next step if I can create an array of 5 fish species with the legal catch sizes and use the rotary encoder to cycle through the values on the LCD?

Then next, do the ultrasonic circuit but also attach a green LED and set a value inside the code, say like 10cm. Then make the green LED come on when it is reading 10cm or above.

Really appreciate any responses, thanks.

When I'm kind'a stumped on a project like this. I find the actual best approach is to write the user manual. A very detailed one. What are the buttons? What exactly do they do? How do you use them? Where do I put the fish? What am I seeing on the display and when?

You will find that 99% of your code will be the UI. So, the better you have that pinned down early on the better off you will be. Then. Like you are starting to already, start coding the UI and see how much of that you can get working before adding the fish and ultra sonic hardware.

How in the world do you measure a fish with an ultrasonic sensor anyway?

-jim lee

jimLee:
How in the world do you measure a fish with an ultrasonic sensor anyway?

Well I guess it includes many factors like the fish should move in a straight line perpendicular to the ultrasonic sensor or something like that. Well that's in theory. I would like the OP to share how this could be achieved.

By the way, shouldn't this post be in Project Guidance?

Hello my friend,

Post code your testing with please. What Arduino board are you using in the project?
Please attach Schematics, photo's of real world testing environments the project is being tested in.

You might like to check this link out . Very helpful to understand how to create a first project.

Thanks for the replies.
The Arduino uno is housed in a box with all the components. This box is attached to a set of fish lip grips with a bike handlebar gopro mount.
Keep in mind this is a super basic project so it can be very rough built.
The fish is picked up with the lip grips and held above a surface so the tail is touching the ground perpendicular.
The sensor is basically just measuring the distance to the ground, which would be the length of the fish + some calibration/extra.

As for the UI, maybe I should mention the display being used is just the dox matrix LCD you get in beginner kits, nothing fancy. All it needs to display is the fish species name and legal length. Maybe even catch limit if I wanted to fill out the screen

EG on the screen it might say
FISH SPECIES: BREAM
LEGAL SIZE: 23CM, CATCH LIMIT: 25

Maybe if we break it down and get one part working first.
I already have the LCD screen, arduino and ultrasonic sensor connected so that it displays the length being measured. How could I add a green and red LED so that if the measurement is below 10cm the LED is red and if its above 10cm then the LED is green?

If it helps I could put this circuit together in tinkercad?

suffa01:
Maybe if we break it down and get one part working first.
I already have the LCD screen, arduino and ultrasonic sensor connected so that it displays the length being measured. How could I add a green and red LED so that if the measurement is below 10cm the LED is red and if its above 10cm then the LED is green?

A good start would be for you to post code you're currently using to display the length.

It's a lot easier to help with adding something when we know what it is that you're adding it to!

Steve

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

So the base code Im working with is this.
Also the current circuit

I think the next step would be to add a variable with a distance I can set eg 5cm. Then add an LED that goes red if its under 5cm. Any idea how to do this?

#include <LiquidCrystal.h>
#define trigPin 10
#define echoPin 13

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() { 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() { 
  float duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  if (distance >= 400 || distance <= 2){ 
    lcd.print("Out of range"); 
    delay(500); 
  }else{
    lcd.print(distance);
    lcd.print(" cm"); 
    delay(500); 
  }
  delay(500);
  lcd.clear();
}

Hi,
Ops circuit;


Tom.. :slight_smile:

How cool is it when you make a little progress. Its the best feeling! I can tell why this hobby is addictive.

I have added a red led and connected to pin 9. Altered the code so the LED turns on when "out of range" and off when there is a measurement being registered.

Hmm so next would be adding a green LED or an RGB? Ill look into that.

Ok so I suppose next is creating an array with a few fish species in it with their legal catch sizes. Then being able to cycle between the species on the screen and have it look at the legal measurement and turning the LED on or off based to that measurement being under or over.

#include <LiquidCrystal.h>
#define trigPin 10
#define echoPin 13
#define ledred 9

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() { 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledred, OUTPUT);
}

void loop() { 
  float duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  if (distance >= 400 || distance <= 4){ 
    digitalWrite(ledred,HIGH);
    lcd.print("Out of range"); 
    delay(500); 
  }else{
    digitalWrite(ledred,LOW);
    lcd.print(distance);
    lcd.print(" cm"); 
    delay(500); 
  }
  delay(500);
  lcd.clear();
}

Sorry, things move so fast in this forum. Can anyone help with my next step? See post above.

I'd just written an involved post about solving your first problem when it turned out to be pointless as you posted saying you'd solved it. That's great but I'm waiting a while for you to solve the next part before I try again.

So give it a try, you obviously know what needs doing. If you can't quite get it post your best effort together with any problems that still remain. Then we can help.

Steve

slipstick:
I'd just written an involved post about solving your first problem when it turned out to be pointless as you posted saying you'd solved it. That's great but I'm waiting a while for you to solve the next part before I try again.

So give it a try, you obviously know what needs doing. If you can't quite get it post your best effort together with any problems that still remain. Then we can help.

Steve

Thanks for going to that trouble. I am stuck at this point though.
The step I'm working on now is using a rotary encoder to cycle through fish species on a standard 2x16 LCD screen. So it would show something like

screen 1
FISH SPECIES: BREAM
LEGAL SIZE: 23CM

screen 2
FISH SPECIES: WHITING
LEGAL SIZE: 25CM

and so on. The plan from there would be that when a species is selected on screen, then the legal length of that species would control when the red or green led would be on.
Any help on this one? I have been stuck on it all day. Just when I think I have found a simple menu I found that the video is years old and the hosted files are no longer available. That or its just way to complex.

That sounds perfectly feasible. But I can't see any code where you've tried anything so there's not a lot I can help with. Can you read a rotary encoder? Can you display information on the LCD and change it when you want to? Do you know how to set up a arrays with information like WHITING and 25 in? Can you use the number from the encoder to read the appropriate entry in the array(s)? Then you're probably most of the way there.

Alternatively Google shows plenty of projects online for encoder based LCD display menus but none are going to do exactly what you need without work. You can always pick one and start from there.

Steve

Picking code from the net typically results in you having a dark brooding mysterious mess for your code base. It is literally the blind leading the blind.

You are MUCH better off starting your code yourself in Human. Then, translating your human code into computer. After you do that, you will have very little issue understanding what you did and why.

Always remember : "All those people that post code, they're not special. They're probably dumber than you." So roll your own.

-jim lee

Thanks for the replies, as a coder for arduino projects, consider me a complete noob. The only reason I can make edits and understand some of it is because I can scrape a website together in wordpress so Im used to changing stuff in css until something works.

Thats why Im so stuck on this next part, Im not even sure where or how to begin. Even just a link to a tutorial on how to create an array and have it display on an LCD screen would get me in the right direction. The stuff Im finding is just too far ahead of my capabilities.

What ill try and do and put something together as above and then post the code I have managed to do, then go from there.

Good morning friends,

Response to posts #9 and #12. Look at the link I posted... You might find some answers to questions you are yet to ask. A wise man once posted a response to a question. "Your lack of foresight and planning is not my responsibly....." I will not write the rest of the quote because it has no purpose here.

So have made some progress. I have been able to to get the rotary encoder to change what is on the lcd but using switch cases. The problem i have is its only one direction. Rotating the dial forward or back just results in the lcd changing between the switch cases consecutively. Any idea on how to make it go forward a switch state in one direction and back with the other direction?

#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

//--- Assign rotary pins---//
int rotaryOutputA = 8;
int rotaryOutputB = 9;
int rotarySwitch = 10;

//--- Set intial screen to page 1 ---//
int Display = 0;


void setup() {
  lcd.begin(16, 2);
  pinMode(rotaryOutputA, INPUT);
  pinMode(rotaryOutputB, INPUT);
  pinMode(rotarySwitch, INPUT);
  digitalWrite(rotaryOutputA, HIGH);      // turn on pullup resistor//
  digitalWrite(rotaryOutputB, HIGH);
  digitalWrite(rotarySwitch, HIGH);

//--- Welcome sreen ---//
  lcd.print("Welcome: Push"); 
  lcd.setCursor(0,1);
  lcd.print("button to begin");
}


void loop() {
 if (digitalRead(rotaryOutputA) == HIGH){ 
   delay(500);                        // delay to debounce switch

   Display ++;
   if(Display > 3){
     lcd.clear();
     Display = 1;
   }
   
   switch (Display) {
     case 1: {
       lcd.setCursor(0,0);
       lcd.print("Fish Species");
       lcd.setCursor(0,1);
       lcd.print("Bream");
       break;
     }
       
     case 2: {
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Fish Species");
       lcd.setCursor(0,1);
       lcd.print("Whiting");
       break;
     }
       
      case 3: {
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Fish Species");
       lcd.setCursor(0,1);
       lcd.print("Flathead");
       break;
      }
 }
}
}

What encoder are you using? How is connected? Your schematic is completely out of date.

You only ever look at 1 pin. Generally you need to look at both output pins to tell the direction it's moving. Have a look at this which should help How rotary encoder works

Steve