Can Crusher Programming Help

I am trying to build a can crusher that will sense the can coming in with an ultrasonic sensor. Then it will tell a 12v pneumatic valve (normally closed) to send air to a pneumatic actuator (normally closed), crushing the can. I am having trouble with the code to do this process. I can get the code to tell me distance but I am having trouble getting it to do the process after. Ideally this is what would happen.

  1. Can comes in.
  2. Senor realizes that the distance changed.
  3. That causes the pneumatic actuator to crush the can. (around 5 secs to do this)
  4. Valve allows the actuator to return.
  5. Another can feeds and causes the same process and stops when there is no cans left.

Below is the code I have now. If you guys could offer up any help to get this to work please let me know.

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int valve = 11; // 12 volt valve that controls air flow

// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(valve, OUTPUT);
Serial.begin(9600); // Starts the serial communication

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance 
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(valve, HIGH);
  digitalWrite(valve, LOW);

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");

Possibly you actually mean

if (safetyDistance <= 5){
  digitalWrite(valve, HIGH);

This will close the valve for 5 seconds rather than 0.005 seconds. (I doubt the valve can actually move at all in 0.005 seconds)

Thank you for the help, that was the problem.

Hope you also fixed that missing ; as the code you posted doesn't compile. Or did you secretly go back and edit it, making all other comments look nonsensical?