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/

Go Up