Pages: [1]   Go Down
Author Topic: My First Arduino Robot, A Photovore  (Read 2822 times)
0 Members and 1 Guest are viewing this topic.
Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 40
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino Uno, L298N Motor Shield, 2 Photo Resistors, Push Button, Two Small DC Motor (Out of a broken helicopter not sure of the voltage or draw, but pretty small).

Code:
const int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6)
const int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11)
const int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7)
const int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13)
const int button = 7; //Motor ON OFF button
const int righteye = 0; // Right Photoresistor on PIN 0
const int lefteye = 1; // Left Photo Resistor on PIN 1

int lVal = 0; // used to store value from right eye
int rVal = 0; // used to store value from right eye
int val = 0; // val used to store button state
int old_val = 0; //stores the old value
int state = 0; // 0= LED OFF while 1 = LED ON

void setup()
{
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  pinMode(button, INPUT); // button is an input
}

void loop()
{
  val = digitalRead(button); // read input value and store it

  //check if there was a transition
  if ((val == HIGH) && (old_val == LOW)){
  state = 1 - state;
  }
  old_val = val; // val is now old, store it
  // check if button is pressed (HIGH)
  // and chage the state
  if (state == 1) {
  rVal = analogRead(righteye); // Read value from right eye sensor
  lVal = analogRead(lefteye); // Read value from left eye sensor
  analogWrite(pwm_a, rVal/4);  //set both motors to run at one quarter of light level
  analogWrite(pwm_b, lVal/4);
  } else {
    analogWrite(pwm_a, 0);  //set both motors to OFF
    analogWrite(pwm_b, 0);
  }
}

no body yet
« Last Edit: March 31, 2012, 02:28:04 am by bytedisorder » Logged

Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8602
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ummm, and you're posting because?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 40
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To get some feedback on the code, start a thread for the project. I'm still waiting for the wheels tracks and gearbox. First project with Arduino that's all.
Logged

Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8602
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

At a glance your code looks OK, I gather it's supposed to follow a light?

There's no debouncing on the button but that may not worry you.

The motor speed is directly tied to the light level (rVal/4), that may or may not be appropriate when you get real hardware.

You are not using the dir pins.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 40
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm looking at the http://www.robotshop.com/productinfo.aspx?pc=RB-Tam-27&lang=en-US Tamiya Tracked Vehicle Chassis Kit with the http://www.robotshop.com/productinfo.aspx?pc=RB-Tam-32&lang=en-US Tamiya 4-Speed Double Gear Box (Left / Right Independent) with two 3 volt motors. Do you think this is a decently good way to go about it?
Logged

Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8602
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I don't know enough about robots to advise on chassis etc, there may be others here who do or alternatively go over to the SoR forum.

http://www.societyofrobots.com/robotforum/

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 40
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Rob, I appreciate the input. The only qualm I was having with button debouncing was that I cant have a delay using this method of motor control.

Code:
const int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3 (or 5,6)
const int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 9 (or 10,11)
const int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 2 (or 4,7)
const int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 8 (or 12,13)
const int button = 7; //Motor ON OFF button
const int righteye = 0; // Right Photoresistor on PIN 0
const int lefteye = 1; // Left Photo Resistor on PIN 1

int lVal = 0; // used to store value from right eye
int rVal = 0; // used to store value from right eye
int val = 0; // val used to store button state
int old_val = 0; //stores the old value
int state = 0; // 0= LED OFF while 1 = LED ON

long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup()
{
  Serial.begin(9600);  // open the serial port
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  pinMode(button, INPUT); // button is an input
}

void loop()
{
  val = digitalRead(button); // read input value and store it

  //check if there was a transition
  if ((val == HIGH) && (old_val == LOW)){
    state = 1 - state;
  }
  old_val = val; // val is now old, store it
  // check if button is pressed (HIGH)
  // and chage the state
  if (state == 1) {
    rVal = analogRead(righteye); // Read value from right eye sensor
    lVal = analogRead(lefteye); // Read value from left eye sensor
    analogWrite(pwm_a, rVal/4);  //set both motors to run at one quarter of light level
    analogWrite(pwm_b, lVal/4);
  }
  else {
    analogWrite(pwm_a, 0);  //set both motors to run at one quarter of light level
    analogWrite(pwm_b, 0);
  }
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Serial.println(rVal);
    Serial.println(lVal);
  }
}

I suppose I could use a similar routine as I use there for the serial output to have the button debouncing on a delay of 100. Would that work?
Logged

Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8602
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can debounce without using delay() but I'd see if you need to first.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 40
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the body of the robot itself







Logged

Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 40
He that would make his own liberty secure, must guard even his enemy from oppression; for if he violates this duty, he establishes a precedent that will reach to himself.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Now I have to add a larger platform above the reads for the core and more sensors, such as IR on the corners.
Logged

Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8602
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That looks like it will be a fun thing to work with.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino socks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the body? Some kind of off-the-shelf module?? How much and where could I get one? Looks just the thing I've been looking for?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm looking at the http://www.robotshop.com/productinfo.aspx?pc=RB-Tam-27&lang=en-US Tamiya Tracked Vehicle Chassis Kit with the http://www.robotshop.com/productinfo.aspx?pc=RB-Tam-32&lang=en-US Tamiya 4-Speed Double Gear Box (Left / Right Independent) with two 3 volt motors. Do you think this is a decently good way to go about it?

He is using the parts quoted above or you can piece together your own with the different tamiya products.
http://www.robotshop.com/search/search.aspx?locale=en_us&keywords=tamiya&Brand=Tamiya&Category=Robot+Parts

Our you can check out stuff like http://www.robotshop.com/dfrobotshop-rover-tracked-robot-basic-kit-5.html or http://www.solarbotics.com/product/60125/
I have the dfrobotshop rover myself but they can be a bit limited if you want to do certain things.

I think there are few other platforms that take the tamiya tracks if you dig around.

Logged

Pages: [1]   Go Up
Jump to: