Arduino Forum

Community => Exhibition / Gallery => Topic started by: Muneeb on Aug 25, 2011, 10:36 am

Title: Ping))) SENSOR colour slider
Post by: Muneeb on Aug 25, 2011, 10:36 am
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
Title: Re: Ping))) SENSOR colour slider
Post by: GrooveFlotilla on Aug 25, 2011, 12:32 pm
I would comment if I could see something
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Aug 25, 2011, 09:10 pm
oups


http://www.youtube.com/watch?v=9DUIZnRMsjE
(http://www.youtube.com/watch?v=9DUIZnRMsjE)
Title: Re: Ping))) SENSOR colour slider
Post by: JO3RI on Aug 25, 2011, 10:11 pm
Not bad at all. Tell us how you did this, so we can comment on your work.  ;)
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Aug 26, 2011, 01:45 am
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:
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  :)
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 06:12 pm
Quote
if something isn't working, tell me  

Well, given
Code: [Select]
const int pingPin = 7;, then
Code: [Select]
if ( pingPin ==  microseconds /50 /2 )
or
Code: [Select]
if ( pingPin == microseconds /60/2 )

don't seem very likely.
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 06:18 pm
well ,
the pingPin is name given to the Sensor connected to pin 7

Quote

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
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 06:29 pm
Quote
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?
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 06:36 pm
still not

you can just change the number of the pin where is connected the Sensor
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 06:39 pm
Code: [Select]
const int pingPin = 7;

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

This
Code: [Select]
if ( pingPin ==  microseconds /50 /2 )
is satisfied by just one value of "microseconds" (for most intents)

Seeing it now?
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 06:43 pm
sorry,
i still cant understand.
i dont know about constant int
but i know about int
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 06:46 pm
Quote
i still cant understand.


You've got a whole string of comparisons like
Code: [Select]
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?
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 06:49 pm
thats all good, each 5 cm is gonna turn an led on
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 06:55 pm
{{ 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?
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 07:13 pm
so ,that stupid guy should change the code pin 7 to 12
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 07:22 pm
..and then go through all the code and change all the range calculations?

Yes, that's one way of doing it.
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 07:28 pm
ssssshhhhhhhhhhhhhh,
its an exibition gallerie...
not an annoying one
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 07:29 pm
I'll repeat the question:
Why should a range calculation be dependent upon the number of the pin the range sensor is attached to?
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 07:34 pm
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
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 04, 2011, 08:10 pm
Quote
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.
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 04, 2011, 09:37 pm
well, i did it 35 cm (but i could up to 3 meter).
because i have less space in my room
Title: Re: Ping))) SENSOR colour slider
Post by: AWOL on Sep 05, 2011, 09:16 am
Are you just joking, or do you really not see the problem with what you have written?

Relating a measurement to the pin number to which the sensor is attached is a little like relating the size of someone's bank balance to their house number.
Title: Re: Ping))) SENSOR colour slider
Post by: Arrch on Sep 05, 2011, 06:58 pm
Let's see if explaining it this way helps...

In your code, you have something like this:

Code: [Select]
if ( pingPin ==  microseconds /50 /2 )

Using simple algebra, we can rewrite this as:

Code: [Select]
if (microseconds == pingPin * 100)

Now, we know that pingPin equals 7, because you've declared it as a constant and it will not change. So the LED will turn on at exactly 700 microseconds.

Now let's say I decide to use digital pin 12 instead of 7 for my sensor. That shouldn't change the behavior of my project at all, if the code is done correctly. But, if we plug 12 into pingPin, then the LED will turn on at 1200 microseconds. Because time is directly related to distance, that means that by simply changing the pin that I gather the sensor data from, the LED will turn on at more than 50% the distance. That's a problem because changing the pin shouldn't change the behavior of the project.
Title: Re: Ping))) SENSOR colour slider
Post by: Muneeb on Sep 05, 2011, 08:56 pm
oooooooooooooo  :smiley-eek:
thanks for the info
Title: Re: Ping))) SENSOR colour slider
Post by: JO3RI on Sep 06, 2011, 12:38 pm
He got the point  XD

I think people stayed very polite  and helpful and kept on explaining. Big hurray for all the posters  8)
I thought you all would loose your patience, but you didn't. :D

I'm just posting this because, I found it remarkable.