Jump to content


Si_Price

Member Since 10 Dec 2014
Offline Last Active Mar 22 2015 11:45 PM
-----

Posts I've Made

In Topic: modifying funduino robot code for project.

16 March 2015 - 04:54 PM

You mean the distance at which it changes from the drive forward to the turn left state? That would be this code:

if(ultrasonic2Distance > 0 && ultrasonic2Distance < 6) { // there's an object in front of us

That code is saying to go into the IF statement if the distance measured is 6. Making that number smaller means the robot will have to get closer to the object before branching, making the number larger means it will branch sooner.

 

Ah okay thanks, I tried increasing it but my robot still manages to flip himself sometimes, I think it's because of the angle he approaches the wall and the radius of the distance sensor only reading from the front. I have managed to implement the flashing LED and the stop sequence though so that's something! thanks :)


In Topic: modifying funduino robot code for project.

16 March 2015 - 11:48 AM

Hi Nathan, how do you change the distance at which the ultrasonic sensor works, my robot flips itself when it gets into corners?


In Topic: modifying funduino robot code for project.

12 March 2015 - 07:03 PM

Yeah I think what I'm going to do is use your default robot code with a few modifications (LED light, stop delay) as a contingency in case I can't debug the code I've been trying to write, i've got a ton of other things on the project to work on at the same time (I'm writing the Max code as I type this)  so it'd be good to have a backup!


In Topic: modifying funduino robot code for project.

11 March 2015 - 12:11 PM

Also with the original code if I wanted to add a stop sequence before the robot turns after reading the distance sensor and turning left will this work?

 {
            // turn left
            
            rightMotor.write(90);
            leftMotor.write(90);
            
            delay (5000);
            
            
            rightMotor.write(180);
            leftMotor.write(180);
        }

The servo motors are quite noisy when they're running and this is for a sound art installation so I thought it would be useful to have pauses of silence so the audience can hear the composition, the only problem is when I have tried stopping the servo's in the past one still turns? it's like discharging voltage still or its receiving voltage from somewhere else (because it turns really slow).

 

Sorry to keep bombarding you with questions!


In Topic: modifying funduino robot code for project.

11 March 2015 - 11:33 AM

I'll have to have a look I did buy a kit that had a load of examples and I've started working my way through them, it's things like blinking LED's and stuff but I'm definitely going to get more into it when i finish university.

 

I've added the code in where I think it should be and I'm still not getting any changes to either the Sensor or the LDR, I think I'm going to have to admit defeat on this. I've only got two and a half weeks until It needs to be ready and there's still other aspects of the project I'm yet to finish, Here's the newly written code:

#include <Servo.h>

// create servo objects
Servo leftMotor;
Servo rightMotor;

const int serialPeriod = 250; // serial print to console every 1/4 second
unsigned long timeSerialDelay =0;

const int loopPeriod = 20;   // ultrasonic period and frequency (50hz)
unsigned long timeLoopDelay =0;

// pins used for ultrasonic sensor,LDR and LED 
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;

const int ldrPin = A0;
int led = 3;

int ultrasonic2Distance;
int ultrasonic2Duration;
int ldrValue;

#define DRIVE_FORWARD 0   // defined states
#define TURN_LEFT     1
#define MY_STATE      2

int state = DRIVE_FORWARD;

void setup()
{
    Serial.begin(9600);
    
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(ultrasonic2EchoPin, INPUT);
     pinMode(led, OUTPUT); 
     
    leftMotor.attach(13);
    rightMotor.attach(12);
}
    
void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay >= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        readLdr(); // read and store the LDR value
        
        stateMachine();
        
        timeLoopDelay = millis();
        
          digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
    }
}


void stateMachine()
{
    if(state == DRIVE_FORWARD) {
    Serial.println("inside DRIVE_FORWARD"); // debug
    
    if(ultrasonic2Distance > 0 && ultrasonic2Distance < 6) { // there's an object in front of us
        Serial.println("ultrasonic2Distance > 0 && ultrasonic2Distance < 6"); // debug
        state = TURN_LEFT;
    } else if(ldrValue > 500) { // if an LED is detected
        Serial.println("ldrValue > 500"); // debug
        state = MY_STATE; // next loop the program will branch into MY_STATE
    }
  else // there's an object in front of us
        {
            state = TURN_LEFT;
        }
    }
    else if(state == TURN_LEFT) // obstacle detected -- turn left
    {
        unsigned long timeToTurnLeft = 500; // it takes around .5 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurnLeft) // stay in this loop until timeToTurnLeft (.5 seconds) has elapsed
        {
            // turn left
            rightMotor.write(180);
            leftMotor.write(180);
        }
        Serial.println("nothing in front of us and no LED detected, driving forward.."); // debug
        // drive forward
        rightMotor.write(180);
        leftMotor.write(0);
    }  
        state = DRIVE_FORWARD; // after you do your thing, you can revert back to the default state
    }



void readUltrasonicSensors()
{
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    
    ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}


void readLdr()
{
    ldrValue = analogRead(ldrPin);
}

void debugOutput()
{
    if((millis() - timeSerialDelay) > serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm");
        Serial.println();
        
        Serial.print("ldrValue: ");
        Serial.print(ldrValue);
        Serial.println();
        
        timeSerialDelay = millis();
    }
}

Do you think it would be easier if i omitted the distance sensor from the code and just used the LDR?, it seems that the two states don't like working with one another so maybe if it was simplified it would work how I perceive it in my head?