Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Question about Ranger Finder on: May 30, 2011, 05:57:43 am
Code:
digitalValue = digitalRead(6);             // Read input value
 
                                      // if the digital value is high enough, turn on the LED:
  if (digitalValue > threshold) {
"digitalRead" returns either zero or one.
"threshold" has a value of four.

Have you looked at the example code for these devices?

So what should I change "digitalRead" to analogRead?

As for the "4"  that was meant to be 4 inches.   
2  Using Arduino / Programming Questions / Re: Question about Ranger Finder on: May 30, 2011, 01:57:58 am
Quote
Any suggestions be great.
Post your code as it is now.

Ok after beating my head against this issue.  I still at a lost on how to turn off and on a LED if the var reaches a present amount.

Thanks for any input.

Quote
/* Tested Range finder with stop motor control

   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 20 May 2010
   by Watertown labs
 


 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 6;                     // pin that the input or signal
const int ledPin = 13;                     // pin that the LED is attached or turn off forward motor
const int threshold = 4;                  // an arbitrary threshold level that's in the range of the analog input
int digitalValue = 0;                       // Variable for reding the pin status


void setup() {
 
  Serial.begin(9600);         // initialize serial communication
 
  pinMode(ledPin, OUTPUT);    //declare LED as output
  pinMode(6, INPUT);          //declare sensor as input
}

void loop()
{
  digitalValue = digitalRead(6);             // Read input value
 
                                      // if the digital value is high enough, turn on the LED:
  if (digitalValue > threshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin,LOW);
  }

/*********************************     Start ping      ******************************/

  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // 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);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  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)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
3  Using Arduino / Programming Questions / Re: Question about Ranger Finder on: May 24, 2011, 07:06:20 am
OK, I got the datasheet from here
http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
Are you sure this is the device you have?

I don't see that the device has any kind of analogue interface, so I can't see where the analogRead in your sketch comes from.
I thought that the Ping tutorial worked out-of-the-box as posted - I haven't seen any bug reports for it - why did you reject so much working code?

Also "analogRead(6)" does not read the same resource as a "digitalRead(6)", so you're confusing the results even more.
Yes.

Ok, I looked over my sketch and connected it (I believe) correctly again. Yet the output is varys from  423 to 450, which isn't inches or cm so I during something wrong.

As for the "analogRead" I did say I borrow know working code and created mine. Any suggestions be great.
4  Using Arduino / Programming Questions / Re: Question about Ranger Finder on: May 18, 2011, 09:34:33 am
Its a Parallax 28015.

5  Using Arduino / Programming Questions / Re: controlling speed on: May 18, 2011, 07:13:29 am
lol no problem my error was I had no reference voltage so of course my pot wouldn't work this is why I said dumb mistake


Thanks!
6  Using Arduino / Programming Questions / Question about Ranger Finder on: May 18, 2011, 07:06:52 am
I was wondering how to code my Arduino to turn of a LED with a Ultrasonic rangefinder within a certain range.

I enclosed the code, which I attempted to mod from other sources.

Thanks in advance.

Quote
/* Range finder that turns off LED

   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 16 May 2010
   by Watertown labs
 


 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 6;                     // pin that the input or signal
const int ledPin = 13;                     // pin that the LED is attached or turn off forward motor
const int threshold = 17;                  // an arbitrary threshold level that's in the range of the analog input



void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop()
{
 
  int analogValue = analogRead(pingPin);   // read the value of the sonar:

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin,LOW);
  }

/*********************************     Start ping      ******************************/

  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // 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);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  Serial.println(analogValue, DEC); // print the analog value:
 
 
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
7  Using Arduino / Storage / Re: Arduino + official ethernet shield + SD -> all working together. on: May 04, 2011, 04:19:49 pm
Bump
8  Forum 2005-2010 (read only) / Interfacing / Re: Python error on: August 30, 2010, 10:33:26 pm
Thanks all
9  Forum 2005-2010 (read only) / Interfacing / Re: Python error on: August 30, 2010, 09:39:46 pm
Jonsn0w

Here the code in "red"  :-/

#!/usr/bin/env python
#
# joystick-servo.py
#
# created 19 December 2007
# copyleft 2007 Brian D. Wendt
# http://principialabs.com/
#
# code adapted from:
# http://svn.lee.org/swarm/trunk/mothernode/python/multijoy.py
#
# NOTE: This script requires the following Python modules:
#  pyserial - http://pyserial.sourceforge.net/
#  pygame   - http://www.pygame.org/
# Win32 users may also need:
#  pywin32  - http://sourceforge.net/projects/pywin32/
#

import serial
import pygame

# allow multiple joysticks
joy = []

# Arduino USB port address (try "COM5" on Win32)
usbport = "COM8"

# define usb serial connection to Arduino
ser = serial.Serial(usbport, 9600)

# handle joystick event
def handleJoyEvent(e):
    if e.type == pygame.JOYAXISMOTION:
        axis = "unknown"
        if (e.dict['axis'] == 0):
            axis = "X"

        if (e.dict['axis'] == 1):
            axis = "Y"

        if (e.dict['axis'] == 2):
            axis = "Throttle"

        if (e.dict['axis'] == 3):
            axis = "Z"

        if (axis != "unknown"):
            str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
            # uncomment to debug
            #output(str, e.dict['joy'])

            # Arduino joystick-servo hack
            if (axis == "X"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                if (move < 0):
                    servo = int(90 - abs(move))
                else:
                    servo = int(move + 90)
                # convert position to ASCII character
                servoPosition = chr(servo)
                # and send to Arduino over serial connection
                ser.write(servoPosition)
                # uncomment to debug
                #print servo, servoPosition

    elif e.type == pygame.JOYBUTTONDOWN:
        str = "Button: %d" % (e.dict['button'])
        # uncomment to debug
        #output(str, e.dict['joy'])
        # Button 0 (trigger) to quit
        if (e.dict['button'] == 0):
            print "Bye!\n"
            ser.close()
            quit()
    else:
        pass

# print the joystick position
def output(line, stick):
    print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
    while True:
        e = pygame.event.wait()
        if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):
            handleJoyEvent(e)

# main method
def main():
    # initialize pygame
    pygame.joystick.init()
    pygame.display.init()
    if not pygame.joystick.get_count():
        print "\nPlease connect a joystick and run again.\n"
        quit()
    print "\n%d joystick(s) detected." % pygame.joystick.get_count()
    for i in range(pygame.joystick.get_count()):
        myjoy = pygame.joystick.Joystick(i)
        myjoy.init()
        joy.append(myjoy)
        print "Joystick %d: " % (i) + joy.get_name()
    print "Depress trigger (button 0) to quit.\n"

    # run joystick listener loop
    joystickControl()

# allow use as a module or standalone script
if __name__ == "__main__":
    main()
10  Forum 2005-2010 (read only) / Interfacing / Python error on: August 30, 2010, 09:07:03 pm
Hello

I'm new to the world of Arduino, so when I found a cool project where the a USB joystick can control a servo via "python 2.7". Thinking that it would be eazy. I jumped right in.

Two weeks later, and countless "Google" searches. I'm still wonder what the hell is

pygame.joystick.init()
AttributeError: 'module' object has no attribute 'joystick'


Anyone here have any idea how to resolve this?

I tried the following
- re-installing Python
- copying the file to Lib
- Different PC and ver of software

Any advice would be appreciated.

11  Forum 2005-2010 (read only) / Workshops / Re: Arduino Class being taught at NYCResistor 12/5! on: December 01, 2010, 08:48:08 pm
Tx
Pages: [1]