Pages: [1]   Go Down
Author Topic: L298N Problem with photo resistors  (Read 374 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

So, I am having a problem, hopefully somebody here has encountered it and has a solution. I am building a Photovore, and when I have my photo resistors hooked up directly to the Arduino, my light levels are correct (I.E. 920 directly under a flashlight). However in the exect same configuration under the exact same light source running the sensor on any analog pin on the motor shield, i get a reading like 28 or 30. I am poering directly from the USB at this point and this is with no motors running. I tried hooking up an additional power source (9V battery) via the DC jack on the Arduino, and I am get the same results (getting the results via serial print). Anybody know why this make be? I'm using a 10K ohm resistor ground and 5v out to the breadboard.

Here is my code

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);
  }
}

Every once in a while with the motor shield (L928N) i get a value like 331 or so. This is proving to be a major stumbling point on the road to a working photovore.
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

Looks like I blow out A0 and A1 on the L298N, or they didnt work when I bought it today, results are correct on A4 and A5.

Noope I'm a dummy, for the record: A0 and A1 are used by the L298N Motor Shield. Open/Disabled
« Last Edit: March 31, 2012, 02:39:22 am by bytedisorder » Logged

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

http://www.bitronictech.net/

Offline Offline
Sr. Member
****
Karma: 1
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One of them could me pulled down to gnd and the other pulled up to 5v. So, light and dark might be switched. On the one that has a reading of ~30, does it change according to the light level?
Logged

http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

Pages: [1]   Go Up
Jump to: