Pulsing Leds Oddity

Hello, 
I'm not much good at these things . 

I have a dilemma. I modified some code from here. 

http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/

It works fantastically well .

Here's my modification . It controls eight LEDS. 

[code]
//
#include <math.h>
int ranNum;
int ranDel;
void setup() {

randomSeed(analogRead(0));

pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
    
    float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
    ranNum=random(5,13);
    
    
    analogWrite(ranNum, val);

Don’t comment too much on the code please. I don’t really know what I am doing .

However, my dilemma.

If the Arduino is plugged into the USB of the PC ( the LEDS are powered through transistors and a 5v powerpack ) the pulsing is slow and matches the example shown in the link above, that is , slow breathing .

If I unplug the PC from the Arduino the LEDS begins to pulse so much faster as if to flash on and off.

What difference would having the USB port of the Arduino plugged in make to the timing of the analogWrite ?

Thank you for your time if you read and reply.

All the best

Stuart

[/code]

the LEDS are powered through transistors and a 5v powerpack

How is the Arduino powered when you unplug the USB cable.
Show us a picture of your wiring.

That's not your whole code so it's a bit difficult to determine what's happening.

It's possible that it's most of your code, but there is at least a closing brace missing.

Please re-edit your first post, and put the complete code inside [ code] [ /code] tags.

I would try adding a delay(10); at the bottom of the loop and see what effect that has on the difference between plugged in and not plugged in.

I'm so sorry. I left out the tiny bracket thing at the bottom. 
[code]
//
#include <math.h>
int ranNum;
int ranDel;
void setup() {
// Seed RNG from analog port.
randomSeed(analogRead(0));
// Setup 3 output ports for LED's
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);

pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
    //Generate random number between 8 and 10
    
    float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
    ranNum=random(5,13);
    // Generate random delay time
  
    
    analogWrite(ranNum, val);   
 
}

As I said I don’t think its a code thing as it works when the Arduino USB is connected to the PC.

Sorry I don’t know how to show the wiring here .

When the Arduino is not plugged into the USB a 5v powerpack plugs into the Arduino… This same powerpack has the wires split to provide the correct amperage to the LEDS and transistors the are switched via the Base of the transistor.

So the powerpack powers the Arduino and also supplies the LED circuit. The Arduino does not connect to the LEDS. only to the Base of the transistor. Oh, and a 470 ohm resistor to make the transistor default to OFF .

Am I making myself clear ?

Thank you so much for taking an interest

All the best

Stuart

Gee, I hope that helps

[/code]

Please re-edit both your posts, and put the code inside [ code] [ /code] tags.

See How to use this forum, Section 7.

As I say, I would try adding a delay(10); at the bottom of the loop and see what effect that has on the difference between plugged in and not plugged in. It could (indirectly) still be a code thing, despite initial appearances.

BTW, the way your code is written, you are going to get random steps on each led, since the algorithm is spread over multiple leds. It will make the fade look more jagged.

If the only difference is power then have a close look at the voltage coming out of your 'power pack'.

Thanks again.

I think I have the code looking like your rules suggest.

I understand the way it works isn't going to be perfect. I suggested at the outset the way I have made the LEDs light was not perfect coding.

My enquiry was and still is why the rate ( VAL ) at analogWrite would change (faster ) because the USB was disconnected from the Arduino. .

The random effects look so good ( breathing, heart beat ...almost a sigh at times ) when USB is connected . Disconnected looks like disco dancing.

Why does the USB connection cause this ?

Hi Fungus,

No difference in power . Arduino connected to powerpack 5 v . LED array connected to same powerpack ,

USB connected - good result.

USB disconnected = fast flashing

Like the code changes in analogWrite ( VAL )

Sgms: My enquiry was and still is why the rate ( VAL ) at analogWrite would change (faster ) because the USB was disconnected from the Arduino. .

The random effects look so good ( breathing, heart beat ...almost a sigh at times ) when USB is connected . Disconnected looks like disco dancing.

Why does the USB connection cause this ?

Well, I have a theory, but you refuse to test my code change, so I can't help you.

Because you don't know what the issue is, I would suggest doing what people suggest here (whether it's me or anybody else), unless you have a strong reason not to. They may lead nowhere, but they may also lead somewhere.

When the Arduino is not plugged into the USB a 5v powerpack plugs into the Arduino.. This same powerpack has the wires split to provide the correct amperage to the LEDS and transistors the are switched via the Base of the transistor.

Where exactly are you plugging the powerpack into the arduino? If to the DC power connector then that is most likely your problem. Acceptable voltage for using the arduino external DC power connector is in the 6 to 12 vdc range, 5 volts is too low.

If your power pack is a good regulated source of +5vdc (filtered and regulated) then one safe method of connecting to an arduino is to take a spare usb cable and cut off the PC end and strip out the +5vdc and ground wires and wire them to your +5vdc power pack.

Hello again,

Thank you for all your lovely advice .

The addition of Delay (10); ,as suggested, does nothing to solve the behavioural oddity. It just makes the LEDs flash a little slower. No pulsing detected. Various Delay (x) settings just make the flashing slower . No sign of a pulse. Once the USB is disconnected the pulsing stops. Reconnect the USB and the heartbeat recommences.

When connected to USB the Arduino makes the LEDs go up and down in brightness as is evident in the link in my original post.

I changed the setting on the powerpack to 6 volts. I measured the output. 6.95 V unloaded. Drops to 6.24 to 6.7 with the LEDs powered and flashing .

I imagine the answer remains a mystery to those who might know . I really wonder why an unplugged USB on an Arduino would make the code change its behaviour

Thanks for all your help . I appreciate your attempts to understand.

Kindest regards

Stuart

Sorry, I'm having trouble understanding your descriptions of the behaviour. With the delay, I would have expected at the very least, the led changes to slow down. It sounds like they did, but they are still described as "flashing". Does that mean they go from OFF to ON, with no fade in between, and only stay on extremely briefly?

Sgms: I imagine the answer remains a mystery to those who might know

I don't know what this means

Sgms: I really wonder why an unplugged USB on an Arduino would make the code change its behaviour

There are a number of possibilities for this. The USB is not just a power cable, it's an interface cable. When you plug it in, the Arduino uses the USB cable to do a number of things, including sending code, operating as a serial port output, etc. It shouldn't cause a problem, but that is one reason why plugging in a USB cable could cause the code to change its behaviour since this functionality requires processing that is not required when the USB cable is not plugged in.

Hi, you can post your circuit diagram by adding it as an attachment, this is under ADDITIONAL OPTIONS at the bottom of the reply page. You seem to think its code problem, why when its the circuit you change that causes your problem. Can you also post us a picture of the project, what are you using to wire up the project?

Tom.... :)

My guess it that you have not wired up the ground correctly or at all and that the analogue input is floating when you remove the USB cable. Your code is irrelevant it is a hardware issue.

TomGeorge: You seem to think its code problem, why when its the circuit you change that causes your problem.

Read all the posts. The original poster didn't think it was a code problem. I saw a way that it could be a code problem so I wanted that investigated.

Grumpy_Mike: My guess it that you have not wired up the ground correctly or at all and that the analogue input is floating when you remove the USB cable. Your code is irrelevant it is a hardware issue.

How could this speed up the rate of flashing of the LEDS? I'm not saying you're wrong, but I don't see how the description of the problem could match this fault. Unless you're suggesting that because the LEDs are floating, they are effectively flashing pseudo-randomly.

I thought some one mentioned a pot.

As we all know there is no way that unplugging the USB port will affect anything. This is another one of those "my arduino is doing something that is not possible" threads from an OP that lacks the descriptive skills to say what his setup is and why he thinks it is behaving in an impossible way. Face it he hasn't even got the nouse to read the how to use this forum sticky and post his code correctly so it dosn't bode well for him understanding replies does it.

OP please prove me wrong.

I hear that.

Thanks to some of you for your help

I'm sorry to trouble those of you who resort to attempted insults.

I'll make sure I don't bother your fragile sensitivities again with my naivete and ignorance.

I'm sorry to trouble those of you who resort to attempted insults. I'll make sure I don't bother your fragile sensitivities again with my naivete and ignorance.

Everyone is trying to help. We ask questions so we can discover what you are doing wrong. If you want a solution, answer the questions we ask and provide the feedback when we ask for information. That's how it works.