Multiple Analog Sensors

Thread Starter

blake11

Joined Feb 20, 2010
21
Hey guys.. I originally posted this over on the Arduino forums, but since it is in C, I figured I should spread around my chances of finding an answer.

So, I am building a pretty big circuit that has 40 leds and 20 IR sensors. I have been prototyping using just 3 sensors with 6 leds. Right now, I can get the circuit to act how it is supposed to when an object passes in front of one of the sensors. When an object is detected, the Arduino talks to a shift register and sets the pin high this is associated the sensor. Each sensor is associated with 2 leds, one controlled by a standard inverter connected to a BJT that switches one of the leds. When nothing is in front of the sensor, the inverter causes the white led to illuminate, and when something is present, the shift register causes the inverter to go high (causing the BJT to switch) and the register switches another BJT to illuminate the other led. Before I confuse you much more, here is a drawing of what I am doing.

Here is a sketch of what I am talking about. Hopefully you can make out my handwriting.

I can see from the serial monitor that the sensors are detecting movement at the same time, but only one light will change at a time.

Here is the code.
Rich (BB code):
int sV01 = 0;  //Sensor 1 Analog Value
int sV02 = 0;  //Sensor 2 Analog Value
int sV03 = 0;  //Sensor 3 Analog Value
int bitToSet;
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
//Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
//Pin for Sensor01
const int s01 = 1;
//Pin for Sensor02
const int s02 = 2;
//Pin for Sensor03
const int s03 = 3;
//Pin for Master Resest (Active Low)
const int mRPin = 10;

int sValue;

void setup()
{
    Serial.begin(9600);  //Begin serial communcation );
    //Set pins to output because they are addressed in the main loop
    pinMode(latchPin, OUTPUT);
    pinMode(dataPin, OUTPUT);  
    pinMode(clockPin, OUTPUT);
    pinMode(mRPin, OUTPUT);
    Serial.begin(9600);
    Serial.println("reset");
}
 
void loop()
{
    digitalWrite(mRPin, HIGH);
    for(int i = 0; i < 3; i++)
    {
      Serial.print("__");
      Serial.print(analogRead(i)); //Write the value of the photoresistor to the serial monitor.
      Serial.print("__");
    }
    Serial.println("\r");
    
    if(analogRead(s01) > 250)
    {
       changeValue(1);
    }
    else if(analogRead(s02) > 250)
    {
       changeValue(2);
    }
    else if(analogRead(s03) > 250)
    {
       changeValue(4);
    }
    else if((analogRead(s01) > 250) && (analogRead(s02)))
    {
       changeValue(3);
    }
    else if((analogRead(s01) > 250) && (analogRead(s03)))
    {
       changeValue(5);
    }
    else if((analogRead(s02) > 250) && (analogRead(s03)))
    {
       changeValue(6);
    }
    else
    {
        changeValue(0);
    }
    //digitalWrite(mRPin, LOW);
}

// This method sends bits to the shift register:
void changeValue(int nTd) {

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, nTd);

    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
}
I just don't understand why this part is not working.

Rich (BB code):
else if((analogRead(s01) > 250) && (analogRead(s02)))
    {
       changeValue(3);
    }
    else if((analogRead(s01) > 250) && (analogRead(s03)))
    {
       changeValue(5);
    }
    else if((analogRead(s02) > 250) && (analogRead(s03)))
    {
       changeValue(6);
    }
Thanks in advance for any help you guys can provide.
 

Attachments

bertus

Joined Apr 5, 2008
22,276
Hello,

As SgtWookie said the "Embedded Sysytem and Microcontrollers" is a better place for your thread.
Here it will draw more attention.

The schematic is very hard to read , the contrast is low and the text is to tiny.

At the output transistors for the led : at one you use a resistor in the base line in the other with the invetrer not, Why is that?

Bertus
 

Thread Starter

blake11

Joined Feb 20, 2010
21
Thank you for moving the post where it might get a little more attention. I am not really sure what you are asking about the transistors. One gate input is being controlled by an inverter and the other is directly controlled by the shift register. So, when the register pin is low, the gate inline with the inverter will switch the led on. When the pin is high, the other led turns on.

Ohhh,. I just looked and I used a PNP symbol instead of the NPN type transistor. It is supposed to be a NPN type, 2n4401 BJT.
 

bertus

Joined Apr 5, 2008
22,276
Hello,

I have made some adjustments in the schematic.



I have put two resistors on the base of the 2N4401.
Without them the transistor will burn and overload the inverter or shiftregister.

Bertus
 

Attachments

Thread Starter

blake11

Joined Feb 20, 2010
21
Thank you Bertus. I already had a resistor on the base of the transistor directly connected to the shift register. Sgt Wookie really helped me out understanding how to calculate the required base resistor.

I will be adding in that second resistor as soon as possible also. But, do you see why my code is not working when trying to turn on two pins at the same time using this bit of code?
Rich (BB code):
else if((analogRead(s01) > 250) && (analogRead(s02)))
    {
       changeValue(3);
    }
Thank you for taking a look at my issue.
 

bertus

Joined Apr 5, 2008
22,276
Hello,

I do not know to much of programming,
but when you close each if statement with an endif (or was it fi in C) command,
will this help?

Bertus
 

rjenkins

Joined Nov 6, 2005
1,013
Whenever you transfer the shift register data to the output latches, all outputs will be set to match the present content of the shift registers.

You need to use a buffer in memory as an 'image' of the outputs, and set or reset the bits in that, then clock the whole buffer through the shift registers and fnally update the outputs.

That's the only way to 'remember' the state of the bits you are not specifically changing.
 
Top