1

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
8
  • 1
    It wouldn't be something as simple as the light sensor "seeing" all those LED's, could it? :)
    – JRobert
    Commented Jun 6, 2015 at 11:45
  • No, they aren't bright enough. And even covering it with my hand it results in totally random numbers. Commented Jun 6, 2015 at 11:49
  • 1
    I see two things to try: 1) lightSensorPin's i/O direction hasn't been set; 2) There's a lot of I/O going on immediately before you call analogRead(). Try delaying for a couple of milliseconds before that read to let any transients settle out.
    – JRobert
    Commented Jun 6, 2015 at 12:04
  • 1
    0 - 3000? I missed that. You're seeing bigger numbers than the A/D can deliver (0 - 1023). Can something be over-writing lightSensorValue? Have you tried making lightSensorValue local to the loop() function? That would move it far away from the other globals (though it wouldn't live between calls to loop(). Also, Serial.println(analogRead(lightSensorPin)); should directly print the return from analogRead() (though the compiler may have already done that for you).
    – JRobert
    Commented Jun 6, 2015 at 12:34
  • 1
    @MichaelRader I've just read your solution. Thank you for cheering me up. And as far as embarassing mistakes go, welcome to the club. Commented Jun 6, 2015 at 12:59

2 Answers 2

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.

1

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.

3
  • 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. Commented Jun 6, 2015 at 12:43
  • Also how do you specify INPUT or OUTPUT with the preprocessor directive? Commented 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 from void setup() and beyond. Commented Jun 6, 2015 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.