Show Posts
Pages: [1] 2 3 ... 6
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Help please with creating a Rangefinder with Processing. on: June 10, 2014, 07:26:27 am
Thanks for answering.

I put the line in the code in Processing. As I expected it printed "COM21".

The error line in Processing goes like this:

myPort = new Serial(this, Serial.list()[21], 9600);

Error: ArrayIndexOutofBound: 21

I don´t know what is wrong!

2  Using Arduino / Interfacing w/ Software on the Computer / Help please with creating a Rangefinder with Processing. on: June 08, 2014, 10:21:01 am
Hi,

I am trying to take on this project:
http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-part-3-visualising-the-data-from-sharp-infrared-range-finder/

The original code for Arduino:
Code:
/*
luckylarry.co.uk
Radar Screen Visualisation for Sharp GP2Y0A02 IR range finder
Sends sensor readings for every degree moved by the servo
values sent to serial port to be picked up by Processing
*/
#include <Servo.h>            // include the standard servo library
Servo leftRightServo;         // set a variable to map the servo
int leftRightPos = 0;         // set a variable to store the servo position
const int numReadings = 10;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
float total = 0;              // the total of all readings must be a float to allow totaling of float values
int average = 0;              // the average
int IRpin = 1;                // analog pin for reading the IR sensor
 
/* setup the pins, servo and serial port */
void setup() {
  leftRightServo.attach(9);
  // initialize the serial port:
  Serial.begin(9600);
}
 
/* begin rotating the servo and getting sensor values */
void loop() {
  for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) {  // going left to right.                                
    leftRightServo.write(leftRightPos);            
      for (index = 0; index<=numReadings;index++) {            // take x number of readings from the sensor and average them
        float volts = analogRead(IRpin)*0.0048828125;          // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
        float distance = 65*pow(volts, -1.10);                 // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
        total = total + distance;                              // update total
        delay(20);
      }
    average = (int) total/numReadings;                         // create average reading CAST TO INT!! remove the decimal places
 
    if (index >= numReadings)  {                               // reset the counts when at the last item of the array    
      index = 0;          
      total = 0;    
    }
    Serial.print("X");                                         // print leading X to mark the following value as degrees
    Serial.print(leftRightPos);                                // current servo position
    Serial.print("V");                                         // preceeding character to separate values
    Serial.println(average);                                   // average of sensor readings
  }
  /*
  start going right to left after we got to 180 degrees
  same code as above
  */
  for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) {  // going right to left                                
    leftRightServo.write(leftRightPos);            
    for (index = 0; index<=numReadings;index++) {
      float volts = analogRead(IRpin)*0.0048828125;            // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
      float distance = 65*pow(volts, -1.10);                   // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
      total = total + distance;
      delay(20);
    }
    average = (int) total/numReadings;  
    if (index >= numReadings)  {          
      index = 0;          
      total = 0;    
    }
    Serial.print("X");
    Serial.print(leftRightPos);
    Serial.print("V");
    Serial.println(average);
   }  
}

However I have two problems at the moment:

1) I don´t have an IR sensor but an Ultrasonic HC-SRO4 instead. I don´t know what to modify on the original code to make it work.

2) When I download the Processing code I get the error message "ArrayIndexOutOfBoundExeption: 1"
The error seems to came from the following line of code:
Code:
myPort = new Serial(this, Serial.list()[1], 9600);

My wiring:

- Servo attached to pin 9

- Ultrasonic Sensor: Trig to pin 6 and Eco to pin 7

- COM port assigned by Arduino at the moment: 21

Please help me. Thanks!
3  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 30, 2014, 03:29:16 pm
Sorry guys for not being able to catch up yesterday and today so far.

I took off the serial prints statements just to see if the delays that went with them were causing any problem. That was not the case so I will put them back in again. Additionally,  I'll send an email to skullbucks with my circuit diagram as requested.

Thanks again to you all for helping me!



4  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 28, 2014, 12:08:08 pm
Made the two mods. Thanks wildbill and skullbucks. However, it still doesn´t works. The robot turns to one side or the other ramdomly.

I really don´t understand what´s not working. The code looks ok for me to do the task.

May it be a problem with the ' turnLeft = !turnLeft' statement ? I have seen that some people use ' bool turnLeft = !turnLeft ' instead. However I also tried this other way and there is no improvements.
5  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 27, 2014, 09:28:26 pm
I like the idea wildbill. Following your advise I introduced the following if statement:

Code:
if (distance >= 30 && OldDistance <= 29);{
 turnLeft = !turnLeft;

So no the new code is this one:
Code:
#include <Servo.h>

Servo motorLeft;          // Define left motor
Servo motorRight;        // Define right motor
#define trigPin A2
#define echoPin A3
boolean turnLeft = true;
int OldDistance;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 motorLeft.attach(A5);    // Set left motor to pin A5
 motorRight.attach(A4);  // Set right motor to pin A4
}

void loop() {
  
  int duration, distance;                               //ultrasonic sensor routine
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  if (distance >= 30 && OldDistance <= 29);{
  turnLeft = !turnLeft;
  }
  
  if (distance >= 30) {
    Serial.println("Out of range");
    motorLeft.write(20);
    motorRight.write(20);
    }
  
  if (distance <= 29 && turnLeft == true){
    Serial.print(distance);
    Serial.println( "cm");    
    motorLeft.write(170);
    motorRight.write(10);        
    }
 
  if (distance <= 29 && turnLeft == false){        
    Serial.print(distance);
    Serial.println( "cm");    
    motorLeft.write(10);
    motorRight.write(170);        
    }  
    
   OldDistance = distance;
}  

To my understanding now the robot should alternate turns from one obstacle to the next one as I wanted. Instead when it finds and obstacle the weels try to go forward and backward at the same time so they get stucked. I really don´t know what is wrong now.
6  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 27, 2014, 03:05:17 pm
That doesn´t work wildbill. If I move the ' turnLeft = !turnLeft; ' to the turn subroutines the wheels try to go forward and backward at the same time. I tried before that variation and now again. That´s the result.

Any other approach?
7  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 27, 2014, 11:12:27 am
Well I keep on trying. In this new variation of the code I introduce the boolean value change ' turnLeft = !turnLeft; ' when there is no obstacle detected. So when an obstacle is detected, the robot rotates in one direction or the other depending on that value.

At  this point I think the code is ok to make the robot turn in alternating manner to one side and then when the next obstacle is detected to the other. But instead the robot turns ramdomly to one side or the other.

Variation of the code:
Code:
#include <Servo.h>

Servo motorLeft;          // Define left motor
Servo motorRight;        // Define right motor
#define trigPin A2
#define echoPin A3
boolean turnLeft = true;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 motorLeft.attach(A5);    // Set left motor to pin A5
 motorRight.attach(A4);  // Set right motor to pin A4
 
}

void loop() {
  
  int duration, distance;                               //ultrasonic sensor routine
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  if (distance >= 30 || distance <= 0){    
    Serial.println("Out of range");
    motorLeft.write(20);
    motorRight.write(20);  
    turnLeft = !turnLeft;
  }
  
  if (distance <= 30 && turnLeft == true){
    Serial.print(distance);
    Serial.println( "cm");    
    motorLeft.write(170);
    motorRight.write(10);        
    }
 
  if (distance <= 30 && turnLeft == false){        
    Serial.print(distance);
    Serial.println( "cm");    
    motorLeft.write(10);
    motorRight.write(170);        
    }  
    
  }

Getting much closer but not there yet. Help please to get to understand what could still be failing.
8  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 26, 2014, 08:54:53 pm
I removed the two delays in the turn subroutines. Now the robot goes forward until it finds an obstacle. But when it finds an it instead of turning right or left continuously the wheels kind of jamm in the same place.

I think the problem is that I don´t know how to change from true to false so the routinge of turning left ( or right ) keeps going until the obstacule is sorted.  I have tried variations to the if statement like do-while of for-loop. None had given any good results. That´s why an example would be great to clarify concepts.

Thanks!
9  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 26, 2014, 08:34:09 pm
I know that I have to take off the delays but if I do it with the code as it is the motors don´t work. That´s why I put them in even when they are not suitable for the task I'm trying to achieve.

Regarding the IF IT IS LESS AND 30 UNITS TURN Northof49 I don´t know what you mean. May I have an example please? Alongside that, even as I tried in many ways to change boolean from true to false when the event of clearing the obstacle is done I haven´t been successful so far. 

Is it possible please for someone to give me a routine example? Maybe that way I can understand the concept finally and start experimenting with it.

Thanks to you all for the help you are giving me.





10  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 26, 2014, 01:43:32 pm
I´m trying now to follow Paul's advise but the code is still not doing what I want.

This is the new code:
Code:
#include <Servo.h>

Servo motorLeft;          // Define left motor
Servo motorRight;         // Define right motor
#define trigPin A2
#define echoPin A3
int i;
boolean turnLeft = true;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 motorLeft.attach(A5);   // Set left motor to digital pin 4
 motorRight.attach(A4);  // Set right motor to digital pin 7
 
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 30 || distance <= 0){
    Serial.println("Out of range");
    motorLeft.write(20);
    motorRight.write(20);
  }
  if (distance <= 30){
    if(turnLeft){
    Serial.print(distance);
    Serial.println( "cm");    
    motorLeft.write(170);
    motorRight.write(10);
    delay (1500);
    turnLeft = false;
    }
    else {
    Serial.print(distance);
    Serial.println( "cm");    
    motorLeft.write(10);
    motorRight.write(170);
    delay (1500);    
    turnLeft = true;
    }
  }
  delay(100);
}

The problem is that now when the robot finds and obstacule it turns right for 1500ms and then left for 1500ms in a loop. But what I want is for robot turn right when it finds and obstacule for as long as it takes to get it sorted. And then when it finds the next obstacule to do the same but now turning left. I have tried many variations on the code, but I still can get the outcome wanted.
11  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 25, 2014, 06:01:31 am
Please someone tell me what I´m doign wrong with the code.

I already changed if ( i=0) for if ( I==0) but still doesn´t work. The if statements should be in a different place or something, but I don´t know where.
12  Using Arduino / Installation & Troubleshooting / Re: Can´t upload any code. Help please. on: April 24, 2014, 06:29:34 am
The problem was back! However, I think I know now what the issue was for certain. To fix the Arduino to my robot chasis I used some metallic nuts what were in thouch with the base of the board. They in fact were causing all this problem.

To follow the steps in the link you johnwasser sent me I took the board off the robot chasis. Then the board started working even the fact that I coudn´t follow all the steps in that link, just because the board wasn´t touching metal any more.

Thanks johnwasser for all your help as always. And good luck vishnuX in solving your problem as well.

13  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 23, 2014, 09:08:18 pm
Thank you for the correction widbill. But I don´t know where to put the if statements either. I tried different ways to put them in but I haven´t been successful to make it work so far.
14  Topics / Robotics / Re: Help please to improve this obstacle avoiding robot project. on: April 23, 2014, 02:25:00 pm
I still can´t make the code  work on my robot. Any help is more than welcome.
15  Using Arduino / Installation & Troubleshooting / Re: Can´t upload any code. Help please. on: April 23, 2014, 10:01:09 am
Hi Johnwasser,

i am using an Arduino UNO which I bought a few days ago, so in theory it should be the newer version.

You were right and the problem now seems to be solved. But I am now puzzled because I only downloaded the DFU programmer. I didn´t down download however de firmware update and didn´t reset using 8u2.

Is there a way that by only downloading the DFU programmer the problem was solved?

Thanks!!!
Pages: [1] 2 3 ... 6