1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Need help with controlling an led fading sketch

Discussion in 'Arduino' started by hoghunter, Jul 17, 2017.

  1. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas
    Great info! I did consider that putting the supply directly through the Vin would not be feasible, so I do plan to use a PWM capable led driver (I already have it) on a 12v battery circuit to feed the leds via an PNP transistor(?). I will update my schematic and post it soon. I like the idea of incorporating the serial.prints in my code to verify what is happening. I will look into that (tutorials).

    One question:
    I did decide to swap the function of my code, putting the fade sequence before the delay loop. The code reads pin PO and if high, executes the fade, otherwise, it will show me several 1/2 sec blinks then loop back and start over. I'll change the blink code, when I know the sketch is flowing properly. However, no matter what I do, when I try to verify my sketch, } else { is highlighted and it tells me that there is either 'else without previous if'', or 'expected unqualified-id before '{' token'. I cannot get past it. I have fixed this issue in previous versions of my sketch, but am now stuck.What am I missing?
    Code (text):

    /*
     updown_fade

     This example shows how to fade an LED in/out using the analogWrite() function.

     The circuit:
     * LED (+) attached to pin 1. (3.3v w/ resistor)
     * motion sensor 'signal' will be attached to pin 2 (for now I am just attaching and detatching a 3.3v lead to pin2)
     * digispark microcontroller on breadboard (prototyping)

     Created 7/15/2017
     By John A. Austin





     */
    int sensorValue = 0; //analog value of sensorPin (P0) when off
    int sensorPin = 2;  //motion sensor out pin connected to digital pin 2 (P0 on digispark)
    int ledPin = 1;    // LED connected to digital pin 1
    int fadeValue = 0; //initial led brightness (variable)
    //int analogValue = analogRead(ledPin);

    void setup()
    {
    // initialize the sensorPin as an input:
      pinMode(sensorPin, INPUT);
    // initialize the ledPin as an output:
      pinMode(ledPin, OUTPUT);
    }





    void loop() {


    if(digitalRead(sensorPin) == HIGH)
    for (int fadeValue = 0 ; fadeValue <= 255 ; fadeValue += 1)
    {
    analogWrite(ledPin, fadeValue);
    delay(235);
    }
    {
    analogWrite(ledPin, 0);
    delay(2000);
    analogWrite(ledPin, 5);
    delay(5000);
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 1)
    analogWrite(ledPin, fadeValue);
    delay(125);
    analogWrite(ledPin, 5);
    delay(5000);
    } else {
    analogWrite(ledPin, 255);
    delay(500);
    analogWrite(ledPin, 0);
    delay(500)
    loop(5);
    }


     
     
  2. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas
    As I'm editing my schematic in eagle, I just realized the the analog signal from P1 would only trigger a gate open or closed on the transistor, so I am going straight to a PWM led driver........... I'll post it soon.
     
  3. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas
    Here it is:
    upload_2017-7-19_11-30-25.png
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Wp100

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    965
    Likes:
    43
    Location:
    England

    Just space out your code into smaller sections, it soon becomes clear where you have gone wrong with your { }

    No Expert on Ardunio code but do not know what/how this is used ; it did not build . loop(5);

    Had to Upload the code as a .zip file, cannot find the code # tag on the menu bar ???

    Edit - the diagram looks ok
     

    Attached Files:

  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,830
    Likes:
    138
    Location:
    morristown,tn
    I read this from top and from what your saying?
    You want the thing to come on it tripped for 7 minutes then go back off
    You really need to have a better switch code
    Code (text):

    if (pinInput == 1)
    {
      // action A
    Run fade code here
    }
    else
    {
      // action B
    Run delay code here
    }
    And if you want more to happen add it here.
     
  7. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas
    Fantastic, I actually went back to a previous, cleaner version of my sketch and have it working now. It is triggering HIGH/LOW and flowing properly as I am watching the led on the controller. I'm trying to get the real world circuit built on the breadboard now (fingers crossed). Thank you for all of your input, it is really helping.....
    Thanks be80be. Here's the final code and it is working with the sensor really well now. The problem I am having now is figuring out how to connect my LED driver board into the circuit. I will be driving (2) or (3) 3W high power leds ea with a Vf of 2.2v. This particular driver board needs a min 7v apparently. The board has 7 pins.... (2) IN+, (2) IN-, PWM, LED+ & LED-. I cannot find any diagrams of how to connect this board if using the PWM. Here is the board I plan to use. I'm assuming it has a mosfet incorporated somewhere and that maybe I need to add a pull down/up resistor or something to my circuit, but that is over my head as you can imagine. Any ideas?
    [​IMG]
     
  8. KeepItSimpleStupid

    KeepItSimpleStupid Well-Known Member Most Helpful Member

    Joined:
    Oct 30, 2010
    Messages:
    9,939
    Likes:
    1,099
    What are the numbers on U1? from the markings you can hopefully find the chip number and datasheet and go from there.

    Measure the voltage from PWM control to Power In -. Suspect it will be +5.
     
  9. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas
    Wp100, I finally lookedcat the sketch in post #20. That opto isolator is pretty slick.... my fade sequence seems to be working fine right now as I have a 2N3904 NPN transistor on the neg leg of the leds. It's being switched by the PWM signal from the digispark. I'm now able to run 12v to the leds and control them with low voltage from the controller. I'll let you know if something blows up........:eek:
     
    Last edited: Jul 25, 2017
  10. Wp100

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    965
    Likes:
    43
    Location:
    England
    [QUOTE="hoghunter, post: 1300734, member: 266737"
    The problem I am having now is figuring out how to connect my LED driver board into the circuit. I will be driving (2) or (3) 3W high power leds ea with a Vf of 2.2v. This particular driver board needs a min 7v apparently. The board has 7 pins.... (2) IN+, (2) IN-, PWM, LED+ & LED-. I cannot find any diagrams of how to connect this board if using the PWM. Here is the board I plan to use. I'm assuming it has a mosfet incorporated somewhere and that maybe I need to add a pull down/up resistor or something to my circuit, but that is over my head as you can imagine. Any ideas?
    [​IMG][/QUOTE]


    The first question to ask is do you have a 1W board or a 3W board ; for your 3W leds you need that latter.

    The makers do not seem to publish a datasheet on this board, but if you look at the other drivers on their page they do, so you can see how its done.

    The only thing not clear is if a 5v PWM signal is ok for the driver board, often it is.

    Any doubts, mail them.

    https://www.pcboard.ca/index.php?route=product/search&search=led drivers
     
  11. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas

    The first question to ask is do you have a 1W board or a 3W board ; for your 3W leds you need that latter.

    The makers do not seem to publish a datasheet on this board, but if you look at the other drivers on their page they do, so you can see how its done.

    The only thing not clear is if a 5v PWM signal is ok for the driver board, often it is.

    Any doubts, mail them.

    https://www.pcboard.ca/index.php?route=product/search&search=led drivers[/QUOTE]

    For the initial prototype, I am sending the PWM signal from the MCU's pin 1 to the base of a 2N3904 NPN transistor and letting that allow the ground circuit to open/close on the 12v feeding the leds. I have a 3w constant current led driver on the + side 12v. It seems to be working fine at this point.....
    Do you think what I am doing is ok, or am I causing a problem by not using a PWM driver instead of the trans and constant current driver?
    [​IMG]
     
  12. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas
    I am about to pull the trigger on this. I want to use it for my light sensor circuit. Is this the appropriate choice?

    upload_2017-7-20_19-56-58.png
     
  13. hoghunter

    hoghunter Member

    Joined:
    Jun 29, 2017
    Messages:
    195
    Likes:
    2
    Location:
    Texas
    Thank you, great suggestion!
     

Share This Page