[SOLVED]LDR's seem to be linked. What am I missing?

Hi all, I'm building device that moves a stepper motor based on input from two LDR's. I developed the code using simple contacts (for convenience), and it worked great.

I've removed the digital contacts, and installed two LDR's in A1 and A2, but the code no longer seems to work. When I monitor the LDR's I noticed that when I light one up w/ a laser, it increases as expected, but so does the other one (but less so). Even with this, there is a large enough difference that my triggers should work, but again... they don't. :stuck_out_tongue: Any help would be appreciated.

I know my motor works because I can load my button code and everything is still fine. I have the LDR's in A1 and A2 with a 10k resistor on the output of the LDR feeding the Analog input. Ground is attached on the same rail as the output of the LDR.

#include <Stepper.h>

const int sensor_1 = A1; 
const int sensor_2 = A2;

int sensorValueA1 = analogRead(sensor_1);
int sensorValueA2 = analogRead(sensor_2);

const int stepsPerRevolution = 512;  

// initialize the stepper library on pins 4 through 7
Stepper myStepper (stepsPerRevolution, 4, 5, 6, 7);

void setup() 

{
  // Set the speed of the stepper.  
  myStepper.setSpeed(15);
  // initialize the serial port:
  Serial.begin(9600);
  analogReference(INTERNAL); 
//   pinMode(button_1, INPUT);
//   pinMode(button_2, INPUT);
   pinMode(sensor_1, INPUT); 
   pinMode(sensor_2, INPUT); 

   
}

void loop() 


{
  // read the sensors
  sensorValueA1 = analogRead(sensor_1); 
  sensorValueA2 = analogRead(sensor_2); 
  float voltage1 = sensorValueA1;(5.0/1023.0); 
  float voltage2 = sensorValueA2;(5.0/1023.0); 
  
{
  
    if ((analogRead(voltage1)<17) && (analogRead(voltage2)<17)) // Chassis on target
    {
    delay(100);
    }    // Wait's a tenth of a second before looking for new inputs.
   
}
{
        if ((analogRead(voltage1)<17) && (analogRead(voltage2)>17)) // Chassis is below target
        { 
         myStepper.step(1);   // Track up  
        }
}
{         
        if ((analogRead(voltage1)>17) && (analogRead(voltage2)<17)) // Chassis is above target
        { 
         myStepper.step(-1);   // Track down  
        }
         
}

     //Print sensor information
  Serial.print("sensor_1 = "); //from site
  Serial.println(voltage1);//from site //changed from sensorValueA1
  Serial.print("sensor_2 = "); //from site
  Serial.println(voltage2);//from site //changed from sensorValueA2
  delay (1000); 
}

As a side note, it's now started kicking me out of my Comm port. Not sure what that's all about, but I'm pretty sure it's pissed at me. HAHA!!

float voltage1 = sensorValueA1;(5.0/1023.0);

Wassat?

  analogReference(INTERNAL);

Why are you setting the ref to 1.1 volts and then calculating (or trying to) for 5v?

Mark

float voltage1 = sensorValueA1;(5.0/1023.0); 
  float voltage2 = sensorValueA2;(5.0/1023.0); 
  
{
  
    if ((analogRead(voltage1)<17) && (analogRead(voltage2)<17)) // Chassis on target
    {
    delay(100);
    }    // Wait's a tenth of a second before looking for new inputs.
   
}
{
        if ((analogRead(voltage1)<17) && (analogRead(voltage2)>17)) // Chassis is below target
        { 
         myStepper.step(1);   // Track up  
        }
}
{         
        if ((analogRead(voltage1)>17) && (analogRead(voltage2)<17)) // Chassis is above target
        { 
         myStepper.step(-1);   // Track down  
        }
         
}

     //Print sensor information
  Serial.print("sensor_1 = "); //from site
  Serial.println(voltage1);//from site //changed from sensorValueA1
  Serial.print("sensor_2 = "); //from site
  Serial.println(voltage2);//from site //changed from sensorValueA2
  delay (1000); 
}

Reading from a floating point pin number.
Yuk

Groove
I ditched that extra ;. I was commenting that formula in and out for testing, and on my last go left it in.
What would you suggest instead of the floating point?

Holmes4
I tried not using a reference at all, but the numbers I was getting from the LDR's were very low (like 0-2). When I added the internal reference, the numbers were larger (like 10-20). This spread was easier to grab a hard break point I could use for my switching. Would you suggest I do it a different way?

You analogRead a pin using an integer pin number, then you assign the returned value to a floating point variable.
You then use that floating point value to address (more than likely) another pin and analogRead that pin.

Does that seem sensible to you?

Would you suggest I do it a different way?

Yes. Junk the existing code.

Just read and print out the analog reading and see what you get.

The range of readings you seam to be getting makes me think that the LDR's are incorrectly wired. Provide a drawing of your circuit.

Mark

PS

Do not use pinmode for analog inputs.

M

Hi,

Its time

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We don't know how the LDRs are connected.
We don't know what the value of the series resistor.
What is the resistance of the LDR
(1) in normal room light?
(2) in complete darkness?
(3) when you shine your laser on it?

Do you have a DMM?

Thanks..... Tom.... :slight_smile:

I would not suggest hardware problems at this point - look at his code!
He may not have the problem any more once he fixes his heinously broken code.

  sensorValueA1 = analogRead(sensor_1);   // this reads sensor 1, stores in sensorValueA1 
  float voltage1 = sensorValueA1;(5.0/1023.0);  //this stores the value just read as a float. The (5.0/1023.0) is a separate statement and does nothing. 

if ((analogRead(voltage1)<17) && (analogRead(voltage2)<17)) 

//now, instead of analogRead()'ing the sensor, you're analogRead()'ing voltage1 and voltage2. 
// What are those equal to? Well, you just set it equal to the float representation of the number returned by analogRead() of the sensor - which is a number from 0 to 1023, now a number from 0.0 to 1023.0. 

//So we pass a float to analogRead() implicitly casting it to unsigned byte, meaning we have a byte containing a number from 0 to 255 (I don't know OTOMH what it does to values higher than 255). If that's over 14, analogRead() will subtract 14 from it. Then it truncates it to 3 bits. The upshot of all this is that you're reading random ADC channels at this point.

AFTER your removal of the semicolon (just looked above and saw you did that)....

That means that voltage1/2 are small - between 0 and 5.0. Since it's unlikely to actually be 5.0, it's 4.x, so it's truncated to a value between 0 and 4.
So, depending on what value you read into sensorValueA1, and sensorValueA2, that would determine which analog pin it's reading. If they were both around the same value, the result would be that the analogRead(voltage1) and analogRead(voltage2) are reading the same pin, because both voltage1 and voltage2 get truncated to the same integer.

@DrAzzy : see reply 3

Groove:
@DrAzzy : see reply 3

Yeah, but people were continuing to post suggesting hardware problems, and meanwhile in reply #4 (OP's last reply) he didn't seem to realize what was going on - and it would completely explain his observed behavior.

Okay, I'm (obviously) new at this. I think I get some of what you guys are saying. I'm away from my setup for the next few hours, but will try to fix it based on your advice and report back (probably tomorrow).

Thanks so far.

    if ((analogRead(voltage1)<17) && (analogRead(voltage2)<17)) // Chassis on target

Don't you just mean

    if ((voltage1<17) && (voltage2<17)) // Chassis on target

?

Hi,

I have the LDR's in A1 and A2 with a 10k resistor on the output of the LDR feeding the Analog input. Ground is attached on the same rail as the output of the LDR.

I think we have a;

Yeah, but people were continuing to post suggesting hardware problems

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Do you have a DMM?

Thanks ..Tom..... :slight_smile:

Okay, I figured out why my motor was pulsing. I noticed that it's actually turning by the 1 step and -1 step I was calling for. When I increase the number of steps, the motor turns thusly.

I know my code is nasty, and I appreciate the help. I'm still learning how to build a unicycle (which is embarrassing), but I have to go through this before I can learn to build a Ferrari.

#include <Stepper.h>

const int sensor_1 = A1;
const int sensor_2 = A2;

int sensorValueA1 = analogRead(sensor_1);
int sensorValueA2 = analogRead(sensor_2);

const int stepsPerRevolution = 512;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 4 through 7
Stepper myStepper (stepsPerRevolution, 4, 5, 6, 7);


void setup() 
{
  // Set the speed of the stepper.  The number is not rpm, but the motor does go faster w/ larger numbers.  
  myStepper.setSpeed(15);
  
  // initialize the serial port:
  Serial.begin(9600);
  analogReference(INTERNAL); //from site suggestion.  INTERNAL reference is 1.1V.  Need to check for a better reference, but this works.  
   pinMode(sensor_1, INPUT); 
   pinMode(sensor_2, INPUT); 
}

void loop() 
{
  // read the sensors

  sensorValueA1 = analogRead(sensor_1); 
  sensorValueA2 = analogRead(sensor_2);
  float voltage1 = sensorValueA1*(5.0/1023.0); //this math isn't necessary, but it works.
  float voltage2 = sensorValueA2*(5.0/1023.0); //this math isn't necessary, but it works.  
  {
  
    if (((voltage1)<0.08) && ((voltage2)<0.08)) // removed analogRead before voltage Chassis on target changed voltage1 to sensor_1
    {
    delay(100);
    }    // Wait's a tenth of a second before looking for new inputs.
   
}
{
        if (((voltage1)<0.08) && ((voltage2)>0.08)) // Chassis is below target
        { 
         myStepper.step(100);   // Track up  
        }
}
{         
        if (((voltage1)>0.08) && ((voltage2)<0.08)) // Chassis is above target
        { 
         myStepper.step(-100);   // Track down  
        }
         
}

     //Print debugging information
  Serial.print("sensor_1 = ");
  Serial.println(voltage1);
  Serial.print("sensor_2 = ");
  Serial.println(voltage2);
  delay (1000); 
}

I've integrated some of your suggestions, and puzzled over others (remember... unicycle).

New question:

If I replace my voltage < and > code w/ digital contacts, the program works perfectly, and the stepper smoothly runs up and down as long as the && conditions are met. Why is it fixed to my step count in this configuration?

I'm attaching a pen and ink sketch of my LDR wiring. I know it's wrong, but when I do it "right," my indicated voltage numbers are stuck at 5V (or 1023 depending on which line I have in) regardless of my light contact on the LDR.

EDIT:

As I was reading though my post, I noticed I had an extra delay in my serial reporting section. I ditched that, and had to add a few counts to my stepper run to smooth out the action, but....

IT WORKS!!!!!!!!!!!!!!!!!!!!!! Thanks a million guys. I'm so grateful.

Please feel free to offer me more feedback on the code. I'm taking it all in. :slight_smile: