Continue to Site

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.

  • 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.

Need help with controlling an led fading sketch

Status
Not open for further replies.

hoghunter

Member
Please read the 'Current status' portion below: I know I need to fix something, but don't know what. The sketch is verified as it is below.

Code:
/*
 updown_fade

 Fades an led up slowly, then back down slowly and utilizes condition statements

 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
 JAA





//Current status:
//The actual fading of the light  (up & down) is working exactly how I want it to, but now I need to control it:
//When 3.3v is attached to pin2 it skips FADE IN and goes directly to HOLD:
//If I don't connect the 3.3v lead to pin2, it does skip FADE IN and delays 30 seconds with the led off as expected, but then continues to HOLD,:
//keeping the led at 250 for 5 min, instead of looping back and starting over. 
//I am needing to clean up my controlling code to trigger the fading sequence via a digital 'HIGH' reading on pin 2 ?????:
// at which time it would go through the fading up & down sequence, then go back and check pin2 again. 
//or (else) if pin 2 is 'LOW' delay for 30 seconds (30000) then loop and digitalread pin 2 again:
//I almost had it working, but was getting an error at } else { saying there was no 'if' preceeding it:
//I have taken it back to it's basic fading sequence and will wait for input from you all. Thanks for any help.
//By the way, this is my first sketch and my microcontroller is a digispark.
//In a nutshell, the light will have a PIR motion sensor that will send a signal to pin2 to 'trigger' the fading up/down sequence which will:
//last approx 7 minutes total, then wait for the trigger again and start all over: 



 */
int sensorPin = 2;  //motion sensor out pin connected to digital pin 2 
int ledPin = 1;    // LED connected to digital pin 1
int fadeValue = 0;  //default brightness of led (variable)

void setup() {
// initialize the sensorPin as an input:
  pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}
void loop() {
//START
    for (int fadeValue = 0 ; fadeValue <= 255 ; fadeValue += 1);
    if(digitalRead(sensorPin)==HIGH) {
      // sets the value (range from 0 to 255):
//FADE IN  
// fade in from off to max in increments of 1 point:  
    analogWrite(ledPin, fadeValue);
    // wait for .235 seconds then loop
    delay(235);
} else {
      delay(30000);
      loop; }
{
//HOLD
//hold led at max brightness for 5 minutes
  analogWrite(ledPin, 250);
   delay(300000); }
//FADE OUT
// fade out from max to off in increments of 5 points:
 for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {  
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
     // wait for .25 seconds then loop
    delay(588);}
}



Any help/advice would be greatly appreciated.
 
delay(300000) never work Your code just sits there till the delay is done so there be no fading.

Look at blink without delay
 
Last edited:
delay(300000) never work Your code just sits there till the delay is done so there be no fading.

Look at blink without delay
Yes. Once the led has brightened to 255, I want it to 'hold' there at 255 for 5 minutes (300,000ms), then fade back out slowly.
Is the 300000 millisecs too much for the ATtiny85 chip on the digispark to handle?
 
If you look at this code your delays are way to big. You need your code to do something like this
Fade in if fully on hold that with millis for 5 minutes then fade out

Code:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
delay you have is 3000
 
I loaded your fade example in my digispark a few weeks ago to get the microcontroller set up and test my first circuit. It worked fine. However, I still need to 'hold' the led at 255 brightness for 5 minutes after the initial fade up sequence, then fade back out. Like I said, the actual 'fading' up or down in my sketch is working just like I want it to. It is the triggering by the PIR sensor and the timing that I am trying to control. I currently have a 30 second delay after the } else { and want it loop back to the beginning, not continue to the end of the sketch. Maybe that is not possible with this setup.
Using my test code below and connecting 3.3v to pin2, it goes straight to 'HOLD', which lights the led fully, then it fades out slowly, then loops directly back to 'HOLD'.
'START' & 'FADE IN' are completely ignored.
When I run it without connecting 3.3v to pin2, it goes directly to } else {, then goes to 'FADE OUT' and fades slowly, then loops directly back to } else {.
The functions that run are running exactly as I would expect. It is controlling the flow of the sketch itself that I am having trouble with.

Code:
/*
 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 sensorPin = 2;  //motion sensor out pin connected to digital pin 2
int ledPin = 1;    // LED connected to digital pin 1
int fadeValue = 0; //initial led brightness (variable)

void setup() {
// initialize the sensorPin as an input:
  pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}
void loop() {
//START
    for (int fadeValue = 0 ; fadeValue <= 255 ; fadeValue += 1); {
    if(digitalRead(sensorPin)==HIGH) {
      // sets the value (range from 0 to 255):
//FADE IN 
// fade in from off to max in increments of 1 point: 
    analogWrite(ledPin, fadeValue);
    // wait for .235 seconds then loop
    delay(235);
} else {
      analogWrite(ledPin, 50);
      delay(2000);
      analogWrite(ledPin, 0);
      delay(2000);
      analogWrite(ledPin, 50);
      delay(2000);
      analogWrite(ledPin, 0);
      delay(2000);
      analogWrite(ledPin, 50);
      delay(2000);
      analogWrite(ledPin, 0);
      delay(2000);  }
{   
//HOLD
//hold led at max brightness for 10 seconds
  analogWrite(ledPin, 255);
  delay(10000); }
//FADE OUT
// fade out from max to off in increments of 5 points:
 for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
     // wait for .25 seconds then loop
    delay(588);}
    }}
 
I have the sketch doing exactly what I want now. However, as I am 'triggering' the fade sequence with a PIR motion sensor, it will be sending approx 3v to
pin PO on the digispark whenever motion is sensed. But, what I am finding is that whenever I send 3.3v to PO it runs my delay loop instead of the fade sequence regardless of if I have it looking for a HIGH or LOW reading on that pin. When I take the 3.3v lead off of pin PO, it runs the fade sequence. So, I am wondering how I should be getting my PIR sensor signal to the microcontroller. I was able to figure out the code, but the electrical triggering part has me confused. Any help? :confused: Or, is it as simple as swapping my 'endless off loop' code with my fade sequence code? So just an electrical question...... Thanks
 
Apart from your code, how are you applying the 3v3 to P0 ? just tapping / plugging it with a 3v3 wire, then leaving it floating ?

Any such input should ideally be debounced or you can get a whole series of mixed up signals going to the micro.

A simple and usually effect switch input is like this circuit, its a press to go low, but you can reverse it to press to go high.
 

Attachments

  • 000321.jpg
    000321.jpg
    15.4 KB · Views: 246
  • 000322.jpg
    000322.jpg
    6.8 KB · Views: 253
Apart from your code, how are you applying the 3v3 to P0 ? just tapping / plugging it with a 3v3 wire, then leaving it floating ?

Any such input should ideally be debounced or you can get a whole series of mixed up signals going to the micro.

A simple and usually effect switch input is like this circuit, its a press to go low, but you can reverse it to press to go high.

I think that is what I needed. The low voltage (3.3v) signal will come from the sensor's 'out' pin, of course, but it sends the signal for a minimum of approx 3 seconds when triggered, so it is not a true 'momentary' switch. Will your 000321.jpg schematic work in my case? If 000322.jpg will work, that is even better.....
I really appreciate your help with this! Yes, I was just tapping a 3.3v source into P0 for testing ...
 
You say that your sensor gives out a High pulse when active, so you need to use the circuit in 322.

Vcc is connected to your 3v3 , the other side to 0v/GND , so P0 is help firmly at 0v/Low until the switch is closed and P0 becomes High.
( leaving P0 unconnected gives all sorts of random signals, not recommended)

The pull down resistor can be any value to hand from 1k to 10k.

Providing you use a new/clean small electronic type of switch that should give a good enough signal for your needs.

Adding extra components not really needed.

If you want to Debounce the signal on P0 then use the Arduino Debounce Example.

Lots of tutorial around to help you get going with hardware/software, here's just part of a whole series.
 
I'll go through the tutorials you linked to, but in the meantime, just to be clear:

I attach my 3.3v out from the motion sensor to Vcc on the digispark with the 2N3904 in line? Then connect a ground wire from PO to the sensor's ground wire with a 10k resistor in line? I think I am getting a sense of what's happening to the current to make the pin stay LOW or go HIGH....

thanks again for the help!!
 
No, I was talking about your original problems and using the circuit in 322 for a manual switch as input to P0.


Actual connection to the motion sensor is something to do when you have sorted your coding problems out.
Also what you have described in your last post for connection to the motion sensor /p0 is not clear at all, always provide a circuit diagram as its too easy to misunderstand without.
It may not be necessary to use a transistor as a 3v3 input to P0 should be seen as a High.
 
No, I was talking about your original problems and using the circuit in 322 for a manual switch as input to P0.


Actual connection to the motion sensor is something to do when you have sorted your coding problems out.
Also what you have described in your last post for connection to the motion sensor /p0 is not clear at all, always provide a circuit diagram as its too easy to misunderstand without.
It may not be necessary to use a transistor as a 3v3 input to P0 should be seen as a High.

I misunderstood. My coding issues have been fixed and the fade up/down portion of my sketch is working properly on the breadboard. That said, I will provide a schematic for you to look at shortly. Thanks.
 
Wp100,

This is what I have....... please comment...
I was either going to bring voltage from the LED driver into Vin on the controller as shown, or let the voltage from P1 trigger a 2n3904 transistor to allow an isolated circuit supply the LEDs.
Thanks, you are really a big help!!

FOLLOW UP: Regardless of whether digitalRead returns HIGH or LOW from PO, it executes the fade sequence when there is no voltage to PO. If I attach 3.3v to PO, it goes through the 'off' loop, again, that is regardless of whether I have HIGH or LOW in my 'if' statement. I guess I need to look at your second circuit again and try incorporating that for this testing like you said.

upload_2017-7-18_17-23-54.png
 
Last edited:
I reviewed this entire thread and I have to apologize for the frustration it must be causing. The tutorial link was very informative.
I now have PO connected to ground with a 10k pulldown resistor as you were telling me to do. Nothing is attached to the Vin pin on the controller. Just 5v & GND. PO is being read properly now as HIGH/LOW. When I close the PO circuit using 3.3v on the breadboard, pulling it HIGH, the fade sequence starts.......
I still have additional logic to add to the sketch, but good progress made today. Thanks again for your patience.
 
I reviewed this entire thread and I have to apologize for the frustration it must be causing. The tutorial link was very informative.
I now have PO connected to ground with a 10k pulldown resistor as you were telling me to do. Nothing is attached to the Vin pin on the controller. Just 5v & GND. PO is being read properly now as HIGH/LOW. When I close the PO circuit using 3.3v on the breadboard, pulling it HIGH, the fade sequence starts.......
I still have additional logic to add to the sketch, but good progress made today. Thanks again for your patience.


That sounds good.
To hep you locate your code bugs, try using the Serial Monitor and place Serial.prints in your code so you can 'see' whats actually happening as the code runs.


However on your diagram there are problems.

You cannot drive high powered leds directly from the chip / P1, it will simply burn out the chip.

You show Vin connected to the LED driver ? What are you using as the Led Driver ?

A led driver typically uses a control signal from the Micro/ Digispark, often a PWM signal, so it can drive the leds at various brightness.

For your input from the motion detector, the DP should accept 3v3 directly, my Arduino does, however you may want to protect both circuits by using a little opto coupler.
There are many similar devices , not just the one shown in the diagram.
You will probably have to lower the leds input resistor to that shown in the diagram, try 330R if you do not have a multimeter.
 

Attachments

  • 000323.jpg
    000323.jpg
    5.3 KB · Views: 234
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top