Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Detecting changes in array, where am I going wrong? on: December 04, 2012, 08:13:51 am
I changed the doubles to int's, and everything still works about the same.

On the code that moves the servo 180-0 and then 0-180 the code is precisely the same.  I had these in a separate function, but then I couldn't get them to reference the correct number in the array distancedata[def].  Would love to know how I was messing that one up.
2  Using Arduino / Programming Questions / Detecting changes in array, where am I going wrong? on: December 03, 2012, 12:36:56 am
Hoping to have someone smarter than me (not hard to find) look over this bit of poorly written code and figure out what I'm doing wrong.

I have a robot that turns a servo with an ultrasonic rangefinder with a laser "death ray" attached to it.  What I expect to happen is that the servo will turn 1 degree at a time, will record the distance to the nearest object at each degree, and then will scan at each degree and notice any change in distance... at which point it will fire its laser-death_ray-disintegration_device-laser_pointer at the object that has entered the scan area.

What I'm currently experiencing is that the "death ray" fires at anything beyond my minimum range (300 cm).  I know that using this library (newPing.h) I will get a response of "0" for ranges beyond what I set as the max range.  Sounds like an easy fix, right? Well, go ahead.  What the hell am I doing wrong?

Code:
#include <NewPing.h>
#define trigPin 6
#define echoPin 7
#define MAX_DISTANCE 300 // Maximum distance to ping for (in cm)

NewPing sonar(trigPin, echoPin, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

// servo stuff
#include <Servo.h> //include servo library
Servo servoMain; // Define our Servo.  It will start set at 90 degrees
Servo servoQE; // Define our QE Servo

int laser1 = 2;  // set laser trigger to dig pin

// variables that we will need throughout
double currentdist;
double distancedata[181]; // defines our search deflection array
int def;

void setup()
{
  Serial.begin (9600);  //Serial input for rangefinder if we hook it up to computer
  servoMain.attach(14); // servo on pin A0
  servoQE.attach(4); // up and down servo on pin 4
 
  pinMode (laser1, OUTPUT); // prepares pin A1 to power the laser
 
  servoMain.write(90);
  servoQE.write(180);
}

void loop()
{
  double scandif;
 
  servoMain.write(0); // turns servo to start position
  delay (500); // gives servo time to get to 0 position
 
  for (int def=0; def <= 180; def++){  // this for loop will make the robot pan its servo 180 degrees and take a distance measure at each
    servoMain.write(def);  // Turn servo
    delay (100);
    distancedata[def] = ranger();   
    delay (100);
   

  }

  for (int iterations=0; iterations <= 10; iterations++){ // Sets total number of times that bot will conduct scan
   
    for (int def=180; def >= 0; def--){  // scan and check distances from 180 to 0 degrees
     servoMain.write(def);  // Turn servo
     delay (100);
     currentdist = ranger();
     scandif = (distancedata[def] - currentdist);  // checks the current distance against the previous scanned distance for that deflection
 
     if (distancedata[def] != 0){
       if (scandif >= 100){
          target();
       }
     }
 
     if (distancedata[def] <= 100){
          if (currentdist <= 200){
           target();
        }
     }
     delay (100);

    }
 
 
 
   for (int def=0; def <= 180; def++){  // scan and check distances from 0 to 180 degrees
     servoMain.write(def);  // Turn servo
     delay (100);
     currentdist = ranger();
     scandif = (distancedata[def] - currentdist);  // checks the current distance against the previous scanned distance for that deflection
 
     if (distancedata[def] != 0){
        if (scandif >= 100){
           target();
        }
      }
 
     if (distancedata[def] <= 100){
        if (currentdist <= 200){
           target();
        }   
     }
     
     delay (100);
   }
  } 
}

double ranger(){
   int uS = sonar.ping(); // coolguy command from newping library
   return (sonar.convert_cm(uS));  // convert ping time to cm and return
     }

void target() {
   servoQE.write(170);
   delay(500);
   digitalWrite(laser1, HIGH); // fires on new target
   delay(1500);
   digitalWrite(laser1, LOW);
   delay(1500);
   servoQE.write(180);
   delay(500);
}
3  Using Arduino / General Electronics / Re: Pins stop working on Motorshield R3 on: December 01, 2012, 03:03:35 pm
In case anyone else checks out this thread looking for information for their own projects:

The laser works perfectly well with a 100ohm resistor in the line.
4  Using Arduino / Sensors / Re: Consistency with HC-SR04 on: November 30, 2012, 09:30:40 am
Tim,

Just quickly glanced across your ping library page and I think that using that will greatly improve the performance of my robot/killing machine.

I'll try out a 2S Lipo battery pack (I think they have a whole wall of them at the radio shack here in town).  I've been using 9V's and AA's because I understand them and as I work through this project I've been trying to build off things I already know and learn just one new thing at a time.  I think I'm getting familiar enough with batteries that I can step it up a notch.

Thanks for the help!

-Jim
5  Using Arduino / General Electronics / Re: Pins stop working on Motorshield R3 on: November 30, 2012, 08:57:42 am
Okay, haven't experimented yet to see if the resistor in the line will work with the laser diode, but assuming that it doesn't... what if I just used the original two button cell batteries in circuit with the laser, and then just used a mosfet or something to trigger the laser on and off?
6  Using Arduino / Sensors / Re: Consistency with HC-SR04 on: November 29, 2012, 05:23:54 pm
My code gives the command to move the servo one degree, delay 20, scan for range, delay 20... then rinse and repeat.  (badji, based on the project you just posted on LMR I know you're a huge fan of "delay" smiley )

My reason for thinking this is a power issue is that I've noticed that the sensor works far better when running off a freshly charged battery.  Back when I was just using the sensor to test proximity in my obstacle avoiding robot, the bot would continually act as though there were an obstacle in front of it unless I had a fresh battery hooked into it (as opposed to one with plenty of charge that I'd been using for a while).
7  Using Arduino / General Electronics / Re: Pins stop working on Motorshield R3 on: November 29, 2012, 05:17:13 pm
Okay, yes, I see now that I certainly confused the capacitance with the current.

Based on my last post I went out and bought a pack of 100 ohm resistors.  I was going to move the laser over one pin, put a 100 ohm resistor on in the line, crack open a beer and hand out high-fives, but now I'm thinking that isn't a great idea.

How do I find the "response curve for the laser diode" and the internal resistance of the battery so I can "intersect" them?

8  Using Arduino / General Electronics / Re: Pins stop working on Motorshield R3 on: November 28, 2012, 09:51:38 pm
Okay, that made sense.  I don't have a datasheet for the laserpointer or anything, but I can look up what the batteries were providing.

According to what I see online, an AG3 buttoncell battery is 1.5V and 35mA.  Since the device came with 2 x AG3's in series, that should be 3V at 35mA.  Based on this it looks like I should be under the 40mA limit, but I think (and I'm really still learning here) that what you're saying is that since the laser is set up with a diode which reduces a 3V / 35mA to the proper level for the laser, based on Ohms law when I increase the voltage to 5V, the current will be much higher?
9  Using Arduino / General Electronics / Re: Pins stop working on Motorshield R3 on: November 28, 2012, 11:51:27 am
The laser pointer is just a cheap dollar general version that we had around the house.  Was originally running off two 1.5v button cell batteries (AG3) in series, so it comes ready to run on 3v. 

Here is the motorshield I am using attached to my uno: http://arduino.cc/en/Main/ArduinoMotorShieldR3

Here is the pinout that I have on the motorshield currently:

Pin:
3 - Motor Power A (Comes pre-set this way with motorshield)
4 - Servo1 control
5 - laser power. pinmode OUTPUT, HIGH = laser on
6 - Trigger for an ultrasonic rangefinger
7 - Echo for an ultrasonic rangefinder
8 - Brake B (comes set this way with motorshield)
9 - Brake A (comes set this way)
10 - not used
11 - Motor Power B (pre-set)
12 - Motor Direction B (pre-set)
13 - Motor Direction A (pre-set)

A0 - Servo2 Control

Both servos and ultrasonic rangefinder are powered from the 5V pin on the motorshield.  The servos, rangefinder, and laser pointer all run their ground back to the two ground pins on the motorshield.

So laser receives power when pin is set to high, and turns off when set to low.  Applicable code looks like this:

int laser1 = 5;  // set laser trigger to dig pin 5

void setup(){
pinMode (laser1, OUTPUT); // prepares pin to power the laser
}

void loop(){
    digitalWrite(laser1, HIGH); // fires on target
    delay(1500);
    digitalWrite(laser1, LOW);
    delay(1500);
}
10  Using Arduino / Sensors / Consistency with HC-SR04 on: November 28, 2012, 10:11:02 am
I'm sure this is no surprise, but I'm having some consistency problems with the HC-SR04 ultrasonic sensor that I bought on ebay for $2.00.

My project uses the sensor set on top of a servo, I have the servo pan 180 degrees, taking a range reading at each degree, and then it will continue to scan back and forth to detect anyone approaching (change in distance at each degree as compared to original scanned difference).  Getting a lot of false readings.

I've noticed that it seems to work better when I'm running it off a freshly charged 9v, so I'm guessing that consistent power is the issue.  Currently it runs straight off the power from the arduino uno (via a motorshield). Does anyone have some experience with making this thing work right?  Would I be better off with a dedicated power source for the sensor?  Would capacitors in the line help? Is the product just such a cheap piece of garbage that it will never achieve the kind of consistency that I'm looking for?
11  Using Arduino / General Electronics / Pins stop working on Motorshield R3 on: November 28, 2012, 10:00:20 am
I was wondering if anyone else has had a problem with this and if so if there is a fix.  I'm using the standard motorshield that radioshack is selling attached to an Uno board.

The problem is that several of the pins have stopped working on the motorshield.  I'm running a laser pointer ("death-ray laser") off the board that is triggered by having a pin with an output "High" to power it.  Originally I ran the laser pointer off the A1 pin, but it stopped working after a few days.  When I remove the shield and run straight from the arduino uno it works fine from that pin.  I was in a hurry during project development, so I moved the trigger/power pin over to digital pin 5 and everything worked fine.  Zip ahead to 3 weeks later and suddenly the "death ray" isn't working on digital pin 5 anymore.

Does anyone have any suggestions? Has anyone else had trouble with motorshield pins?
Pages: [1]