Sketch problems

I am having trouble with my sketch using ultrasonic distance to activate a relay.

#define trigPin 12

#define echoPin 13

int relay2 = 2;

int i = 1000;

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(relay2, OUTPUT);

}

void loop() {

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

digitalWrite(2, LOW);

if (distance >= 25 || distance <= 2){

Serial.println(“Out of range”);

}

if (i >= 4){

delay(i);

(distance >= 10);

(distance <= 8);

i = i/2;

}

else {

digitalWrite(2, HIGH);

digitalWrite(2, LOW);

Serial.print(distance);

Serial.println(" in");

delay(i);

digitalWrite(2, LOW);

delay(i);

digitalWrite(2, HIGH);

i = i/2;

}

delay(2000);

}

What sort of trouble?

How is the setup wired?

Note that statements like this don’t do anything:

(distance >= 10);
(distance <= 8);

Please use code tags to post.

I agree with jremmington. It is not clear what you want this code to do, it is not clear what it is doing now. And it is not clear why you want what it is doing.

For example the if-statement based on the counter i makes no sense. If i starts as 1000 then for the most part this works as a way to sequentially divide i by 2 until it is less than 4, each time waiting for an ultrasonic pulse. Maybe you had something special in mind for this with those statements jremmington made note of. After the halving of i it waits for 2 seconds at the end of the loop. Once i becomes less than 4, each run through the loop it does it’s ultrasonic ping first, then starts switching the relay output with delays of a few miliseconds. And halving i again each time. Which means it eventually becomes 0 because it is an integere. Then it is followed by the 2 second delay at the end of each loop pass. If this is a mechanical relay then this short milisecond pulse switching of the relay may not be sufficient to move it. Definitely not if i=0. How this is supposed to be dependent on the ultrasonic echo distance is anybodies guess.

The attachment shows what I am trying to accomplish.

And what was the idea behind using the integer i and halving it each time?

This instructables seems to have the code where you based your code on:

http://www.instructables.com/id/Automat … ontroller/

Did it not suffice?

This is their sketch and I could not get it to compile.

What did I do wrong?

HC-SR04 Ping distance sensor:

VCC to arduiino 5v

GND to arduino GND

Echo to arduino pin 9

Trig to arduino pin8*/

#define echopin 9 //echo pin

#define echopin 8 //triger

int maxiumrange = 9;

long duration, distance;

void setup(){

Serial.begin (9600);

pinMode(trigpin, OUTPUT);

pinMode(echopin, INPUT);

pinMode(4, OUTPUT);

pinMode(13 OUTPUT);

}

void loop()

{

{

digitalWrite(trigpin, LOW);

delayMicroseconds(2);

digitalWrite(trigpin, HIGH);

delayMicroseconds(10);

duration=pulsein (echopin,HIGH);

distance=duration/58.2;

delay(50);

Serial.printin(distance);

}

if (distance<=15){

digitalwirw(4, HIGH);

digitalwirw(13, HIGH);

}

else if (distance<=13){

digitalWrite (4, LOW);

digitalWrite (13, LOW);

}

}

The compiler should tell you what is wrong. Spelling errors obviously, or syntax as it is officially called. Hint: Do the Copy/Paste thing.