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
2 Answers 2
I'd prefer to delete this question because I'm so embarrassed by the answer. I didn't notice I had Serial.println(distance);
in my function. The odd numbers were coming from my rangefinders and being printed in between my light readings.
There's no pinMode(lightSensorPin, INPUT);
in either of your codes. In the original code this probably doesn't matter because you set no other pins to In or Out.
As a side note, instead of declaring all your pin number as int
then defining them, make use of preprocessor directives:
#define trigPin1 5;
#define echoPin1 6;
#define blue1 7;
#define green1 8;
When you hit Verify or Upload the preprocessor sweeps through your code and replaces all instances of a defined term with whatever comes immediately after it in the definition. You can't use it for variables of course, but it's perfect for designating pins.
This will cut down on unnecessary memory usage. Not a huge saving in your case, but I don't see why people don't do it more often.
-
Thanks for the tip on the preprocessor directive. Do those go in the setup? Also, we already discussed that the
pinMode(lightSensorPin, INPUT);
does nothing different. It doesn't solve the problem.Michael Rader– Michael Rader2015年06月06日 12:43:20 +00:00Commented Jun 6, 2015 at 12:43 -
Also how do you specify
INPUT
orOUTPUT
with the preprocessor directive?Michael Rader– Michael Rader2015年06月06日 12:46:01 +00:00Commented Jun 6, 2015 at 12:46 -
#define
goes at the top of the program, along with things like#include <A_LIBRARY>
. That said, you can put#define
at the bottom, or in the middle, but code before it that relies on the definition will not compile. You can't specify if a pin is input or output with#define
- think of it as a 'find-and-replace' command. Change the first block of code to what I put in my answer above, in exactly the same place it is in currently, and you won't have to change a single bit of the remaining code fromvoid setup()
and beyond.CharlieHanson– CharlieHanson2015年06月06日 12:54:01 +00:00Commented Jun 6, 2015 at 12:54
Explore related questions
See similar questions with these tags.
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).