Go Down

Topic: My First Arduino Robot, A Photovore (Read 2 times) previous topic - next topic

bytedisorder

Mar 31, 2012, 07:07 am Last Edit: Mar 31, 2012, 09:28 am by bytedisorder Reason: 1
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: [Select]
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
Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

Graynomad

Ummm, and you're posting because?

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

bytedisorder

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.
Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

Graynomad

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
Rob Gray aka the GRAYnomad www.robgray.com

bytedisorder

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?
Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

Graynomad

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
Rob Gray aka the GRAYnomad www.robgray.com

bytedisorder

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: [Select]
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?
Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

Graynomad

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

bytedisorder

Here is the body of the robot itself







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

http://www.bitronictech.net/

bytedisorder

Now I have to add a larger platform above the reads for the core and more sensors, such as IR on the corners.
Looking to host an Arduino related site? Check us out:

http://www.bitronictech.net/

Graynomad

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

andycb

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?

dropdeaddick


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.


Go Up