Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: Having a weird issue with two LEDs and some light sensors on a Teensy LC

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    5

    Having a weird issue with two LEDs and some light sensors on a Teensy LC

    Hey. I made a quick thing in the office today that turns on an LED when a light sensor goes high, turns on another LED when another light sensor goes high, and turns on a relay when both light sensors go high.

    The board is a Teensy LC. The sensors are ALSPT19 Light sensors from Adafruit (adafru.it/2748). The LEDs are 5v LEDs with built in resistors for pinball machines.

    The issue is, the LEDs are not getting the full 3.3v. The relay gets 3.3v when activated. The LEDs get between 1.5 and 1v. They also seem to change brightness based on whether they're both on or not.

    Here's my code. Everything is working as expected except for the brightness/voltage issues. Let me know what you think.

    LEDs are "doorA" and "doorB", relay is "StpLght".

    Code:
    const int sensorA = A0;
    const int sensorB = A1;
    const int doorA = 12;
    const int doorB = 11;
    const int StpLght = 10;
    
    void setup() {
      Serial.begin(9600);
      pinMode(sensorA, INPUT);
      pinMode(sensorB, INPUT);
      pinMode(doorA, OUTPUT);
      pinMode(doorB, OUTPUT);
      pinMode(StpLght, OUTPUT);
    
    }
    
    void loop() {
      if (analogRead(sensorA) >= 25){
        digitalWrite(doorA, HIGH);
      }
      if (analogRead(sensorB) >= 25){
        digitalWrite(doorB, HIGH);
      }
      if (analogRead(sensorA) >= 25 && analogRead(sensorB) >= 25){
        digitalWrite(StpLght, HIGH);
      }
      else {
        digitalWrite(doorA, LOW);
        digitalWrite(doorB, LOW);
        digitalWrite(StpLght, LOW);
      }
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,565
    They are pulsing from the order of the code - try this:
    Code:
    void loop() {
      if (analogRead(sensorA) >= 25){
        digitalWrite(doorA, HIGH);
      }
      else {
        digitalWrite(doorA, LOW);
      }
      if (analogRead(sensorB) >= 25){
        digitalWrite(doorB, HIGH);
      }
      else {
        digitalWrite(doorB, LOW);
      }
      if (analogRead(sensorA) >= 25 && analogRead(sensorB) >= 25){
        digitalWrite(StpLght, HIGH);
      }
      else {
        digitalWrite(StpLght, LOW);
      }
    }

  3. #3
    Unrelated to LED problem, but I’d use two local variables to eliminate unnecessary calls to analogRead().

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,565
    Quote Originally Posted by tele_player View Post
    Unrelated to LED problem, but I’d use two local variables to eliminate unnecessary calls to analogRead().
    Not sure if this answers the question - but for example the above code could be written as:
    Code:
    void loop() {
      uint32_t SensorA_val = analogRead(sensorA);
      uint32_t SensorB_val = analogRead(sensorB);
      if (SensorA_val >= 25){
        digitalWrite(doorA, HIGH);
      }
      else {
        digitalWrite(doorA, LOW);
      }
      if (SensorB_val >= 25){
        digitalWrite(doorB, HIGH);
      }
      else {
        digitalWrite(doorB, LOW);
      }
      if (SensorA_val >= 25 && SensorB_val >= 25){
        digitalWrite(StpLght, HIGH);
      }
      else {
        digitalWrite(StpLght, LOW);
      }
    }

  5. #5
    Senior Member crees's Avatar
    Join Date
    Dec 2016
    Location
    Utah
    Posts
    210
    If code suggestions donít fix, I am guessing that the led draw is too much for the teensy to sink/source properly. I recall that pinball leds with built in resistors is to prevent lighting when the circuit leaks out a low voltage that incandescent bulbs donít show but the LEDs do (hence the resistor to fix the issue) . The LED Pinball Bulbs I am referring to is non ghosting bulbs. One way to fix this is to add a transistor circuit per led. (see post below for ideas).

    https://forum.pjrc.com/threads/36766...a-beginner-ish

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,565
    Quote Originally Posted by crees View Post
    If code suggestions don’t fix, I am guessing that the led draw is too much for the teensy to sink/source properly. I recall that pinball leds with built in resistors is to prevent lighting when the circuit leaks out a low voltage that incandescent bulbs don’t show but the LEDs do (hence the resistor to fix the issue) . The LED Pinball Bulbs I am referring to is non ghosting bulbs. One way to fix this is to add a transistor circuit per led. (see post below for ideas).

    https://forum.pjrc.com/threads/36766...a-beginner-ish
    That was my first wonder - too much current esp. depending on the 'relay' - hopefully it won't come to that - though it seems possible.

    Looking at the code - if only one door then its LED was set LOW right after it was set HIGH at loop() end - so half value makes sense.

  7. #7
    Junior Member
    Join Date
    Mar 2019
    Posts
    5
    These are all awesome ideas. Thank you so much! I'll give them a try when I'm back in the office on Monday and see how that goes.

  8. #8
    Junior Member
    Join Date
    Mar 2019
    Posts
    5
    The re-arranged code was exactly the right fix! I just loaded it up and it all seems to be working as intended now. This is a system of lights at our office that tells people when the two single-user bathrooms are in use, so they don't have to get up from their desk or walk across the warehouse and rattle the knobs to see if they can use the room. It really helps. Thank you so much for helping me with the code!

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,565
    Very practical - glad it helped and was the right answer.

    Now you can keep stats and see if a 3rd restroom is called for

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •