I have a light sensor that works fine and outputs the correct data to the serial monitor when I only upload the following code to the nano:
const int lightSensorPin = A0;
int lightSensorValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
lightSensorValue = analogRead(lightSensorPin);
delay(100);
Serial.println(lightSensorValue);
}
Now when I upload it with all of my other code, which includes lighting up about 6 LED's at one time and getting readings from two rangefinder sensors, the light sensors no longer output expected data, instead spewing out a bunch of random numbers, 0 to over 3000.
Could sharing the ground with all the LED's and rangefinders be causing interference for the light sensor? Or could it be a code optimization issue? Here's my entire code if it helps at all:
int trigPin1 = 5;
int echoPin1 = 6;
int blue1 = 7;
int green1 = 8;
int trigPin2 = A3;
int echoPin2 = A4;
int blue2 = A2;
int green2 = A1;
int redLED = 10;
int greenLED = 3;
int yellowLED = 9;
int blueLED = 4;
const int lightSensorPin = A0;
int lightSensorValue = 0;
int securityLength1 = 28;
int securityLength2 = 28;
int frontEyes(11);
int backEyes(12);
const char* front = "front";
const char* back = "back";
void setup()
{
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(blue1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(blue2, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(frontEyes, OUTPUT);
pinMode(backEyes, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
}//end setup
void loop(){
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, HIGH);
digitalWrite(blueLED, HIGH);
eyeballs(trigPin1, echoPin1, securityLength1, green1, blue1, front);
eyeballs(trigPin2, echoPin2, securityLength2, green2, blue2, back);
lightSensorValue = analogRead(lightSensorPin);
delay(100);
Serial.println(lightSensorValue);
}//end loop
void eyeballs(int trigPin, int echoPin, int securityLength, int green, int blue, const char* frontOrBack){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = (duration/2) / 29.1;
Serial.println(distance);
if (distance >= securityLength || distance <= 0) {
digitalWrite(green,HIGH);
digitalWrite(blue,LOW);
if (frontOrBack == "front") {
digitalWrite(frontEyes, LOW);
}//end if
else if (frontOrBack == "back") {
digitalWrite(backEyes, LOW);
}//end else
}//end if
else if(distance < securityLength) {
digitalWrite(blue,HIGH);
digitalWrite(green,LOW);
if (frontOrBack == "front") {
digitalWrite(frontEyes, HIGH);
}//end if
else if (frontOrBack == "back") {
digitalWrite(backEyes, HIGH);
}//end else
}//end else if
}//end function
lightSensorValue
? Have you tried makinglightSensorValue
local to theloop()
function? That would move it far away from the other globals (though it wouldn't live between calls toloop()
. Also,Serial.println(analogRead(lightSensorPin));
should directly print the return fromanalogRead()
(though the compiler may have already done that for you).