Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Problem with code o...
 
Share:
Notifications
Clear all

[Solved] Problem with code of a conveyor belt

2 Posts
2 Users
0 Reactions
657 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  
//I2C LCD Toevoegen (Bibliotheek niet vergeten)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); /// of 16, 2

int trigPin1 = 10;
int echoPin1 = A1;
int trigPin2 = 3;
int echoPin2 = A0;
int trigPin3 = 5;
int echoPin3 = A2;

// Sensor 1,2 & 3 maximum afstand
int max1 = 60;
int max2 = 60;
int max3 = 70;

// Timers opzetten
unsigned long starttime;
unsigned long time1;
unsigned long endtime;

void setup() {
  //Sensor pinnen
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  
  //LCD Setup
  lcd.begin();
  lcd.clear();
  lcd.backlight();
  
  //Basic text LCD
  lcd.setCursor(2, 0);
  lcd.print("Volume");
  starttime = millis() //start timer
}

void loop() {
  // Variabelen set
  int duration1, distance1, duration2, distance2, duration3, distance3;
  
  // Sensor 1
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  
  // De afstand is de tijd maal door de snelheid van het geluid gedeeld door 2
  distance1 = duration1 * 0.034 / 2;
  
  if (distance1 < max1) {
    int final1 = distance1;
    delay(50);
    
    // Sensor 2
    digitalWrite(trigPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin2, LOW);
    duration2 = pulseIn(echoPin2, HIGH);
    
    // De afstand is de tijd maal door de snelheid van het geluid gedeeld door 2
    distance2 = duration2 * 0.034 / 2;
    
    if (distance2 < max2) {
      int final2 = distance2;
      delayMicroseconds(50);
      
      //sensor 3
      digitalWrite(trigPin3, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin3, LOW);
      duration3 = pulseIn(echoPin3, HIGH);
      distance3 = duration3 * 0.034 / 2;
      
      if (distance3 < max3) {
        int final3 = distance3;
        delayMicroseconds(50);
        
        // startime false maken
        bool starttime = false;
        
        // timer starten
        if (final3 < 56) {
          time1 = millis();
          if (final3 > 56) {
            endtime = millis();
            
            // tijd pakket onder sensor 3
            unsigned long elapsedtime = (endtime - meetwaarde1);
            
            // gemeten waardes omvormen en een paar berekeningen
            const int measuredvalue = (final1 + final2);
            const int widthpackage = (60 - gemetenwaarde);
            const int heightpackage = (54 - final3);
            const int opp1 = (heightpackage * widthpackage);
            const int timsensor3 = (elapsedtime * 10 ^ -3); //measured time is in millis and i need it in seconds
            const int speedconveyorbelt = (1);
            const int lengthpackage = (tijdsensor3 * speedconveyorbelt);
            lcd.setCursor(2, 1);
            lcd.print(lengthpackage);
          }
        }
      }
    }
  }
  delay(4000);
}

I'm doing a project where a package is on the move and during it's travel I need to measure the volume of that package. I've measured 2 dimensions already by using an ultrasonic sensor. So we can save 2 extra sensors we decided to measure the third dimension by timing how long it is detected under the sensor that measures the height. But me and my team are having trouble setting up the timer. When we have the time period we want to multiply it with the speed of the belt so we get the length. as last we will multiply every dimension and it will be printed on our lcd. We've already spend more than 15 hours trying to solve this problem. The arduino forum didn't help us either, can someone pls help us?

Sensor 1 and 2 are for the width, and Sensor 3 for the lenght and height of the package.

I normally have a photo, but I don't get allowed to upload one.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2717
 

Note: Sorry that I edited your post; I cleaned up your code a little for readability.

I have no idea how large your items are, but I can imagine that timing something passing a sensor can be quite a challenge and may need to be very accurate (depending on size and speed).

Having said that;
The Arduino may not be very suitable for this, since timing would require a proper "interrupt" to catch a precise value.
Meaning: your code is still running (the loop), but will not get interrupted when the item passes the sensor (start and stop).

 

What you could do, ... use an ESP32 microcontroller instead of an Arduino (you're not posting what kind of device you're using).
Here my short article on how to use an ESP8266 instead of an Arduino (works the same for ESP32).
It's easy to use and can be used like an Arduino - and those controllers are cheaper, have more memory, have WiFi, and are faster than most Arduino's.

The cool part of these ESP32 controllers is that they are multi core!
Normally you'd use only one of them, but you can actually use both.

To use that second core, you'll have to tinker a little, but it's not hard at all: See this article: "How to use ESP32 Dual Core with Arduino IDE".

So the idea being that the 2nd core does the more precise timing for items passing, and handing over the result to the main core.
(Eg. Core 1 = your main program, Core 2 = constantly measuring) 

 

Another option would be using a microswitch and a very light "arm" that is gently pressing the microswitch while the object is passing.
Since it is an actual switch, you could tie this on the Arduino to an interrupt, which allows you to do a precise timing.

In that case you'd start with the switch = off, time_start = 0.
Once the switch is touched, time_start=now (switch=on).
Keep doing a while loop until switch=off again, and the duration = now - time_start.

 

Again; I'm just doing some initial guesses right now, since I do not have a good idea of what we're working with (microcontroller, package sizes, speed etc).


   
ReplyQuote
Share: