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
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.