Ping))) SENSOR colour slider

hi, this is my first project (and youtube video) and i would like to share it to every one. i also put a tutorial in this video so that maybe you can make one. dont forget to comment :D

I would comment if I could see something

oups

http://www.youtube.com/watch?v=9DUIZnRMsjE

Not bad at all. Tell us how you did this, so we can comment on your work. :wink:

Easy,

1.connect the 5v, ground, signal pin(7) to the PING))) 2.connect an led to the ground and between the pin 4 to 13 (exepte 7) 3.copy and past the code written in this quote:

const int pingPin = 7;

void setup() { pinMode (13,OUTPUT); pinMode (12,OUTPUT); pinMode (11,OUTPUT); pinMode (10,OUTPUT); pinMode (9,OUTPUT); pinMode (8,OUTPUT); pinMode (6,OUTPUT); pinMode (5,OUTPUT); pinMode (4,OUTPUT); Serial.begin(9600); }

void loop() { long duration, inches, cm;

pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);

inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration);

Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();

delay(100); }

long microsecondsToInches(long microseconds) { return microseconds / 74 / 2;

return microseconds / 29 / 2; }

long microsecondsToCentimeters(long microseconds) { if ( pingPin == microseconds /50 /2 ) { digitalWrite(13,HIGH);

} else { digitalWrite(13,LOW); } { if ( pingPin == microseconds /60/2 ) { digitalWrite(12,HIGH); } else { digitalWrite(12,LOW); } { if ( pingPin == microseconds /70 /2 ) { digitalWrite(11,HIGH);

} else { digitalWrite(11,LOW); } { if ( pingPin == microseconds /80/2 ) { digitalWrite(10,HIGH); } else { digitalWrite(10,LOW); } { if ( pingPin == microseconds /90 /2 ) { digitalWrite(9,HIGH);

} else { digitalWrite(9,LOW); } { if ( pingPin == microseconds /100/2 ) { digitalWrite(8,HIGH); } else { digitalWrite(8,LOW); } { if ( pingPin == microseconds /110 /2 ) { digitalWrite(6,HIGH);

} else { digitalWrite(6,LOW); } { if ( pingPin == microseconds /120/2 ) { digitalWrite(5,HIGH); } else { digitalWrite(5,LOW); } { if ( pingPin == microseconds /130 /2 ) { digitalWrite(4,HIGH);

} else { digitalWrite(4,LOW); } { if ( pingPin == microseconds /140/2 ) { digitalWrite(3,HIGH); } else { digitalWrite(3,LOW); } } }

return microseconds / 29 / 2; } } } } } } } }

if something isn't working, tell me :)

if something isn't working, tell me

Well, givenconst int pingPin = 7;, then

if ( pingPin ==  microseconds /50 /2 )

or

if ( pingPin == microseconds /60/2 )

don't seem very likely.

well , the pingPin is name given to the Sensor connected to pin 7

if ( pingPin == microseconds /50 /2 )

this is the speed for the sending ultrasonic burst divised by 2 for the echo.

i dont get what the problem

the pingPin is name given to the Sensor connected to pin 7

No, "pingPin" is the name given to the pin to which your sensor is connected.

It has the value 7 decimal, which never changes. Do you see the problem now?

still not

you can just change the number of the pin where is connected the Sensor

const int pingPin = 7;

It has the value seven, 7, 0x7, 07, 0b111. You have made it a constant.

This

if ( pingPin ==  microseconds /50 /2 )

is satisfied by just one value of "microseconds" (for most intents)

Seeing it now?

sorry, i still cant understand. i dont know about constant int but i know about int

i still cant understand.

You've got a whole string of comparisons like

if ( pingPin ==  microseconds /50 /2 )
if ( pingPin == microseconds /60/2 )
if ( pingPin ==  microseconds /70 /2 )
if ( pingPin == microseconds /80/2 )
if ( pingPin ==  microseconds /90 /2 )

where "pingPin" only ever has, only ever can have, the value 7. Doesn't that seem wrong to you?

You've posted this code in the Exhibition as an example of working code - why?

thats all good, each 5 cm is gonna turn an led on

{{ sob }}

Why on Earth should a range calculation be related to the pin the sensor is connected to?
What if the next guy to download your code decides to attach the sensor to pin 12 instead?

so ,that stupid guy should change the code pin 7 to 12

..and then go through all the code and change all the range calculations?

Yes, that's one way of doing it.

ssssshhhhhhhhhhhhhh, its an exibition gallerie... not an annoying one

I'll repeat the question: Why should a range calculation be dependent upon the number of the pin the range sensor is attached to?

well, the ping pin is atached to pin 7 so, if ping sensor (ping pin(pin 7)) is equal to (lets say 35 cm), turn led on

so, if ping sensor (ping pin(pin 7)) is equal to (lets say 35 cm), turn led on

Yes, that could be a really sensible approach.

Not sure in which orthogonal universe that would be though.