Multiple Analog Sensors

Discussion in 'Embedded Systems and Microcontrollers' started by blake11, May 29, 2010.

  1. blake11

    Thread Starter New Member

    Feb 20, 2010
    21
    0
    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.
    Code ( (Unknown Language)):
    1. int sV01 = 0;  //Sensor 1 Analog Value
    2. int sV02 = 0;  //Sensor 2 Analog Value
    3. int sV03 = 0;  //Sensor 3 Analog Value
    4. int bitToSet;
    5. //Pin connected to latch pin (ST_CP) of 74HC595
    6. const int latchPin = 8;
    7. //Pin connected to clock pin (SH_CP) of 74HC595
    8. const int clockPin = 12;
    9. //Pin connected to Data in (DS) of 74HC595
    10. const int dataPin = 11;
    11. //Pin for Sensor01
    12. const int s01 = 1;
    13. //Pin for Sensor02
    14. const int s02 = 2;
    15. //Pin for Sensor03
    16. const int s03 = 3;
    17. //Pin for Master Resest (Active Low)
    18. const int mRPin = 10;
    19.  
    20. int sValue;
    21.  
    22. void setup()
    23. {
    24.     Serial.begin(9600);  //Begin serial communcation );
    25.     //Set pins to output because they are addressed in the main loop
    26.     pinMode(latchPin, OUTPUT);
    27.     pinMode(dataPin, OUTPUT);  
    28.     pinMode(clockPin, OUTPUT);
    29.     pinMode(mRPin, OUTPUT);
    30.     Serial.begin(9600);
    31.     Serial.println("reset");
    32. }
    33.  
    34. void loop()
    35. {
    36.     digitalWrite(mRPin, HIGH);
    37.     for(int i = 0; i < 3; i++)
    38.     {
    39.       Serial.print("__");
    40.       Serial.print(analogRead(i)); //Write the value of the photoresistor to the serial monitor.
    41.       Serial.print("__");
    42.     }
    43.     Serial.println("\r");
    44.    
    45.     if(analogRead(s01) > 250)
    46.     {
    47.        changeValue(1);
    48.     }
    49.     else if(analogRead(s02) > 250)
    50.     {
    51.        changeValue(2);
    52.     }
    53.     else if(analogRead(s03) > 250)
    54.     {
    55.        changeValue(4);
    56.     }
    57.     else if((analogRead(s01) > 250) && (analogRead(s02)))
    58.     {
    59.        changeValue(3);
    60.     }
    61.     else if((analogRead(s01) > 250) && (analogRead(s03)))
    62.     {
    63.        changeValue(5);
    64.     }
    65.     else if((analogRead(s02) > 250) && (analogRead(s03)))
    66.     {
    67.        changeValue(6);
    68.     }
    69.     else
    70.     {
    71.         changeValue(0);
    72.     }
    73.     //digitalWrite(mRPin, LOW);
    74. }
    75.  
    76. // This method sends bits to the shift register:
    77. void changeValue(int nTd) {
    78.  
    79.   // turn off the output so the pins don't light up
    80.   // while you're shifting bits:
    81.   digitalWrite(latchPin, LOW);
    82.  
    83.   // shift the bits out:
    84.   shiftOut(dataPin, clockPin, MSBFIRST, nTd);
    85.  
    86.     // turn on the output so the LEDs can light up:
    87.   digitalWrite(latchPin, HIGH);
    88. }
    I just don't understand why this part is not working.

    Code ( (Unknown Language)):
    1.  
    2. else if((analogRead(s01) > 250) && (analogRead(s02)))
    3.     {
    4.        changeValue(3);
    5.     }
    6.     else if((analogRead(s01) > 250) && (analogRead(s03)))
    7.     {
    8.        changeValue(5);
    9.     }
    10.     else if((analogRead(s02) > 250) && (analogRead(s03)))
    11.     {
    12.        changeValue(6);
    13.     }
    Thanks in advance for any help you guys can provide.
     
    • Ckt.jpg
      Ckt.jpg
      File size:
      153.7 KB
      Views:
      30
  2. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
  3. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,348
    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
     
  4. blake11

    Thread Starter New Member

    Feb 20, 2010
    21
    0
    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.
     
  5. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,348
    Hello,

    I have made some adjustments in the schematic.

    [​IMG]

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

    Bertus
     
  6. blake11

    Thread Starter New Member

    Feb 20, 2010
    21
    0
    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?
    Code ( (Unknown Language)):
    1. else if((analogRead(s01) > 250) && (analogRead(s02)))
    2.     {
    3.        changeValue(3);
    4.     }
    Thank you for taking a look at my issue.
     
  7. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,348
    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
     
  8. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    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.
     
Loading...