# Read Objects (Heights and Pieces)

499

14

## Introduction: Read Objects (Heights and Pieces)

This only shows you the code (THIS IS NOT FOR THE ASSEMBLY OF THE BAND)

The code is set up after the transportation band, so if you wan't to use the code to your own band, it is easily for you to change the code.

(WWW.ARDUINO.COM HAS BEEN USED FOR EXAMPLES) I haven't made it 100%.

COPENHAGEN SCHOOL OF DESIGN AND TECHNOLOGY

It has been made with help from fellow student, teachers, and Arduino enthusiasts :)

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Things You Need

1x Grove ultrasonic sensor

1x Arduino board

1x Arduino base shield

1x Usb cable (Cable A to B)

You can see the things on the pictures above

## Step 2: Code and Assembly

Connect the Arduino board to the Base shield

Connect the sensor into D7

There you go, you can now se the heights and how many pieces of 3 types of object.

The object i've been using is

Object 1= 72mm

Object 2= 40,4mm

Object 3= 53,3mm

If you wan't, you can copy the codes, and change the range. Then will it be possible to have many mores object to read.

CODE:

//28/9-15

//www.arduino.com was used for examples in this code

// these constants won't change. They are the // lowest and highest readings you get from your sensor: const int sensorMin = 0; // sensor minimum (minimum measure) const int sensorMax = 128; // sensor maximum (The height from the sensor to the band)

int objectNumber1 = 1; // counting object number 1 int objectNumber2 = 1; // counting object number 2 int objectNumber3 = 1; // counting object number 3

// Pin output: const int pingPin = 7; // plug the sensor into D7. (You can also take another input, but than you have to change the code) int range = 0; // Range

void setup() { // put your setup code here, to run once: Serial.begin(9600); //turns on the serial printer. Allways use 9600

}

void loop() //Loop will run over and over again {

// establish variables for duration of the ping, // and the distance result milimeters: { long duration, mm; }

// The ping is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: { pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); } pinMode(pingPin, INPUT); int duration = pulseIn(pingPin, HIGH);

// convert time into distance int mm = microsecondsToMilimeters(duration); long Heights = 128 - mm; // Defines sensorMax - mm (Gives you the real heights of the object, you're measure)

if (Heights > 30 && Heights < 45) // Range of the first object) (Between 30-45mm) { Serial.println("Object 2"); // name of first object Serial.print("Heights: "); // Serial.print(128 - mm); // writes the heights in mm Serial.print(" mm"); Serial.println(); Serial.print("Pieces: "); // pieces of objects Serial.println(objectNumber1++); // writes how many of this object has passed the sensor Serial.println(); } if (Heights > 45 && Heights < 60) // Range of the second object) (Between 45-60mm) { Serial.println("Object 3"); Serial.print("Heights: "); Serial.print(128 - mm); Serial.print(" mm"); Serial.println(); Serial.print("Pieces: "); Serial.println(objectNumber2++); Serial.println(); } if (Heights > 60 && Heights < 80) // Range of the third object) (Between 60-80mm) { Serial.println("Obejct 1"); Serial.print("Heights: "); Serial.print(128 - mm); Serial.print(" mm"); Serial.println(); Serial.print("Pieces: "); Serial.println(objectNumber3++); Serial.println();

}

}

long microsecondsToMilimeters(long microseconds) { return microseconds / 2.9 / 2; }

158 11K
92 9.0K
203 14K